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 AddReplyComments(FacebookProcessResult result, List <FbComment> replyComments) { if (!replyComments.Any()) { return; } replyComments = replyComments.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, replyComments.Select(t => t.from).ToList()); var postIds = replyComments.Select(t => t.PostId).Distinct().ToList(); var conversations = _conversationService.FindAll().Where(t => postIds.Contains(t.OriginalId)).ToList(); var parentIds = replyComments.Select(t => t.parent.id).Distinct().ToList(); var parents = _messageService.FindAll().Where(t => parentIds.Contains(t.OriginalId)).ToList(); foreach (var replyComment in replyComments) { var sender = senders.FirstOrDefault(t => t.OriginalId == replyComment.from.id); if (sender == null) { continue; } var conversation = conversations.FirstOrDefault(t => t.OriginalId == replyComment.PostId); if (conversation == null) { continue; } var parent = parents.FirstOrDefault(t => t.OriginalId == replyComment.parent.id); if (parent == null) { continue; } var message = _fbConverter.ConvertToMessage(replyComment); message.Source = MessageSource.FacebookPostReplyComment; message.SenderId = sender.Id; message.ReceiverId = parent.SenderId; message.ParentId = parent.Id; message.ConversationId = conversation.Id; conversation.Messages.Add(message); conversation.Status = ConversationStatus.PendingInternal; conversation.LastMessageSenderId = message.SenderId; conversation.LastMessageSentTime = message.SendTime; conversation.TryToMakeWallPostVisible(_account); result.WithNewMessage(message); } uow.Complete(); } } }
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); } }
public async override Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange data) { var result = new FacebookProcessResult(NotificationManager); var conversation = GetConversation(data.Value.PostId); if (conversation != null) { await DeleteConversation(conversation); result.WithDeletedConversation(conversation); } return(result); }
public async Task ProcessWebHookData(FbHookData fbData) { if (fbData == null || !fbData.Entry.Any()) { return; } var changes = fbData.Entry.First().Changes; if (changes == null || !changes.Any()) { return; } if (fbData.Object != "page") { return; } string pageId = fbData.Entry.First().Id; SocialAccount socialAccount = await GetSoicalAccount(pageId); if (socialAccount == null) { return; } foreach (var change in changes) { if (socialAccount != null) { var strategory = _strategyFactory.Create(change); if (strategory != null) { FacebookProcessResult result = null; await UnitOfWorkManager.Run(TransactionScopeOption.RequiresNew, socialAccount.SiteId, async() => { result = await strategory.Process(socialAccount, change); }); if (result != null) { await result.Notify(socialAccount.SiteId); } } } } }
public async override Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange change) { var result = new FacebookProcessResult(NotificationManager); var sources = new[] { MessageSource.FacebookPostComment, MessageSource.FacebookPostReplyComment }; var message = GetMessage(sources, change.Value.CommentId); if (message != null) { await DeleteMessage(message); result.WithDeletedMessage(message); } return(result); }
public async Task <FacebookProcessResult> PullVisitorPostsFromFeed(SocialAccount account) { var result = new FacebookProcessResult(_notificationManager); _account = account; var data = await _fbClient.GetVisitorPosts(_account.SocialUser.OriginalId, _account.Token); await Init(data); RemoveDuplicated(); await AddPosts(result, PostsToBeCreated); await AddComments(result, CommentsToBeCreated); await AddReplyComments(result, ReplyCommentsToBeCretaed); return(result); }
public async Task <FacebookProcessResult> PullTaggedVisitorPosts(SocialAccount account) { var result = new FacebookProcessResult(_notificationManager); if (!account.IfConvertVisitorPostToConversation) { return(result); } _account = account; var data = await _fbClient.GetTaggedVisitorPosts(_account.SocialUser.OriginalId, _account.Token); await Init(data); RemoveDuplicated(); await AddPosts(result, PostsToBeCreated); await AddComments(result, CommentsToBeCreated); await AddReplyComments(result, ReplyCommentsToBeCretaed); await result.Notify(account.SiteId); return(result); }
public async override Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange change) { var result = new FacebookProcessResult(NotificationManager); if (!socialAccount.IfConvertMessageToConversation) { return(result); } IList <FbMessage> fbMessages = await FbClient.GetMessagesFromConversationId(socialAccount.Token, change.Value.ThreadId, 10); string[] orgiginalIds = fbMessages.Select(t => t.Id).ToArray(); List <Message> existedMessages = MessageService.FindAll().Where(t => t.Source == MessageSource.FacebookMessage && orgiginalIds.Contains(t.OriginalId)).ToList(); // remove duplicated messages. fbMessages = fbMessages.Where(t => !existedMessages.Any(m => m.OriginalId == t.Id)).OrderByDescending(t => t.SendTime).ToList(); foreach (var fbMessage in fbMessages) { await Process(result, fbMessage, socialAccount, change); } return(result); }
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(); } } }