public async void SendNewMessageNoticeToChatUsersAsync(MessageDto newMessage, ClientConnection connection, bool sendPush = true)
        {
            try
            {
                List <ChatUserVm> chatUsers = await loadChatsService.GetChatUsersAsync(newMessage.ConversationId, null).ConfigureAwait(false);

                if (sendPush)
                {
                    pushNotificationsService.SendMessageNotificationAsync(
                        newMessage,
                        chatUsers.Select(opt => new NotificationUser(opt.UserId, opt.IsMuted.Value)).ToList());
                }
                List <Task> noticeTasks = new List <Task>();
                foreach (var chatUser in chatUsers)
                {
                    var clientConnections = connectionsService.GetUserClientConnections(chatUser.UserId);
                    if (clientConnections != null)
                    {
                        noticeTasks.Add(Task.Run(async() =>
                        {
                            NewMessageNotice notice = new NewMessageNotice(
                                MessageConverter.GetMessageVm(newMessage, chatUser.UserId),
                                MarkdownHelper.ContainsMarkdownUserCalling(newMessage.Text, chatUser.UserId));
                            await SendNoticeToClientsAsync(clientConnections.Where(clientConnection => clientConnection != connection), notice).ConfigureAwait(false);
                        }));
                    }
                }
                await Task.WhenAll(noticeTasks).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
예제 #2
0
        public async Task HandleAsync()
        {
            ConversationsUsersNodeResponse response;
            List <ChatUserVm>    chatsUsers    = new List <ChatUserVm>();
            List <ChannelUserVm> channelsUsers = new List <ChannelUserVm>();

            foreach (long id in nodeRequest.ConversationsIds)
            {
                switch (nodeRequest.ConversationType)
                {
                case ObjectsLibrary.Enums.ConversationType.Chat:
                {
                    chatsUsers.AddRange(await loadChatsService.GetChatUsersAsync(id, null, int.MaxValue).ConfigureAwait(false));
                }
                break;

                case ObjectsLibrary.Enums.ConversationType.Channel:
                {
                    channelsUsers.AddRange(await loadChannelsService.GetChannelUsersAsync(id, null, null).ConfigureAwait(false));
                }
                break;
                }
            }
            response = new ConversationsUsersNodeResponse(nodeRequest.RequestId, chatsUsers, channelsUsers);
            await NodeWebSocketCommunicationManager.SendResponseAsync(response, nodeConnection).ConfigureAwait(false);
        }
        private async Task <IEnumerable <MessageDto> > GetChatMessagesAsync()
        {
            List <MessageDto> messages        = new List <MessageDto>();
            ChatUserDto       currentChatUser = (await loadChatsService.GetChatUsersAsync(new long[] { clientConnection.UserId.GetValueOrDefault() }, request.ConversationId).ConfigureAwait(false)).FirstOrDefault();

            if (request.MessagesId == null)
            {
                messages = await loadMessagesService.GetChatMessagesAsync(
                    request.ConversationId,
                    clientConnection.UserId.GetValueOrDefault(),
                    request.NavigationMessageId,
                    request.AttachmentsTypes,
                    request.Direction.GetValueOrDefault(true),
                    LIMIT).ConfigureAwait(false);
            }
            else
            {
                messages = await loadMessagesService.GetMessagesByIdAsync(
                    request.MessagesId,
                    ConversationType.Chat,
                    request.ConversationId,
                    clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false);
            }
            if (currentChatUser.LastReadedGlobalMessageId != null)
            {
                var lastMessage = (await loadMessagesService.GetMessagesByIdAsync(
                                       new List <Guid> {
                    currentChatUser.LastReadedGlobalMessageId.GetValueOrDefault()
                },
                                       ConversationType.Chat,
                                       request.ConversationId,
                                       clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false)).FirstOrDefault();
                if (lastMessage != null)
                {
                    messages.ForEach(message =>
                    {
                        if (message.SenderId != clientConnection.UserId.GetValueOrDefault())
                        {
                            if (message.SendingTime > lastMessage.SendingTime)
                            {
                                message.Read = false;
                            }
                            else
                            {
                                message.Read = true;
                            }
                        }
                    });
                }
            }
            return(messages);
        }
예제 #4
0
        public async Task HandleAsync()
        {
            try
            {
                List <ChatUserDto> chatUsers = await loadChatsService.GetChatUsersAsync(request.UsersId, request.ChatId).ConfigureAwait(false);

                ChatUsersNodeResponse response = new ChatUsersNodeResponse(request.RequestId, ChatUserConverter.GetChatUsersVm(chatUsers));
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.UnknownError), current);
            }
        }
예제 #5
0
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                List <ChatUserVm> chatUsers = await loadChatsService.GetChatUsersAsync(
                    request.ChatId,
                    clientConnection.UserId.GetValueOrDefault(),
                    100,
                    request.NavigationUserId.GetValueOrDefault()).ConfigureAwait(false);

                if (!chatUsers.Any(user => user.UserRole == UserRole.Creator))
                {
                    var nodesIds = await loadChatsService.GetChatNodeListAsync(request.ChatId).ConfigureAwait(false);

                    var nodeConnection = connectionsService.GetNodeConnection(nodesIds.FirstOrDefault(id => id != NodeSettings.Configs.Node.Id));
                    if (nodeConnection != null)
                    {
                        var chat = await nodeRequestSender.GetFullChatInformationAsync(request.ChatId, nodeConnection).ConfigureAwait(false);

                        if (chat != null)
                        {
                            await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);

                            chatUsers = chat.Users?.Take(100).ToList();
                        }
                    }
                }
                chatUsers.ForEach(item =>
                {
                    if (item.UserInfo != null)
                    {
                        item.UserInfo = privacyService.ApplyPrivacySettings(
                            item.UserInfo,
                            item.UserInfo.Privacy);
                    }
                });
                return(new ChatUsersResponse(request.RequestId, chatUsers));
            }
            catch (GetUsersException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User does not have access to the chat.", ErrorCode.PermissionDenied));
            }
        }