public async override Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange change) { var result = new FacebookProcessResult(NotificationManager); string token = socialAccount.Token; if (IsDuplicatedMessage(change.Value.CommentId)) { return(result); } FbComment comment = FbClient.GetComment(socialAccount.Token, change.Value.CommentId); SocialUser sender = await GetOrCreateFacebookUser(socialAccount.Token, comment.from.id); var conversation = GetConversation(change.Value.PostId); if (conversation == null) { return(result); } Message message = FacebookConverter.ConvertToMessage(comment); message.SenderId = sender.Id; var parent = GetParent(change.Value.PostId, comment); if (parent != null) { message.ParentId = parent.Id; message.ReceiverId = parent.SenderId; bool ifParentIsComment = parent.ParentId != null; if (ifParentIsComment) { message.Source = MessageSource.FacebookPostReplyComment; } } message.ConversationId = conversation.Id; conversation.IfRead = false; conversation.Status = sender.Id != socialAccount.Id ? ConversationStatus.PendingInternal : ConversationStatus.PendingExternal; conversation.LastMessageSenderId = message.SenderId; conversation.LastMessageSentTime = message.SendTime; if (conversation.TryToMakeWallPostVisible(socialAccount)) { result.WithNewConversation(conversation); } conversation.Messages.Add(message); await UpdateConversation(conversation); await CurrentUnitOfWork.SaveChangesAsync(); if (!conversation.IsHidden) { result.WithNewMessage(message); } return(result); }
public override async Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange change) { var result = new FacebookProcessResult(NotificationManager); if (IsWallPost(change) && !socialAccount.IfConvertWallPostToConversation) { return(result); } if (!IsWallPost(change) && !socialAccount.IfConvertVisitorPostToConversation) { return(result); } string token = socialAccount.Token; if (IsDuplicatedMessage(MessageSource.FacebookPost, change.Value.PostId)) { return(result); } FbPost post = await FbClient.GetPost(socialAccount.Token, change.Value.PostId); SocialUser sender = await GetOrCreateFacebookUser(socialAccount.Token, post.from.id); Message message = FacebookConverter.ConvertToMessage(post); message.SenderId = sender.Id; if (message.SenderId != socialAccount.Id) { message.ReceiverId = socialAccount.Id; } var conversation = new Conversation { OriginalId = change.Value.PostId, Source = ConversationSource.FacebookVisitorPost, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = GetSubject(message.Content), LastMessageSenderId = message.SenderId, LastMessageSentTime = message.SendTime }; if (change.Value.Verb == "add" && message.SenderId == socialAccount.Id) { conversation.Source = ConversationSource.FacebookWallPost; conversation.IsHidden = true; } conversation.Messages.Add(message); await AddConversation(socialAccount, conversation); await CurrentUnitOfWork.SaveChangesAsync(); if (!conversation.IsHidden) { result.WithNewConversation(conversation); } return(result); }
private async Task Process(FacebookProcessResult result, FbMessage fbMessage, SocialAccount socialAccount, FbHookChange change) { SocialUser sender = await GetOrCreateFacebookUser(socialAccount.Token, fbMessage.SenderId); SocialUser receiver = await GetOrCreateFacebookUser(socialAccount.Token, fbMessage.ReceiverId); var existingConversation = GetUnClosedConversation(change.Value.ThreadId); if (existingConversation != null) { Message message = FacebookConverter.ConvertMessage(fbMessage, sender, receiver); message.ConversationId = existingConversation.Id; bool isNewMessage = fbMessage.SendTime > existingConversation.LastMessageSentTime; if (isNewMessage) { existingConversation.IfRead = false; existingConversation.Status = sender.Id != socialAccount.SocialUser.Id ? ConversationStatus.PendingInternal : ConversationStatus.PendingExternal; existingConversation.LastMessageSenderId = message.SenderId; existingConversation.LastMessageSentTime = message.SendTime; } existingConversation.Messages.Add(message); await UpdateConversation(existingConversation); await CurrentUnitOfWork.SaveChangesAsync(); result.WithNewMessage(message); } else { if (sender.Id == socialAccount.Id) { return; } Message message = FacebookConverter.ConvertMessage(fbMessage, sender, receiver); var conversation = new Conversation { OriginalId = change.Value.ThreadId, Source = ConversationSource.FacebookMessage, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = GetSubject(message.Content), LastMessageSenderId = message.SenderId, LastMessageSentTime = message.SendTime }; conversation.Messages.Add(message); await AddConversation(socialAccount, conversation); await CurrentUnitOfWork.SaveChangesAsync(); result.WithNewConversation(conversation); } }
private async Task AddPosts(FacebookProcessResult result, List <FbPost> posts) { if (!posts.Any()) { return; } var socialAccounts = _socialUserService.FindAll().Where(t => t.Source == SocialUserSource.Facebook && t.Type == SocialUserType.IntegrationAccount).ToList(); posts = posts.OrderBy(t => t.created_time).ToList(); using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) { using (CurrentUnitOfWork.SetSiteId(_account.SiteId)) { List <SocialUser> senders = await _socialUserService.GetOrCreateSocialUsers(_account.Token, posts.Select(t => t.from).ToList()); foreach (var post in posts) { var sender = senders.FirstOrDefault(t => t.OriginalId == post.from.id); if (sender == null) { continue; } var firstMessage = _fbConverter.ConvertToMessage(post); firstMessage.SenderId = sender.Id; var recipient = GetRecipient(post, socialAccounts); if (recipient != null) { // a post @ multiple social accounts. if (recipient.Id != _account.Id) { return; } firstMessage.ReceiverId = recipient.Id; } if (recipient == null && firstMessage.SenderId != _account.Id) { firstMessage.ReceiverId = _account.Id; } var conversation = new Conversation { OriginalId = post.id, Source = ConversationSource.FacebookVisitorPost, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = GetSubject(post.message), LastMessageSenderId = firstMessage.SenderId, LastMessageSentTime = firstMessage.SendTime }; conversation.Messages.Add(firstMessage); if (sender.OriginalId == _account.SocialUser.OriginalId) { conversation.Source = ConversationSource.FacebookWallPost; conversation.IsHidden = true; } if (_account.ConversationAgentId.HasValue && _account.ConversationPriority.HasValue) { conversation.AgentId = _account.ConversationAgentId.Value; conversation.Priority = _account.ConversationPriority.Value; } if (_account.ConversationDepartmentId.HasValue && _account.ConversationPriority.HasValue) { conversation.DepartmentId = _account.ConversationDepartmentId.Value; conversation.Priority = _account.ConversationPriority.Value; } if (conversation.Source == ConversationSource.FacebookWallPost && !_account.IfConvertWallPostToConversation) { continue; } if (conversation.Source == ConversationSource.FacebookVisitorPost && !_account.IfConvertVisitorPostToConversation) { continue; } await _conversationService.InsertAsync(conversation); result.WithNewConversation(conversation); } uow.Complete(); } } }