public static Chat ToChatDto(
     this ConversationDataModel value,
     string userId)
 {
     return(new Chat
     {
         Name = value.Name,
         Id = value.Id,
         DialogueUser = value.IsGroup ? null : value.GetDialogUser(userId)?.ToAppUserDto(),
         IsGroup = value.IsGroup,
         ThumbnailUrl = value.ThumbnailUrl,
         FullImageUrl = value.FullImageUrl,
         Participants = value.participants?.Select(x => x.ToAppUserDto()).ToList(),
         AuthKeyId = value.AuthKeyId,
         IsSecure = value.IsSecure,
         PublicKey = value.PublicKey?.ToDhPublicKey(),
         DeviceId = value.DeviceId,
         ChatRole = value.Role?.ToChatRole(),
         ClientLastMessageId = value.LastMessage?.MessageID ?? 0,
         LastMessage = value.LastMessage?.ToMessage(),
         IsPublic = value.IsPublic,
         IsMessagingRestricted = value.IsMessagingRestricted,
         MessagesUnread = value.UnreadCount
     });
 }
        public static int UrgentWordScoreRule(this ConversationDataModel conversation, string urgentWordMatch)
        {
            int urgentCount = 0;

            foreach (string line in conversation.Content)
            {
                urgentCount += line.ToLowerInvariant().Split(' ').Where(l => l.StartsWith(urgentWordMatch.ToLowerInvariant())).Count();
            }
            return(urgentCount <= 2 && urgentCount >= 1 ? -5 : urgentCount > 2 ? -10 : 0);
        }
        private async Task <Shared.DTO.Conversations.Chat> CreateGroup(
            AppUser creator,
            string chatImageUrl,
            string chatName,
            bool isPublic)
        {
            if (chatName.Length > MaxNameLength)
            {
                throw new InvalidDataException("Chat name was too long.");
            }

            try
            {
                var imageUrl = chatImageUrl ?? chatDataProvider.GetGroupPictureUrl();

                var newChat = new ConversationDataModel
                {
                    IsGroup      = true,
                    Name         = chatName,
                    FullImageUrl = imageUrl,
                    ThumbnailUrl = imageUrl,
                    IsPublic     = isPublic
                };

                await conversationRepository.AddAsync(newChat);

                await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create(
                                                                creator.Id,
                                                                newChat));

                var role = await rolesRepository.AddAsync(
                    ChatRoleDataModel.Create(newChat, creator.Id, ChatRole.Creator));

                newChat.Roles = new List <ChatRoleDataModel>()
                {
                    role
                };
                newChat.participants = new List <AppUser> {
                    creator
                };

                await unitOfWork.Commit();

                return(newChat.ToChatDto(creator.Id));
            }
            catch (Exception e)
            {
                throw new InvalidDataException("Couldn't create group because of unexpected error.", e);
            }
        }
示例#4
0
        private void CalculateDurationScore(ConversationDataModel conversation)
        {
            var functionToExecute = new Func <bool>(() =>
            {
                conversation.Scores.Add(DURATIONSCORE, conversation.DurationScoreRule());
                return(true);
            });
            var functionToExecuteInCaseOfError = new Func <Exception, bool>((ex) =>
            {
                throw new ScoreCalculatorServiceException($"CalculateDurationScore => {ex.Message}", ex.InnerException);
            }
                                                                            );

            TryToPerformAction(functionToExecute, functionToExecuteInCaseOfError);
        }
示例#5
0
        private void CalculateGoodServiceScore(ConversationDataModel conversation, List <string> goodServiceSentences, string excellentMatch)
        {
            var functionToExecute = new Func <bool>(() =>
            {
                conversation.Scores.Add(GOODSERVICESCORE, conversation.GoodServiceScoreRule(goodServiceSentences, excellentMatch));
                return(true);
            });
            var functionToExecuteInCaseOfError = new Func <Exception, bool>((ex) =>
            {
                throw new ScoreCalculatorServiceException($"CalculateGoodServiceScore => {ex.Message}", ex.InnerException);
            }
                                                                            );

            TryToPerformAction(functionToExecute, functionToExecuteInCaseOfError);
        }
示例#6
0
        private void CalculateUrgentWordScore(ConversationDataModel conversation, string urgentMatchWord)
        {
            var functionToExecute = new Func <bool>(() =>
            {
                conversation.Scores.Add(URGENTWORDSCORE, conversation.UrgentWordScoreRule(urgentMatchWord));
                return(true);
            });
            var functionToExecuteInCaseOfError = new Func <Exception, bool>((ex) =>
            {
                throw new ScoreCalculatorServiceException($"CalculateUrgentWordScore => {ex.Message}", ex.InnerException);
            }
                                                                            );

            TryToPerformAction(functionToExecute, functionToExecuteInCaseOfError);
        }
示例#7
0
        private void CalculateTotalScore(ConversationDataModel conversation)
        {
            var functionToExecute = new Func <bool>(() =>
            {
                conversation.TotalScore = conversation.Scores.Sum(x => x.Value);
                return(true);
            });
            var functionToExecuteInCaseOfError = new Func <Exception, bool>((ex) =>
            {
                throw new ScoreCalculatorServiceException($"CalculateTotalScore => {ex.Message}", ex.InnerException);
            }
                                                                            );

            TryToPerformAction(functionToExecute, functionToExecuteInCaseOfError);
        }
        public static int GoodServiceScoreRule(this ConversationDataModel conversation, List <string> goodServiceList, string excellentMatch)
        {
            int goodWordsMatches = 0;

            foreach (var item in conversation.Content)
            {
                foreach (var service in goodServiceList)
                {
                    var serviceCout = item.Split(new string[] { service }, StringSplitOptions.None).Count();
                    goodWordsMatches = goodWordsMatches == 0 && serviceCout > 1 ? 10 : goodWordsMatches;
                }
                if (item.Contains(excellentMatch))
                {
                    goodWordsMatches = goodWordsMatches + 100;
                    break;
                }
            }
            return(goodWordsMatches);
        }
        private async Task <Shared.DTO.Conversations.Chat> CreateDialog(
            AppUser creator,
            string dialogUserId,
            bool isSecure,
            string deviceId)
        {
            var user = await usersRepository.GetByIdAsync(creator.Id);

            if (user == null)
            {
                throw new KeyNotFoundException("Wrong creatorId.");
            }

            try
            {
                //if this is a dialogue , find a user with whom to create chat
                var secondDialogueUser = await usersRepository.GetByIdAsync(dialogUserId);

                if (secondDialogueUser == null)
                {
                    throw new InvalidDataException("Wrong dialog user Id.");
                }

                DhPublicKeyDataModel dhPublicKey = null;

                if (isSecure)
                {
                    dhPublicKey = await publicKeys.GetRandomKey();
                }

                var newChat = new ConversationDataModel
                {
                    IsGroup     = false,
                    IsSecure    = isSecure,
                    PublicKeyId = dhPublicKey?.Id,
                    PublicKey   = dhPublicKey,
                    DeviceId    = deviceId
                };

                await conversationRepository.AddAsync(newChat);

                await usersConversationsRepository.AddAsync(
                    UsersConversationDataModel.Create(dialogUserId, newChat)
                    );

                await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create(
                                                                creator.Id,
                                                                newChat,
                                                                deviceId));

                newChat.participants = new List <AppUser> {
                    user, secondDialogueUser
                };

                await unitOfWork.Commit();

                return(newChat.ToChatDto(creator.Id));
            }
            catch (Exception e)
            {
                throw new InvalidDataException("Couldn't create dialog. Probably there exists one already.", e);
            }
        }
 public static int LinesScoreRule(this ConversationDataModel conversation) => conversation.LinesCount <= 5 && conversation.LinesCount > 1 ? 20 : conversation.LinesCount == 1 ? -100 : conversation.LinesCount > 5 ? 10 : 0;
 public static int DurationScoreRule(this ConversationDataModel conversation) => conversation.Duration < 1 ? 50 : 25;
 public ChatQueryModel(ConversationDataModel baseModel)
 {
     _baseModel = baseModel;
 }