예제 #1
0
        public async Task <ChatDialogDTO> GetDialog(long dialogId)
        {
            var dialog = await _unitOfWork.ChatDialogRepository.Query
                         .Include(x => x.Messages)
                         .Include(x => x.Participant1)
                         .Include(x => x.Participant2)
                         .SingleAsync(x => x.Id == dialogId);

            ChatDialogDTO dl = new ChatDialogDTO
            {
                Id = dialogId,
                ParticipantOneId   = dialog.Participant1.Id,
                ParticipantTwoId   = dialog.Participant2.Id,
                Participant1_Hided = dialog.Participant1_Hided,
                Participant2_Hided = dialog.Participant2_Hided,
                Messages           = dialog.Messages.Where(x => !x.IsDeleted).Select(x => new ChatMessageDTO
                {
                    DialogId  = x.Dialog.Id,
                    MessageId = x.Id,
                    Date      = x.Date,
                    IsReaded  = x.IsReaded,
                    Message   = x.Message,
                    OwnerId   = x.Owner.Id,
                    Files     = x.Files.Select(f => new ChatFileDTO
                    {
                        Id             = f.Id,
                        OriginalName   = f.OriginalName,
                        ServerPathName = f.ServerPathName
                    }).ToList()
                }).ToList()
            };

            return(dl);
        }
예제 #2
0
        public async Task <ChatDialogDTO> CreateDialog(ChatDialogDTO dialog)
        {
            var participant1 = await _unitOfWork.AccountRepository.GetByIdAsync(dialog.ParticipantOneId);

            var participant2 = await _unitOfWork.AccountRepository.GetByIdAsync(dialog.ParticipantTwoId);

            var dl = new ChatDialog
            {
                Participant1 = participant1,
                Participant2 = participant2,
                Messages     = new List <ChatMessage>()
            };

            var createdDialog = _unitOfWork.ChatDialogRepository.Create(dl);
            await _unitOfWork.SaveAsync();

            return(new ChatDialogDTO
            {
                Id = createdDialog.Id,
                ParticipantOneId = createdDialog.Participant1.Id,
                ParticipantTwoId = createdDialog.Participant2.Id,
                Participant1_Hided = dialog.Participant1_Hided,
                Participant2_Hided = dialog.Participant2_Hided,
                ParticipantAvatar = createdDialog.Participant2.CroppedAvatar ?? createdDialog.Participant2.Avatar,
                LastMessageTime = DateTimeOffset.Now
            });
        }
예제 #3
0
        public async Task <IActionResult> CreateDialog([FromBody] ChatDialogDTO dialog)
        {
            var d = await chatService.CreateDialog(dialog);

            return(d == null?StatusCode(409) as IActionResult
                   : Ok(d));
        }
예제 #4
0
        public async Task <ChatDialogDTO> GetDialog(long dialogId, long ownerId)
        {
            var dialog = await _unitOfWork.ChatDialogRepository.Query
                         .Include(x => x.Messages)
                         .Include(x => x.Participant1)
                         .Include(x => x.Participant2)
                         .SingleAsync(x => x.Id == dialogId);

            string avatar;
            string name;

            if (dialog.Participant1.Id == ownerId)
            {
                avatar = dialog.Participant2.CroppedAvatar ?? dialog.Participant2.Avatar;
                name   = GetName(dialog.Participant2);
            }
            else
            {
                avatar = dialog.Participant1.CroppedAvatar ?? dialog.Participant1.Avatar;
                name   = GetName(dialog.Participant1);
            }

            ChatDialogDTO dl = new ChatDialogDTO
            {
                Id = dialogId,
                ParticipantOneId = dialog.Participant1.Id,
                ParticipantTwoId = dialog.Participant2.Id,

                Participant1_Hided = dialog.Participant1_Hided,
                Participant2_Hided = dialog.Participant2_Hided,

                ParticipantName     = name,
                ParticipantAvatar   = avatar,
                IsReadedLastMessage = false,
                Messages            = dialog.Messages.Select(x => new ChatMessageDTO
                {
                    MessageId = x.Id,
                    DialogId  = x.Dialog.Id,
                    Date      = x.Date,
                    IsReaded  = x.IsReaded,
                    Message   = x.Message,
                    OwnerId   = x.Owner.Id,
                    Files     = x.Files.Select(f => new ChatFileDTO
                    {
                        Id             = f.Id,
                        OriginalName   = f.OriginalName,
                        ServerPathName = f.ServerPathName
                    }).ToList()
                }).ToList()
            };

            return(dl);
        }
예제 #5
0
        public async Task <HttpResponseMessage> CreateDialog(ChatDialogDTO dialog)
        {
            try
            {
                var createdDialog = await _chatService.CreateDialog(dialog);

                return(Request.CreateResponse(HttpStatusCode.OK, createdDialog));
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
예제 #6
0
        public async Task <ChatDialogDTO> StartChatWithUser(UserProfileDTO user)
        {
            ChatUserDTO   chatUser = mapper.Map <ChatUserDTO>(user);
            ChatDialogDTO dialog   = new ChatDialogDTO()
            {
                DialogType = ChatGroup.dialog
            };

            List <ChatUserDTO> dp = new List <ChatUserDTO>();

            dp.Add(mapper.Map <ChatUserDTO>(user));
            dialog.Participants = dp;

            return(await CreateDialog(dialog));
        }
예제 #7
0
        public async Task <ChatDialogDTO> CreateDialog(ChatDialogDTO dialog)
        {
            if (dialog.Participants.Count() < 1)
            {
                return(null);
            }

            var currentUser = await this.currentUser.GetCurrentUserProfile();

            if (currentUser == null)
            {
                return(null);
            }

            var targetIdentifier = dialog.Participants.Sum(p => p.GetHashCode()) + currentUser.GetHashCode();
            var d = await uow.GetRepository <ChatDialog>()
                    .GetAsync(dd => dd.Identifier == targetIdentifier && dd.DialogType == ChatGroup.dialog);

            if (d == null)
            {
                var         repo = uow.GetRepository <UserProfile>();
                UserProfile currentInterlocator;
                List <DialogParticipant> dp = new List <DialogParticipant>();

                foreach (var p in dialog.Participants)
                {
                    currentInterlocator = await repo.GetAsync(p.Id);

                    if (currentInterlocator != null)
                    {
                        dp.Add(new DialogParticipant()
                        {
                            Participant = currentInterlocator
                        });
                    }
                }
                dp.Add(new DialogParticipant()
                {
                    Participant = currentUser
                });

                if (dp.Count < 2)
                {
                    return(null);
                }


                var newDialog = await uow.GetRepository <ChatDialog>()
                                .CreateAsync(new ChatDialog()
                {
                    Identifier         = targetIdentifier,
                    DialogParticipants = dp,
                    DialogType         = ChatGroup.dialog
                }
                                             );

                await uow.SaveAsync();

                foreach (var participant in newDialog.DialogParticipants)
                {
                    await this.signalRChatService.DialogsChanges($"{ChatGroup.direct.ToString()}{participant.ParticipantId}", newDialog.Id);
                }
                return(mapper.Map <ChatDialogDTO>(newDialog));
            }
            else
            {
                return(mapper.Map <ChatDialogDTO>(d));
            }
        }