Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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();
                }
            }
        }
Exemplo n.º 3
0
        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);
            }
        }