Esempio n. 1
0
        public Message ReplyFacebookPostOrComment(int conversationId, int postOrCommentId, string content, bool isCloseConversation = false)
        {
            var conversation = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.FacebookVisitorPost && conversation.Source != ConversationSource.FacebookWallPost)
            {
                throw SocialExceptions.BadRequest("Conversation source must be facebook visitor/wall post.");
            }

            var           messages      = Repository.FindAll().Include(t => t.Sender).Include(t => t.Receiver).Where(t => t.ConversationId == conversation.Id).ToList();
            var           postMessage   = messages.FirstOrDefault(t => t.ParentId == null);
            SocialAccount socialAccount = GetSocialAccountFromMessages(messages);

            if (socialAccount == null)
            {
                throw SocialExceptions.BadRequest("Facebook integration account can't be found from conversation.");
            }

            var     previousMessages = GetFacebookPreviousMessages(messages, postOrCommentId);
            Message comment          = null;

            foreach (var previousMessage in previousMessages)
            {
                if (previousMessage.IsDeleted)
                {
                    continue;
                }

                bool isReplyToReplyComment = previousMessage.ParentId != null && previousMessage.ParentId != postMessage.Id;
                if (isReplyToReplyComment)
                {
                    //todo @current message user
                    continue;
                }

                // publish comment
                string fbCommentId = _fbClient.PublishComment(socialAccount.Token, previousMessage.OriginalId, content);
                if (string.IsNullOrWhiteSpace(fbCommentId))
                {
                    continue;
                }

                var fbComment = _fbClient.GetComment(socialAccount.Token, fbCommentId);

                // add message
                comment = new Message
                {
                    ConversationId = conversation.Id,
                    Content        = content,
                    SenderId       = socialAccount.Id,
                    ReceiverId     = previousMessage.SenderId,
                    ParentId       = previousMessage.Id,
                    SendAgentId    = UserContext.UserId,
                    SendTime       = fbComment.created_time,
                    OriginalId     = fbCommentId,
                    OriginalLink   = fbComment.permalink_url,
                    Source         = MessageSource.FacebookPostComment
                };
                if (comment.ParentId != postMessage.Id)
                {
                    comment.Source = MessageSource.FacebookPostReplyComment;
                }

                Save(comment, isCloseConversation);

                break;
            }

            if (comment == null)
            {
                throw SocialExceptions.OriginalPostOrTweetHasBeenDeleted();
            }

            return(this.Find(comment.Id));
        }