private async Task SaveTweet( TwitterProcessResult result, SocialAccount currentAccount, IList <SocialAccount> allAccounts, ITweet currentTweet, List <ITweet> tweets, string senderOriginalId, string receiverOriginalId, List <string> customerOrginalIds) { var sender = await _socialUserService.GetOrCreateTwitterUser(senderOriginalId); var receiver = await _socialUserService.GetOrCreateTwitterUser(receiverOriginalId); var message = TwitterConverter.ConvertToMessage(currentTweet); message.SenderId = sender.Id; message.ReceiverId = receiver.Id; await UnitOfWorkManager.RunWithNewTransaction(currentAccount.SiteId, async() => { var conversations = await FindConversations(tweets, customerOrginalIds); await SaveConversations(result, currentAccount, allAccounts, conversations, message); }); }
public async Task <TwitterProcessResult> ProcessTweet(SocialAccount currentAccount, ITweet currentTweet) { TwitterProcessResult result = new TwitterProcessResult(_notificationManager); // ignore if first tweet was sent by integration account. // 集成账号创建的第一条tweet不需要创建conversation. if (IsFirstTweetCreateByCurrentIntegrationAccount(currentAccount, currentTweet)) { return(result); } // ignore if tweet was created by customer but didn't mention current account. // 客户创建的Tweet, 但是没有@当前集成账号,不需要创建conversation. if (IsTweetCreatedByAnotherButDoNotMentionIntegrationAccount(currentAccount, currentTweet)) { return(result); } // ignore if tweet was sent at agent console. // tweet是在访客监控页面由agent回复的,则不需要再次处理 if (IsSendAtAgentConsole(currentTweet)) { return(result); } IList <SocialAccount> allAccounts = await _socialAccountService.GetAccountsAsync(SocialUserSource.Twitter); // ignore if tweet created by another integration account. // tweet由其他集成账号创建,其他账号的任务会处理,不需要重复处理请求。 if (IsCreateByOtherIntegrationAccount(currentAccount, allAccounts, currentTweet)) { return(result); } // ignore if first tweet was sent by other integration account. // 集成账号创建的第一条tweet不需要创建conversation. if (IsFristTweetCreateByOtherIntegrationAccount(currentAccount, allAccounts, currentTweet)) { return(result); } // ignore if tweet mentioned multiple integration account // and other integration account should process this tweet in case we process the same tweet again; // 客户创建的Tweet, 但是@了多个集成账号,让优先级高(按@顺序)的集成账号处理, 防止数据重复被处理。 if (IsTweetCreatedByAnotherAndMentionedAnotherIntegrationAccount(currentAccount, allAccounts, currentTweet)) { return(result); } List <ITweet> tweets = _twitterTreeWalker.BuildTweetTree(currentTweet); return(await AddTweets(currentAccount, allAccounts, tweets)); }
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); } }
private async Task AddTweet( TwitterProcessResult result, SocialAccount currentAccount, IList <SocialAccount> allAccounts, List <ITweet> tweets, ITweet currentTweet) { var senderOriginalId = currentTweet.CreatedBy.IdStr; var receiverOrignalId = GetReceiverOriginalId(currentAccount, allAccounts, currentTweet); if (string.IsNullOrEmpty(receiverOrignalId)) { return; } // integration account sent to intergration account // 集成账号@集成账号 if (IsIntegrationAccount(senderOriginalId, allAccounts) && IsIntegrationAccount(receiverOrignalId, allAccounts)) { List <string> customerOriginalIds = _twitterTreeWalker.FindCustomerOriginalIdsInTweetTree(currentTweet, tweets, allAccounts); if (!customerOriginalIds.Any()) { return; } await SaveTweet(result, currentAccount, allAccounts, currentTweet, tweets, senderOriginalId, receiverOrignalId, customerOriginalIds); } // integration account sent to customer // 集成账号@客户 if (IsIntegrationAccount(senderOriginalId, allAccounts) && !IsIntegrationAccount(receiverOrignalId, allAccounts)) { var customerOriginalIds = new List <string> { receiverOrignalId }; await SaveTweet(result, currentAccount, allAccounts, currentTweet, tweets, senderOriginalId, receiverOrignalId, customerOriginalIds); } // customer sent to integration account // 客户@集成账号 if (!IsIntegrationAccount(senderOriginalId, allAccounts) && IsIntegrationAccount(receiverOrignalId, allAccounts)) { var customerOriginalIds = new List <string> { senderOriginalId }; await SaveTweet(result, currentAccount, allAccounts, currentTweet, tweets, senderOriginalId, receiverOrignalId, customerOriginalIds); } }
private async Task <TwitterProcessResult> AddTweets(SocialAccount currentAccount, IList <SocialAccount> allAccounts, List <ITweet> tweets) { var result = new TwitterProcessResult(_notificationManager); if (!tweets.Any()) { return(result); } tweets = tweets.OrderByDescending(t => t.CreatedAt).ToList(); // 如果是集成账号回复的客户的tweet,并且客户的tweet没有@任何集成账号,则不处理 if (IsReplyToIrrelevantPerson(currentAccount, allAccounts, tweets.First(), tweets)) { return(result); } foreach (var tweet in tweets) { // if current integration account doesn't involved. if (!IsIntergrationAccountInvolved(tweet, allAccounts)) { continue; } // ignore if processed before. if (_messageService.FindAll().Any(t => t.OriginalId == tweet.IdStr)) { break; } await AddTweet(result, currentAccount, allAccounts, tweets, tweet); } return(result); }
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); }