Exemple #1
0
        private void RemoveUserFromAssociatedChatsInGroup(Models.User user, Guid groupId)
        {
            IEnumerable <ChatUser> chatUsers = _chatUserRepository.GetAllByUserIdAndGroupId(user.Id, groupId);

            if (chatUsers == null || !chatUsers.Any())
            {
                return;
            }

            var group = _groupRepository.Get(groupId);

            foreach (var chatUser in chatUsers)
            {
                var chatId      = chatUser.ChatId;
                var usersInChat = _chatUserRepository.CountByChatId(chatId);

                if (usersInChat <= 3)
                {
                    _chatService.RemoveChatByChatId(chatId);
                }
                else // remove only one chatUser
                {
                    _chatUserRepository.Remove(chatUser);
                    _chatUserStore.RemoveByUserIdAndChatId(user.Id, chatId);
                    _chatEventService.AddUserRemovedFromChatEvent(group, _authorizedUser, user, chatUser.Chat);
                }
            }
        }
Exemple #2
0
        public void EscapeChat(string chatGuid, string userGuid, int mode, string selectedUserGuid)
        {
            var userEr = _userRepository.Get(userGuid);

            if (!userEr.Success)
            {
                Clients.Caller.onEscapeChatCaller(
                    JsonConvert.SerializeObject(new { error = true, message = UserErrorMessage }));
                return;
            }
            var user = userEr.Value;

            var chatElasticResult = _chatRepository.Get(chatGuid);

            if (!chatElasticResult.Success)
            {
                Clients.Caller.onEscapeChatCaller(
                    JsonConvert.SerializeObject(new { error = true, message = ChatErrorMessage }));
                return;
            }
            var chat = chatElasticResult.Value;

            var chatUserEr = _chatUserRepository.Remove(chatGuid, userGuid);

            if (!chatUserEr.Success)
            {
                Clients.Caller.onEscapeChatCaller(
                    JsonConvert.SerializeObject(new { error = true, message = chatUserEr.Message }));
                return;
            }

            foreach (var connectionId in user.ConnectionIds)
            {
                Groups.Remove(connectionId, chatGuid);
            }

            var messageEr = _messageRepository.AddAdminMessage(chatGuid, user,
                                                               string.Format(UserEscapeChatFormatString, user.UserName));

            if (!messageEr.Success)
            {
                Clients.Caller.onEscapeChatCaller(
                    JsonConvert.SerializeObject(new { error = true, message = messageEr.Message }));
                return;
            }

            var message = messageEr.Value;

            Clients.Group(chatGuid).onSendMessageOthers(JsonConvert.SerializeObject(new { message, chatGuid }));

            if (mode == 0)
            {
                var selectedUserEr = _userRepository.Get(selectedUserGuid);
                if (!selectedUserEr.Success)
                {
                    Clients.Caller.onEscapeChatCaller(
                        JsonConvert.SerializeObject(new { error = true, message = selectedUserEr.Message }));
                    return;
                }

                chat.AdminGuid = selectedUserGuid;

                // Update chat administrator
                var chatAdminEr = _chatRepository.Update(chat);
                if (!chatAdminEr.Success)
                {
                    Clients.Caller.onEscapeChatCaller(
                        JsonConvert.SerializeObject(new { error = true, message = chatAdminEr.Message }));
                    return;
                }

                messageEr = _messageRepository.AddAdminMessage(chatGuid, user,
                                                               string.Format(NewChatAdminFormatString, selectedUserEr.Value.UserName));
                if (!messageEr.Success)
                {
                    Clients.Caller.onEscapeChatCaller(
                        JsonConvert.SerializeObject(new { error = true, message = messageEr.Message }));
                    return;
                }

                message = messageEr.Value;
                Clients.Group(chatGuid).onSendMessageOthers(JsonConvert.SerializeObject(new { message, chatGuid }));
                Clients.Group(chatGuid).onEscapeChatOthers(JsonConvert.SerializeObject(chat));
            }

            Clients.Caller.onEscapeChatCaller(JsonConvert.SerializeObject(chat));
        }