private async Task <Shared.DTO.Conversations.Chat> CreateGroup(
            AppUser creator,
            string chatImageUrl,
            string chatName,
            bool isPublic)
        {
            if (chatName.Length > MaxNameLength)
            {
                throw new InvalidDataException("Chat name was too long.");
            }

            try
            {
                var imageUrl = chatImageUrl ?? chatDataProvider.GetGroupPictureUrl();

                var newChat = new ConversationDataModel
                {
                    IsGroup      = true,
                    Name         = chatName,
                    FullImageUrl = imageUrl,
                    ThumbnailUrl = imageUrl,
                    IsPublic     = isPublic
                };

                await conversationRepository.AddAsync(newChat);

                await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create(
                                                                creator.Id,
                                                                newChat));

                var role = await rolesRepository.AddAsync(
                    ChatRoleDataModel.Create(newChat, creator.Id, ChatRole.Creator));

                newChat.Roles = new List <ChatRoleDataModel>()
                {
                    role
                };
                newChat.participants = new List <AppUser> {
                    creator
                };

                await unitOfWork.Commit();

                return(newChat.ToChatDto(creator.Id));
            }
            catch (Exception e)
            {
                throw new InvalidDataException("Couldn't create group because of unexpected error.", e);
            }
        }
Beispiel #2
0
        public async Task HandleAsync(CreateConversationCommand command)
        {
            if (await _conversationRepository.ExistsAsync(new Participant(command.FirstParticipant), new Participant(command.SecondParticipant)))
            {
                return;
            }
            var conversation = Conversation.Create(new ConversationId(command.ConversationId), new Participant(command.FirstParticipant), new Participant(command.SecondParticipant));

            await _conversationRepository.AddAsync(conversation);
        }
Beispiel #3
0
        public async Task <ConversationViewModel> AddConversation(AddConversationRequest request)
        {
            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            var firstmember = new ConversationMember()
            {
                DateJoin = DateTime.Now,
                JoinBy   = currentUser.OId,
                MemberId = currentUser.OId,
                Nickname = $"{currentUser.FirstName} {currentUser.LastName}",
                Role     = ConversationRole.Admin
            };


            request.Participants.Add(firstmember);

            Conversation existConversation = new Conversation()
            {
                Participants = new List <ConversationMember>()
            };

            foreach (Conversation conver in conversationRepository.GetAll())
            {
                if (Feature.IsEqual(conver.Participants, request.Participants) == true)
                {
                    existConversation = conver;
                    break;
                }
            }

            if (existConversation.Participants.Count != 0)
            {
                return(mapper.Map <ConversationViewModel>(existConversation));
            }

            Conversation conversation = MessageAdapter.FromRequest(request, httpContextAccessor, userRepository);

            await conversationRepository.AddAsync(conversation);

            ClientGroup clientGroup = new ClientGroup()
            {
                UserIds   = request.Participants.Select(x => x.MemberId).ToList(),
                Name      = conversation.Id.ToString(),
                GroupType = Feature.GetTypeName(conversation)
            };

            await clientGroupRepository.AddAsync(clientGroup);

            Thread.Sleep(1000);
            return(mapper.Map <ConversationViewModel>(conversation));
        }
Beispiel #4
0
        public async Task AddConversation(List <Guid> userId)
        {
            var conversation = new Conversation();

            foreach (var i in userId)
            {
                var user = await _userRepository.GetAsync(i);

                var uc = new UserConversation();

                uc.User   = user;
                uc.UserId = user.Id;

                uc.Conversation   = conversation;
                uc.ConversationId = conversation.Id;

                conversation.UserConversations.Add(uc);
                //user.UserConversations.
            }

            await _conversationRepository.AddAsync(conversation);
        }