public async Task HandleAsync()
        {
            try
            {
                var chatUsers = await updateChatsService.EditChatUsersAsync(notice.ChatUsers, notice.ChatId, notice.RequestedUserId).ConfigureAwait(false);

                var bannedUsers = chatUsers.Where(opt => opt.Banned == true);
                if (!bannedUsers.IsNullOrEmpty())
                {
                    foreach (var user in bannedUsers)
                    {
                        var systemMessageInfo = SystemMessageInfoFactory.CreateUserBannedMessageInfo(user.UserId);
                        var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo);

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }
                var deletedUsers = chatUsers.Where(opt => opt.Deleted == true);
                if (!deletedUsers.IsNullOrEmpty())
                {
                    foreach (var user in deletedUsers)
                    {
                        var systemMessageInfo = SystemMessageInfoFactory.CreateUserRemovedMessageInfo(user.UserId);
                        var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo);

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }
            }
            catch (UserIsNotInConversationException)
            {
                ChatVm newChat = await nodeRequestSender.GetFullChatInformationAsync(notice.ChatId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(newChat).ConfigureAwait(false);
            }
            catch (ConversationNotFoundException ex)
            {
                ChatVm newChat = await nodeRequestSender.GetFullChatInformationAsync(ex.ConversationId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(newChat).ConfigureAwait(false);
            }
            conversationsNoticeService.SendChangeChatUsersNoticeAsync(notice.ChatUsers, notice.ChatId);
            ChatVm chat = await loadChatsService.GetChatByIdAsync(notice.ChatId).ConfigureAwait(false);

            BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChangeUsersChatSegmetAsync(
                notice.ChatUsers,
                notice.ChatId,
                notice.RequestedUserId,
                node.Node.Id,
                chat.Type,
                NodeData.Instance.NodeKeys.SignPrivateKey,
                NodeData.Instance.NodeKeys.SymmetricKey,
                NodeData.Instance.NodeKeys.Password,
                NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

            BlockGenerationHelper.Instance.AddSegment(segment);
        }
Example #2
0
        public async Task <Response> CreateResponseAsync()
        {
            if (!await conversationsService.IsUserInConversationAsync(request.ConversationType, request.ConversationId, clientConnection.UserId.Value))
            {
                return(new ResultResponse(request.RequestId, "The user is not in conversation.", ErrorCode.PermissionDenied));
            }
            var nodesIds = await conversationsService.GetConversationNodesIdsAsync(request.ConversationType, request.ConversationId).ConfigureAwait(false);

            if (nodesIds.Count > 1)
            {
                long?dialogUserId = null;
                if (request.ConversationType == ConversationType.Dialog)
                {
                    var users = await loadDialogsService.GetDialogUsersAsync(request.ConversationId).ConfigureAwait(false);

                    dialogUserId = users.FirstOrDefault(user => user.Id != clientConnection.UserId).Id;
                }
                nodeNoticeService.SendConverationActionNodeNoticeAsync(clientConnection.UserId.Value, dialogUserId, request.ConversationId, request.ConversationType, request.Action, nodesIds);
            }
            if (request.Action != ConversationAction.Screenshot)
            {
                conversationsNoticeService.SendConversationActionNoticeAsync(clientConnection.UserId.Value, request.ConversationType, request.ConversationId, request.Action);
            }
            if (request.ConversationType != ConversationType.Channel && request.Action == ConversationAction.Screenshot)
            {
                var systemMessageInfo = SystemMessageInfoFactory.CreateScreenshotMessageInfo(clientConnection.UserId.Value);
                var message           = await systemMessagesService.CreateMessageAsync(request.ConversationType, request.ConversationId, systemMessageInfo).ConfigureAwait(false);

                conversationsNoticeService.SendSystemMessageNoticeAsync(message);
            }
            return(new ResultResponse(request.RequestId));
        }
        public async Task HandleAsync()
        {
            foreach (var chat in notice.Chats)
            {
                var editableChat = await loadChatsService.GetChatByIdAsync(chat.Id.Value).ConfigureAwait(false);

                var editedChat = await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);

                if (editableChat.Name != editedChat.Name)
                {
                    var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChat.Name, editedChat.Name);
                    var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, editedChat.Id.Value, systemMessageInfo);

                    conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                }
                List <BlockSegmentVm> segments =
                    await BlockSegmentsService.Instance.CreateEditPrivateChatSegmentsAsync(
                        editedChat,
                        nodeConnection.Node.Id,
                        NodeData.Instance.NodeKeys.SignPrivateKey,
                        NodeData.Instance.NodeKeys.SymmetricKey,
                        NodeData.Instance.NodeKeys.Password,
                        NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                if (segments.Any())
                {
                    nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList());
                    BlockGenerationHelper.Instance.AddSegments(segments);
                }
                editedChat.Users = null;
                conversationsNoticeService.SendEditChatNoticeAsync(editedChat, null);
                UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(await loadChatsService.GetChatUsersIdAsync(editedChat.Id.GetValueOrDefault()).ConfigureAwait(false));
            }
        }
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                ChannelVm editableChannel = await loadChannelsService.GetChannelByIdAsync(request.Channel.ChannelId.Value);

                ChannelVm channel = await updateChannelsService.EditChannelAsync(request.Channel, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false);

                if (editableChannel.ChannelName != channel.ChannelName)
                {
                    var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChannel.ChannelName, channel.ChannelName);
                    var message           = await systemMessagesService.CreateMessageAsync(ObjectsLibrary.Enums.ConversationType.Channel, channel.ChannelId.Value, systemMessageInfo);

                    conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                }
                IEnumerable <long> usersId = await loadChannelsService.GetChannelUsersIdAsync(channel.ChannelId.GetValueOrDefault()).ConfigureAwait(false);

                conversationsNoticeService.SendChannelNoticeAsync(channel, usersId.ToList(), clientConnection);
                UsersConversationsCacheService.Instance.UpdateUsersChannelsAsync(usersId.ToList());
                nodeNoticeService.SendChannelNodeNoticeAsync(channel, clientConnection.UserId.GetValueOrDefault(), null);
                BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChannelSegmentAsync(channel, NodeSettings.Configs.Node.Id).ConfigureAwait(false);

                BlockGenerationHelper.Instance.AddSegment(segment);
                return(new ChannelsResponse(request.RequestId, channel));
            }
            catch (PermissionDeniedException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Channel not found or user does not have access to the channel.", ObjectsLibrary.Enums.ErrorCode.PermissionDenied));
            }
        }
Example #5
0
        public async Task HandleAsync()
        {
            if (!await conversationsService.IsUserInConversationAsync(notice.ConversationType, notice.ConversationId, notice.UserId) && notice.DialogUserId == null)
            {
                return;
            }
            if (notice.ConversationType == ConversationType.Dialog)
            {
                var dialogs = await loadDialogsService.GetUsersDialogsAsync(notice.DialogUserId.Value, notice.UserId).ConfigureAwait(false);

                var senderDialog = dialogs.FirstOrDefault(opt => opt.FirstUserId == notice.UserId);
                if (notice.Action != ConversationAction.Screenshot)
                {
                    conversationsNoticeService.SendConversationActionNoticeAsync(notice.UserId, ConversationType.Dialog, senderDialog.Id, notice.Action);
                }
            }
            else
            {
                if (notice.Action != ConversationAction.Screenshot)
                {
                    conversationsNoticeService.SendConversationActionNoticeAsync(notice.UserId, notice.ConversationType, notice.ConversationId, notice.Action);
                }
            }
            if (notice.ConversationType != ConversationType.Channel && notice.Action == ConversationAction.Screenshot)
            {
                var systemMessageInfo = SystemMessageInfoFactory.CreateScreenshotMessageInfo(notice.UserId);
                var message           = await systemMessagesService.CreateMessageAsync(notice.ConversationType, notice.ConversationId, systemMessageInfo).ConfigureAwait(false);

                conversationsNoticeService.SendSystemMessageNoticeAsync(message);
            }
        }
Example #6
0
        public async Task <Response> CreateResponseAsync()
        {
            List <ChatVm> result = new List <ChatVm>();

            try
            {
                foreach (var chat in request.Chats)
                {
                    ChatVm editableChat = await loadChatsService.GetChatByIdAsync(chat.Id);

                    ChatVm editedChat =
                        await updateChatsService.EditChatAsync(chat, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false);

                    result.Add(editedChat);
                    if (editableChat.Name != editedChat.Name)
                    {
                        var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChat.Name, editedChat.Name);
                        var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, editedChat.Id.Value, systemMessageInfo);

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                    List <BlockSegmentVm> segments = await BlockSegmentsService.Instance.CreateEditPrivateChatSegmentsAsync(
                        editedChat,
                        NodeSettings.Configs.Node.Id,
                        NodeData.Instance.NodeKeys.SignPrivateKey,
                        NodeData.Instance.NodeKeys.SymmetricKey,
                        NodeData.Instance.NodeKeys.Password,
                        NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                    if (segments.Any())
                    {
                        nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList());
                        foreach (var segment in segments)
                        {
                            BlockGenerationHelper.Instance.AddSegment(segment);
                        }
                    }
                    editedChat.Users = null;
                    conversationsNoticeService.SendEditChatNoticeAsync(editedChat, clientConnection);
                    IEnumerable <long> chatUsersId = await loadChatsService.GetChatUsersIdAsync(chat.Id).ConfigureAwait(false);

                    UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(chatUsersId);
                }

                nodeNoticeService.SendEditChatsNodeNoticeAsync(result);
                return(new ChatsResponse(request.RequestId, result));
            }
            catch (PermissionDeniedException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Chat not found or user does not have access to chat.", ErrorCode.PermissionDenied));
            }
        }
        public async Task HandleAsync()
        {
            var editableChannel = await loadChannelsService.GetChannelByIdAsync(notice.Channel.ChannelId.Value).ConfigureAwait(false);

            ChannelVm channel = await createChannelsService.CreateOrEditChannelAsync(notice.Channel, notice.RequestorId, notice.Subscribers).ConfigureAwait(false);

            if (editableChannel?.ChannelName == channel.ChannelName)
            {
                var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChannel.ChannelName, channel.ChannelName);
                var message           = await systemMessagesService.CreateMessageAsync(ObjectsLibrary.Enums.ConversationType.Channel, channel.ChannelId.Value, systemMessageInfo);

                conversationsNoticeService.SendSystemMessageNoticeAsync(message);
            }
            IEnumerable <long> usersId = await loadChannelsService.GetChannelUsersIdAsync(channel.ChannelId.GetValueOrDefault()).ConfigureAwait(false);

            conversationsNoticeService.SendChannelNoticeAsync(channel, usersId.ToList());
        }
        public async Task HandleAsync()
        {
            ChatVm chat;

            try
            {
                chat = await updateChatsService.AddUsersToChatAsync(notice.UsersId, notice.ChatId, notice.RequestorId).ConfigureAwait(false);
            }
            catch (AddUserChatException)
            {
                chat = await nodeRequestSender.GetFullChatInformationAsync(notice.ChatId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);
            }
            catch (ConversationNotFoundException ex)
            {
                chat = await nodeRequestSender.GetFullChatInformationAsync(ex.ConversationId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);

                UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(notice.UsersId);
            }
            foreach (var userId in notice.UsersId)
            {
                var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, chat.Id.Value, SystemMessageInfoFactory.CreateUserAddedMessageInfo(userId));

                conversationsNoticeService.SendSystemMessageNoticeAsync(message);
            }
            conversationsNoticeService.SendNewUsersAddedToChatNoticeAsync(chat, null);
            BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateAddUsersChatSegmentAsync(
                notice.UsersId,
                notice.ChatId,
                notice.RequestorId,
                node.Node.Id,
                chat.Type,
                NodeData.Instance.NodeKeys.SignPrivateKey,
                NodeData.Instance.NodeKeys.SymmetricKey,
                NodeData.Instance.NodeKeys.Password,
                NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

            BlockGenerationHelper.Instance.AddSegment(segment);
        }
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                List <ChatUserVm> chatUsers = await updateChatsService.EditChatUsersAsync(
                    request.ChatUsers, request.ChatId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false);

                var bannedUsers = chatUsers.Where(opt => opt.Banned == true);
                if (!bannedUsers.IsNullOrEmpty())
                {
                    foreach (var user in bannedUsers)
                    {
                        var systemMessageInfo = SystemMessageInfoFactory.CreateUserBannedMessageInfo(user.UserId);
                        var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo);

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }
                var deletedUsers = chatUsers.Where(opt => opt.Deleted == true);
                if (!deletedUsers.IsNullOrEmpty())
                {
                    foreach (var user in deletedUsers)
                    {
                        var systemMessageInfo = SystemMessageInfoFactory.CreateUserRemovedMessageInfo(user.UserId);
                        var message           = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo);

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }

                ChatVm chat = await loadChatsService.GetChatByIdAsync(request.ChatId).ConfigureAwait(false);

                BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChangeUsersChatSegmetAsync(
                    chatUsers,
                    request.ChatId,
                    clientConnection.UserId.GetValueOrDefault(),
                    NodeSettings.Configs.Node.Id,
                    chat.Type,
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    NodeData.Instance.NodeKeys.SymmetricKey,
                    NodeData.Instance.NodeKeys.Password,
                    NodeData.Instance.NodeKeys.KeyId)
                                         .ConfigureAwait(false);

                BlockGenerationHelper.Instance.AddSegment(segment);
                nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> {
                    segment
                });
                conversationsNoticeService.SendChangeChatUsersNoticeAsync(chatUsers, request.ChatId, clientConnection);
                nodeNoticeService.SendChangeChatUsersNodeNoticeAsync(chatUsers.ToList(), request.ChatId, clientConnection.UserId.GetValueOrDefault(), chat);
                UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(chatUsers.Select(opt => opt.UserId));
                return(new ChatUsersResponse(request.RequestId, chatUsers));
            }
            catch (ChatUserBlockedException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User blocked in chat.", ErrorCode.UserBlocked));
            }
            catch (UserIsNotInConversationException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User is not in chat.", ErrorCode.UserIsNotInChat));
            }
            catch (EditConversationUsersException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User does not access to the chat.", ErrorCode.PermissionDenied));
            }
            catch (ConversationNotFoundException ex)
            {
                Logger.WriteLog(ex);
                return(new ResultResponse(request.RequestId, "Chat not found.", ErrorCode.ObjectDoesNotExists));
            }
        }
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                List <ChatUserVm> chatUsers    = new List <ChatUserVm>();
                List <ChatVm>     updatedChats = new List <ChatVm>();
                foreach (long chatId in request.ChatsId)
                {
                    var chat = await loadChatsService.GetChatByIdAsync(chatId).ConfigureAwait(false);

                    if (chat != null && !(chat.Users?.Any(opt => opt.UserRole == UserRole.Creator) ?? false))
                    {
                        var nodeId         = chat.NodesId.FirstOrDefault(id => id != NodeSettings.Configs.Node.Id);
                        var nodeConnection = connectionsService.GetNodeConnection(nodeId);
                        if (nodeConnection != null)
                        {
                            await nodeRequestSender.GetFullChatInformationAsync(chatId, nodeConnection).ConfigureAwait(false);
                        }
                    }
                    updatedChats.Add(await updateChatsService.AddUsersToChatAsync(request.UsersId, chatId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false));
                    foreach (var userId in request.UsersId)
                    {
                        var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, chatId, SystemMessageInfoFactory.CreateUserAddedMessageInfo(userId));

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }
                foreach (ChatVm chat in updatedChats)
                {
                    conversationsNoticeService.SendNewUsersAddedToChatNoticeAsync(chat, clientConnection);
                    if (chat.Users != null)
                    {
                        chatUsers.AddRange(chat.Users);
                        BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateAddUsersChatSegmentAsync(
                            chat.Users.Select(opt => opt.UserId).ToList(),
                            chat.Id.GetValueOrDefault(),
                            clientConnection.UserId.GetValueOrDefault(),
                            NodeSettings.Configs.Node.Id,
                            chat.Type,
                            NodeData.Instance.NodeKeys.SignPrivateKey,
                            NodeData.Instance.NodeKeys.SymmetricKey,
                            NodeData.Instance.NodeKeys.Password,
                            NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                        BlockGenerationHelper.Instance.AddSegment(segment);
                        nodeNoticeService.SendAddUsersToChatNodeNoticeAsync(chat, clientConnection.UserId.GetValueOrDefault());
                        if (chat.Type == ChatType.Private)
                        {
                            nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> {
                                segment
                            });
                        }
                    }
                }
                return(new ChatUsersResponse(request.RequestId, chatUsers));
            }
            catch (AddUserChatException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Failed to add user to chat.", ErrorCode.AddUserProblem));
            }
            catch (ConversationNotFoundException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Chat not found.", ErrorCode.ChatIsNotValid));
            }
            catch (ConversationIsNotValidException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Chat is deleted.", ErrorCode.ChatIsNotValid));
            }
            catch (UserBlockedException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User is blocked in chat.", ErrorCode.UserBlocked));
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Internal server error.", ErrorCode.UnknownError));
            }
        }