Example #1
0
 private async Task <Message> CreateReplyMessage(ReplyMessageDto replyMessageDto, string senderId, Message messageParent)
 {
     return(await _unitOfWork.MessageRepository?.AddAsync(
                new Message
     {
         SenderId = senderId,
         MessageData = replyMessageDto.MessageData,
         ParrentMessageId = messageParent.Id,
         DeletedForAll = false,
         DeletedForSender = false,
     }));
 }
Example #2
0
        public async Task <ActionResult> ReplyForMessage(ReplyMessageDto replyMessageDto)
        {
            try
            {
                var userId = this.User.Claims.First(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
                await _messagesService.ReplyMessage(replyMessageDto, userId);

                return(Ok(MessageControllerConstants.YourReplySended));
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #3
0
        public async Task SendMessageToChatAsync(Chat.Contracts.Entity.Chat chatWhereWeSendMessage, ReplyMessageDto replyMessageDto, string senderId, Message messageParent)
        {
            using (var transaction = await _unitOfWork.BeginTransactionAsync())
            {
                if (chatWhereWeSendMessage.Public)
                {
                    var senderInChat = chatWhereWeSendMessage.UserChats?.Where(uch => uch.UserId == senderId).FirstOrDefault();
                    if (senderInChat == null)
                    {
                        await _unitOfWork.UserChatRepository.AddAsync(
                            new UserChat
                        {
                            ChatId = chatWhereWeSendMessage.Id,
                            UserId = senderId
                        });

                        await _unitOfWork.CommitAsync();
                    }
                    Message message = await CreateReplyMessage(replyMessageDto, senderId, messageParent);

                    await _unitOfWork.CommitAsync();
                    await AttachMessageToChat(message, chatWhereWeSendMessage);
                }
                if (!chatWhereWeSendMessage.Public)
                {
                    var senderInChat = chatWhereWeSendMessage.UserChats?.Where(uch => uch.UserId == senderId).FirstOrDefault();
                    if (senderInChat == null)
                    {
                        throw new ArgumentException("You are not in this chat");
                    }

                    var message = await CreateReplyMessage(replyMessageDto, senderId, messageParent);

                    await _unitOfWork.CommitAsync();
                    await AttachMessageToChat(message, chatWhereWeSendMessage);
                }
                await _unitOfWork.CommitAsync();

                await transaction.CommitAsync();

                return;
            }
        }
Example #4
0
        /// <summary>
        /// Reply for message chat.
        /// If you set ReplyForSenderInPrivateChat = true;
        /// We try to find this chat by ReplyChatId.
        /// If this chat exists and it is not public and you are not attached for this chat or reciverUser
        /// is not attached we will be try to find default private chat for you and your reciever and send reply there.
        /// Another way we will be create this chat attache thre you and your reciever and send message there.
        /// If Chat not exist we will try find default chat and make the same action that describe above.
        /// If you set ReplyForSenderInPrivateChat = false;
        /// We try to find this chat by ReplyChatId.
        /// If this chat exists we send a reply message for here if this message that we want to be replied attached for this chat and you are attached also for this chat.
        /// </summary>
        public async Task ReplyMessage(ReplyMessageDto replyMessageDto, string userId)
        {
            if (string.IsNullOrEmpty(replyMessageDto.MessageData))
            {
                throw new ArgumentException("Please Write your reply message");
            }

            var parrentMessage = await _unitOfWork.MessageRepository.GetByIdAsync(replyMessageDto.MessageID);

            if (parrentMessage == null || parrentMessage.DeletedForAll)
            {
                throw new ArgumentException("Your message doesn't exist");
            }

            var recieverUser = await _userManager.FindByIdAsync(parrentMessage.SenderId);

            if (recieverUser == null)
            {
                throw new Exception("we can't send reply message");
            }

            if (replyMessageDto.ReplyForSenderInPrivateChat)
            {
                var chatWhereWeSendMessage = await _unitOfWork.ChatRepository?.GetChatByIdWithAllIncludes(replyMessageDto.ReplyChatId);

                if (chatWhereWeSendMessage != null && !chatWhereWeSendMessage.Public &&
                    ChatContainUser(userId, chatWhereWeSendMessage).Value &&
                    ChatContainUser(recieverUser, chatWhereWeSendMessage).Value)
                {
                    await SendMessageToChatAsync(
                        chatWhereWeSendMessage,
                        replyMessageDto,
                        userId,
                        parrentMessage
                        );

                    return;
                }

                chatWhereWeSendMessage = _unitOfWork.ChatRepository.Find(ch => ch.DefaultChat && ch.Public == false &&
                                                                         (ChatContainUser(userId, ch).Value) &&
                                                                         (ChatContainUser(userId, ch).Value)).FirstOrDefault();

                if (chatWhereWeSendMessage != null)
                {
                    await SendMessageToChatAsync(
                        chatWhereWeSendMessage,
                        replyMessageDto,
                        userId,
                        parrentMessage
                        );

                    return;
                }
                else
                {
                    chatWhereWeSendMessage = await CreateDefaultChat(false, userId, recieverUser);
                    await SendMessageToChatAsync(
                        chatWhereWeSendMessage,
                        replyMessageDto,
                        userId,
                        parrentMessage
                        );

                    return;
                }
            }
            else
            {
                var chatWhereWeSendMessage = await _unitOfWork.ChatRepository?.GetChatByIdWithAllIncludes(replyMessageDto.ReplyChatId);

                if (chatWhereWeSendMessage != null && chatWhereWeSendMessage.Public &&
                    chatWhereWeSendMessage.ChatMessages.Any(chm => chm.MessageId == replyMessageDto.MessageID))
                {
                    await SendMessageToChatAsync(
                        chatWhereWeSendMessage,
                        replyMessageDto,
                        userId,
                        parrentMessage
                        );

                    return;
                }
                else
                {
                    throw new ArgumentException("We cnan't send reply");
                }
            }
        }