private async Task SaveConversations( TwitterProcessResult result, SocialAccount currentAccount, IList <SocialAccount> allAccounts, IList <Conversation> conversations, Message message) { if (conversations.Any()) { foreach (var conversation in conversations) { if (conversation.Messages.Any(t => t.OriginalId == message.OriginalId)) { continue; } message.ConversationId = conversation.Id; conversation.Messages.Add(message); if (message.SendTime > conversation.LastMessageSentTime) { conversation.IfRead = false; bool isSendByIntegrationAccount = allAccounts.Any(t => t.Id == message.SenderId); conversation.Status = isSendByIntegrationAccount ? ConversationStatus.PendingExternal : ConversationStatus.PendingInternal; conversation.LastMessageSenderId = message.SenderId; conversation.LastMessageSentTime = message.SendTime; } _conversationService.Update(conversation); await CurrentUnitOfWork.SaveChangesAsync(); result.WithUpdatedConversation(conversation); result.WithNewMessage(message); } } else { var conversation = new Conversation { OriginalId = message.OriginalId, Source = ConversationSource.TwitterTweet, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = TwitterUtil.GetSubject(message.Content), LastMessageSenderId = message.SenderId, LastMessageSentTime = message.SendTime }; conversation.Messages.Add(message); _conversationService.AddConversation(currentAccount, conversation); await CurrentUnitOfWork.SaveChangesAsync(); result.WithNewConversation(conversation); } }
public async Task <TwitterProcessResult> ProcessDirectMessage(SocialAccount account, IMessage directMsg) { TwitterProcessResult result = new TwitterProcessResult(_notificationManager); if (!account.IfConvertMessageToConversation) { return(result); } bool isDuplicated = _messageService.IsDuplicatedMessage(MessageSource.TwitterDirectMessage, directMsg.Id.ToString()); if (isDuplicated) { return(result); } bool isSendByAccount = directMsg.SenderId.ToString() == account.SocialUser.OriginalId; SocialUser sender = await _socialUserService.GetOrCreateTwitterUser(directMsg.Sender); SocialUser recipient = await _socialUserService.GetOrCreateTwitterUser(directMsg.Recipient); var existingConversation = _conversationService.GetTwitterDirectMessageConversation(sender, recipient); if (existingConversation != null) { var message = TwitterConverter.ConvertToMessage(directMsg); message.SenderId = sender.Id; message.ReceiverId = recipient.Id; message.ConversationId = existingConversation.Id; existingConversation.IfRead = false; bool isSendByIntegrationAccount = sender.Id == account.Id & recipient.Id != account.Id; existingConversation.Status = isSendByIntegrationAccount ? ConversationStatus.PendingExternal : ConversationStatus.PendingInternal; existingConversation.LastMessageSenderId = message.SenderId; existingConversation.LastMessageSentTime = message.SendTime; existingConversation.Messages.Add(message); _conversationService.Update(existingConversation); CurrentUnitOfWork.SaveChanges(); result.WithUpdatedConversation(existingConversation); result.WithNewMessage(message); } else { if (sender.Id == account.SocialUser.Id) { return(result); } var message = TwitterConverter.ConvertToMessage(directMsg); message.SenderId = sender.Id; message.ReceiverId = recipient.Id; var conversation = new Conversation { OriginalId = directMsg.Id.ToString(), Source = ConversationSource.TwitterDirectMessage, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = TwitterUtil.GetSubject(message.Content), LastMessageSenderId = message.SenderId, LastMessageSentTime = message.SendTime }; conversation.Messages.Add(message); _conversationService.AddConversation(account, conversation); CurrentUnitOfWork.SaveChanges(); result.WithNewConversation(conversation); } return(result); }