Exemple #1
0
        public IList <FilterManageDto> SortingFilters([Required] IList <FilterSortDto> sortDtoList)
        {
            if (!sortDtoList.Any())
            {
                throw SocialExceptions.BadRequest("sortDtoList is required.");
            }

            return(_appService.Sorting(sortDtoList));
        }
        protected override Expression <Func <Conversation, bool> > After(string date)
        {
            DateTime value;

            if (!DateTime.TryParse(date, out value))
            {
                throw SocialExceptions.BadRequest("The value of date time is invalid");
            }
            return(t => t.LastMessageSentTime > value);
        }
Exemple #3
0
        protected override Expression <Func <Conversation, bool> > Before(string date)
        {
            DateTime value;

            if (!DateTime.TryParse(date, out value))
            {
                throw SocialExceptions.BadRequest("The value of date time is invalid");
            }
            return(t => t.ModifiedTime.HasValue && t.ModifiedTime.Value < value);
        }
Exemple #4
0
        public void CheckFieldValue(List <FilterCondition> filterConditons)
        {
            var conversationField = _conversationFieldOptionService.FindAllAndFillOptions().Where(t => filterConditons.Select(f => f.FieldId).Contains(t.Id)).ToList();

            foreach (var filterConditon in filterConditons)
            {
                if (conversationField.Where(t => t.Id == filterConditon.FieldId).FirstOrDefault().DataType == FieldDataType.DateTime)
                {
                    if (conversationField.Where(t => t.Id == filterConditon.FieldId && t.Options.Any(o => o.Value == filterConditon.Value)).Count() == 0)
                    {
                        if (filterConditon.MatchType == ConditionMatchType.Between)
                        {
                            string[] value = filterConditon.Value.Split('|');
                            DateTime DateTime1;
                            if (!DateTime.TryParse(value[0], out DateTime1))
                            {
                                throw SocialExceptions.BadRequest("The value of date time is invalid");
                            }
                            DateTime DateTime2;
                            if (!DateTime.TryParse(value[1], out DateTime2))
                            {
                                throw SocialExceptions.BadRequest("The value of date time is invalid");
                            }
                        }
                        else
                        {
                            DateTime date;
                            if (!DateTime.TryParse(filterConditon.Value, out date))
                            {
                                throw SocialExceptions.BadRequest($"The value's type is not DateTime : '{filterConditon.Value}' ");
                            }
                        }
                    }
                }
                else if (conversationField.Where(t => t.Id == filterConditon.FieldId).FirstOrDefault().DataType == FieldDataType.Number)
                {
                    int number;
                    if (!int.TryParse(filterConditon.Value, out number))
                    {
                        throw SocialExceptions.BadRequest($"The value's type is not Number : '{filterConditon.Value}' ");
                    }
                }
                else if (conversationField.Where(t => t.Id == filterConditon.FieldId).FirstOrDefault().DataType == FieldDataType.Option)
                {
                    if (conversationField.Where(t => t.Id == filterConditon.FieldId && t.Options.Any(o => o.Value == filterConditon.Value)).Count() == 0)
                    {
                        if ((!departmentAddOptions.Contains(filterConditon.Value) || !departmentFieldNames.Contains(conversationField.Where(t => t.Id == filterConditon.FieldId).FirstOrDefault().Name)) &&
                            (!agentAddOptions.Contains(filterConditon.Value) || !agentsFieldNames.Contains(conversationField.Where(t => t.Id == filterConditon.FieldId).FirstOrDefault().Name)))
                        {
                            throw SocialExceptions.BadRequest($"The value's type is not Option : '{filterConditon.Value}' ");
                        }
                    }
                }
            }
        }
Exemple #5
0
        protected override Expression <Func <Conversation, bool> > Is(string date)
        {
            DateTime value;

            if (!DateTime.TryParse(date, out value))
            {
                throw SocialExceptions.BadRequest("The value of date time is invalid");
            }
            return(t => t.ModifiedTime.HasValue && ((DateTime)t.ModifiedTime.Value).Year == value.Year &&
                   ((DateTime)t.ModifiedTime.Value).Month == value.Month &&
                   ((DateTime)t.ModifiedTime.Value).Day == value.Day);
        }
        public async Task <SocialAccount> AddFacebookPageAsync(SocialAccount socialAccount, string originalId)
        {
            if (IsDupliated(SocialUserSource.Facebook, originalId))
            {
                throw SocialExceptions.BadRequest($"'{socialAccount.SocialUser.Name}' has already been added.");
            }

            await _fbClient.SubscribeApp(originalId, socialAccount.Token);

            var socialUser = _socialUserRepo.FindAll()
                             .Where(t => t.OriginalId == originalId && t.Source == SocialUserSource.Facebook)
                             .OrderByDescending(t => t.Id)
                             .FirstOrDefault();

            if (socialUser == null)
            {
                // create a new integraton account.
                await this.InsertAsync(socialAccount);
            }
            else
            {
                // if the user was a customer or a deleted integration account.
                socialUser.Type          = SocialUserType.IntegrationAccount; // make sure convert a customer to integration account.
                socialUser.IsDeleted     = false;                             // make sure to restore a deleted integration account.
                socialUser.Avatar        = socialAccount.SocialUser.Avatar;
                socialUser.Email         = socialAccount.SocialUser.Email;
                socialUser.OriginalLink  = socialAccount.SocialUser.OriginalLink;
                socialAccount.Id         = socialUser.Id;
                socialAccount.SocialUser = socialUser;
                if (socialUser.SocialAccount == null)
                {
                    socialUser.SocialAccount = socialAccount;
                }
                else
                {
                    socialUser.SocialAccount.Token = socialAccount.Token;
                    socialUser.SocialAccount.FacebookPageCategory = socialAccount.FacebookPageCategory;
                    socialUser.SocialAccount.FacebookSignInAs     = socialAccount.FacebookSignInAs;
                }
                socialUser.SocialAccount.IsDeleted = false;
                socialUser.SocialAccount.IfEnable  = true;
                socialUser.SocialAccount.IfConvertMessageToConversation     = true;
                socialUser.SocialAccount.IfConvertVisitorPostToConversation = true;
                socialUser.SocialAccount.IfConvertWallPostToConversation    = true;
                _socialUserRepo.Update(socialUser);

                await this.InsertSocialAccountInGeneralDb(SocialUserSource.Facebook, originalId);
            }

            CurrentUnitOfWork.SaveChanges();
            return(this.Find(socialAccount.Id));
        }
Exemple #7
0
        public Message ReplyTwitterDirectMessage(int conversationId, string message, bool isCloseConversation = false)
        {
            var twitterService = DependencyResolver.Resolve <ITwitterService>();

            Conversation conversation = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.TwitterDirectMessage)
            {
                throw SocialExceptions.BadRequest("Conversation source must be twitter direct message.");
            }

            _conversationService.CheckIfCanReopenWhenReply(conversation);

            var           messages       = Repository.FindAll().Include(t => t.Sender).Include(t => t.Receiver).Where(t => t.ConversationId == conversation.Id).ToList();
            SocialAccount twitterAccount = GetSocialAccountFromMessages(messages);

            if (twitterAccount == null)
            {
                throw SocialExceptions.BadRequest($"Twitter account has been deleted.");
            }

            var customer = messages.Where(t => t.IsDeleted == false && t.Sender.Type == SocialUserType.Customer)
                           .OrderByDescending(t => t.SendTime).Select(t => t.Sender).FirstOrDefault();

            if (customer == null)
            {
                throw new NotSupportedException($"Cant't find last message from conversation. Conversation Id={conversationId}.");
            }

            IUser prviousTwitterUser = twitterService.GetUser(twitterAccount, long.Parse(customer.OriginalId));

            if (prviousTwitterUser == null)
            {
                throw new NotSupportedException($"Cant't find twitter user. Conversation Id={conversationId}.");
            }

            // publish twitter direct message
            IMessage twitterDirectMessage = twitterService.PublishMessage(twitterAccount, prviousTwitterUser, message);

            // create message to db
            Message directMessage = TwitterConverter.ConvertToMessage(twitterDirectMessage);

            directMessage.ConversationId = conversation.Id;
            directMessage.SenderId       = twitterAccount.Id;
            directMessage.SendAgentId    = UserContext.UserId;
            directMessage.ReceiverId     = customer.Id;

            Save(directMessage, isCloseConversation);

            return(this.Find(directMessage.Id));
        }
Exemple #8
0
        public void CheckFieldIdExist(List <FilterCondition> filterConditons)
        {
            List <int> fieldIds = new List <int>();

            foreach (var filterCondition in filterConditons)
            {
                fieldIds.Add(filterCondition.FieldId);
            }
            fieldIds.RemoveAll(a => _conversationFieldRepo.FindAll().Where(t => fieldIds.Contains(t.Id)).Select(t => t.Id).ToList().Contains(a));
            if (fieldIds.Count != 0)
            {
                throw SocialExceptions.BadRequest($"FieldId '{fieldIds[0]}' not exists");
            }
        }
Exemple #9
0
        public Message ReplyFacebookMessage(int conversationId, string content, bool isCloseConversation = false)
        {
            var conversation = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.FacebookMessage)
            {
                throw SocialExceptions.BadRequest("Conversation source must be facebook message.");
            }

            _conversationService.CheckIfCanReopenWhenReply(conversation);

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

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

            SocialUser recipient = messages.Where(t => t.Sender.Type == SocialUserType.Customer)
                                   .OrderByDescending(t => t.SendTime)
                                   .Select(t => t.Sender).FirstOrDefault();

            // publish message to facebook
            string fbMessageId = _fbClient.PublishMessage(socialAccount.Token, conversation.OriginalId, content);

            if (string.IsNullOrWhiteSpace(fbMessageId))
            {
                return(null);
            }
            // create message
            var message = new Message
            {
                ConversationId = conversation.Id,
                Content        = content,
                SenderId       = socialAccount.Id,
                ReceiverId     = recipient != null ? recipient.Id : socialAccount.Id,
                SendAgentId    = UserContext.UserId,
                SendTime       = DateTime.UtcNow,
                OriginalId     = fbMessageId,
                Source         = MessageSource.FacebookMessage
            };

            Save(message, isCloseConversation);

            return(this.Find(message.Id));
        }
Exemple #10
0
        protected override Expression <Func <Conversation, bool> > Between(string date)
        {
            string[] value = date.Split('|');
            DateTime DateTime1;

            if (!DateTime.TryParse(value[0], out DateTime1))
            {
                throw SocialExceptions.BadRequest("The value of date time is invalid");
            }
            DateTime DateTime2;

            if (!DateTime.TryParse(value[1], out DateTime2))
            {
                throw SocialExceptions.BadRequest("The value of date time is invalid");
            }
            return(t => t.ModifiedTime.HasValue && t.ModifiedTime.Value >= DateTime1 && t.ModifiedTime.Value <= DateTime2);
        }
Exemple #11
0
        public IQueryable <Conversation> ApplyFilter(IQueryable <Conversation> conversations, int?filterId)
        {
            if (filterId != null)
            {
                var filter = _filterRepo.Find(filterId.Value);
                if (filter != null)
                {
                    conversations = ApplyFilter(conversations, filter);
                }
                else
                {
                    throw SocialExceptions.BadRequest($"filterId '{filterId}' not exists.");
                }
            }

            return(conversations);
        }
        public async Task AddTwitterAccountAsync(string authorizationId, string oauthVerifier)
        {
            var user = await _twitterAuthService.ValidateAuthAsync(authorizationId, oauthVerifier);

            if (user != null)
            {
                if (IsDupliated(SocialUserSource.Twitter, user.IdStr))
                {
                    throw SocialExceptions.BadRequest($"'{user.Name}' has already been added.");
                }

                SocialAccount account = new SocialAccount
                {
                    Token       = user.Credentials.AccessToken,
                    TokenSecret = user.Credentials.AccessTokenSecret,
                    IfConvertMessageToConversation = true,
                    IfConvertTweetToConversation   = true,
                    IfEnable = true
                };

                var socialUser = _socialUserRepo.FindAll()
                                 .Where(t => t.OriginalId == user.IdStr && t.Source == SocialUserSource.Twitter)
                                 .OrderByDescending(t => t.Id)
                                 .FirstOrDefault();
                if (socialUser == null)
                {
                    account.SocialUser = new SocialUser
                    {
                        Name         = user.Name,
                        ScreenName   = user.ScreenName,
                        Email        = user.Email,
                        Source       = SocialUserSource.Twitter,
                        Type         = SocialUserType.IntegrationAccount,
                        Avatar       = user.ProfileImageUrl,
                        OriginalId   = user.IdStr,
                        OriginalLink = TwitterHelper.GetUserUrl(user.ScreenName)
                    };

                    await this.InsertAsync(account);
                }
                else
                {
                    // if the user was a customer or a deleted integration account.
                    socialUser.Type       = SocialUserType.IntegrationAccount; // make sure convert a customer to integration account.
                    socialUser.IsDeleted  = false;                             // make sure to restore a deleted integration account.
                    socialUser.Avatar     = user.ProfileImageUrl;
                    socialUser.Name       = user.Name;
                    socialUser.ScreenName = user.ScreenName;
                    socialUser.Email      = user.Email;
                    account.Id            = socialUser.Id;
                    account.SocialUser    = socialUser;
                    if (socialUser.SocialAccount == null)
                    {
                        socialUser.SocialAccount = account;
                    }
                    else
                    {
                        socialUser.SocialAccount.Token       = account.Token;
                        socialUser.SocialAccount.TokenSecret = account.TokenSecret;
                    }
                    socialUser.SocialAccount.IsDeleted = false;
                    socialUser.SocialAccount.IfEnable  = true;
                    socialUser.SocialAccount.IfConvertMessageToConversation = true;
                    socialUser.SocialAccount.IfConvertTweetToConversation   = true;
                    _socialUserRepo.Update(socialUser);

                    await InsertSocialAccountInGeneralDb(SocialUserSource.Twitter, user.IdStr);
                }
            }
        }
Exemple #13
0
        public Message ReplyTwitterTweetMessage(int conversationId, int twitterAccountId, string content, bool isCloseConversation = false)
        {
            var          twitterService = DependencyResolver.Resolve <ITwitterService>();
            Conversation conversation   = _conversationService.CheckIfExists(conversationId);

            if (conversation.Source != ConversationSource.TwitterTweet)
            {
                throw SocialExceptions.BadRequest("Conversation source must be twitter tweet.");
            }

            SocialAccount twitterAccount = _socialAccountService.FindAllWithDeleted().FirstOrDefault(t => t.Id == twitterAccountId);

            if (twitterAccount != null && twitterAccount.IsDeleted)
            {
                twitterAccount = _socialAccountService.FindAll().FirstOrDefault(t => t.SocialUser.OriginalId == twitterAccount.SocialUser.OriginalId);
            }
            if (twitterAccount == null)
            {
                throw SocialExceptions.BadRequest("Twitter account has been deleted.");
            }

            var     messages         = Repository.FindAll().Include(t => t.Sender).Include(t => t.Receiver).Where(t => t.ConversationId == conversation.Id).ToList();
            var     previousMessages = messages.Where(t => t.SenderId != twitterAccountId).OrderByDescending(t => t.SendTime);
            Message replyMessage     = null;

            foreach (var previousMessage in previousMessages)
            {
                if (previousMessage.IsDeleted)
                {
                    continue;
                }
                //if (previousMessage.Sender.Type == SocialUserType.IntegrationAccount)
                //{
                //    continue;
                //}

                ITweet previousTweet = twitterService.GetTweet(twitterAccount, long.Parse(previousMessage.OriginalId));
                if (previousTweet != null && !previousTweet.IsTweetDestroyed)
                {
                    // publish tweet
                    if (!content.Contains("@" + previousTweet.CreatedBy.ScreenName))
                    {
                        content = "@" + previousTweet.CreatedBy.ScreenName + " " + content;
                    }
                    var replyTweet = twitterService.ReplyTweet(twitterAccount, previousTweet, content);
                    if (replyTweet == null)
                    {
                        continue;
                    }

                    // add message
                    replyMessage = TwitterConverter.ConvertToMessage(replyTweet);
                    replyMessage.ConversationId = conversation.Id;
                    replyMessage.SenderId       = twitterAccount.Id;
                    replyMessage.SendAgentId    = UserContext.UserId;
                    replyMessage.ReceiverId     = previousMessage.Sender.Id;

                    Save(replyMessage, isCloseConversation);

                    break;
                }
            }

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

            return(this.Find(replyMessage.Id));
        }
Exemple #14
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));
        }