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);
            }
        }
        private async Task SendChannelMessageNotificationAsyhc(MessageDto message, List <NotificationUser> notificationsUsers)
        {
            try
            {
                var administratorsChannel = await loadChannelsService.GetAdministrationChannelUsersAsync(message.ConversationId).ConfigureAwait(false);

                IEnumerable <TokenVm> tokens = await tokensService.GetAllUsersTokensAsync(
                    notificationsUsers.Where(user => user.UserId != message.SenderId).Select(user => user.UserId)).ConfigureAwait(false);

                foreach (var user in notificationsUsers)
                {
                    bool           isCall      = MarkdownHelper.ContainsMarkdownUserCalling(message.Text, user.UserId);
                    var            userTokens  = GetOfflineUserTokens(tokens.ToList(), user.UserId);
                    List <TokenVm> adminTokens = tokens.Where(token => administratorsChannel.Any(admin => admin.UserId == token.UserId))?.ToList();
                    if (adminTokens != null && adminTokens.Any())
                    {
                        adminTokens = GetOfflineUserTokens(adminTokens, user.UserId);
                        var messageVm = MessageConverter.GetMessageVm(message, user.UserId);
                        if (messageVm.Attachments?.Any(attach => attach.Type == AttachmentType.EncryptedMessage) ?? false)
                        {
                            messageVm.Attachments = null;
                        }

                        if (adminTokens != null && (!user.IsMuted || isCall))
                        {
                            await SendNotificationRequestAsync(
                                adminTokens.Select(opt => opt.DeviceTokenId),
                                new NewMessageNotice(messageVm, isCall))
                            .ConfigureAwait(false);
                        }
                    }
                    else if (!user.IsMuted || isCall)
                    {
                        MessageVm subMessage = MessageConverter.GetMessageVm(message, user.UserId);
                        subMessage.SenderId = null;
                        await SendNotificationRequestAsync(
                            userTokens.Select(opt => opt.DeviceTokenId),
                            new NewMessageNotice(subMessage, isCall))
                        .ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
        public async void SendNewMessageNoticeToChannelUsersAsync(MessageDto newMessage, ClientConnection clientConnection = null, bool sendPush = true)
        {
            try
            {
                var channelUsers = await loadChannelsService.GetChannelUsersAsync(newMessage.ConversationId, null, null).ConfigureAwait(false);

                if (sendPush)
                {
                    pushNotificationsService.SendMessageNotificationAsync(
                        newMessage,
                        channelUsers.Select(opt => new NotificationUser(opt.UserId, opt.IsMuted.Value)).ToList());
                }
                var administrationUsers = await loadChannelsService.GetAdministrationChannelUsersAsync(newMessage.ConversationId).ConfigureAwait(false);

                foreach (var channelUser in channelUsers)
                {
                    var clientConnections = connectionsService.GetUserClientConnections(channelUser.UserId);
                    if (clientConnections != null)
                    {
                        clientConnections = clientConnections.Where(opt => opt != clientConnection).ToList();
                        NewMessageNotice notice;
                        if (channelUser.UserId == newMessage.SenderId || administrationUsers.Any(opt => opt.UserId == channelUser.UserId))
                        {
                            notice = new NewMessageNotice(
                                MessageConverter.GetMessageVm(newMessage, channelUser.UserId),
                                MarkdownHelper.ContainsMarkdownUserCalling(newMessage.Text, channelUser.UserId));
                        }
                        else
                        {
                            MessageVm tempMessage = new MessageVm(MessageConverter.GetMessageVm(newMessage, channelUser.UserId))
                            {
                                SenderId = null
                            };
                            notice = new NewMessageNotice(
                                tempMessage,
                                MarkdownHelper.ContainsMarkdownUserCalling(tempMessage.Text, channelUser.UserId));
                        }
                        await SendNoticeToClientsAsync(clientConnections, notice).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
        private async Task SendChatMessageNotificationAsync(MessageDto message, List <NotificationUser> notificationsUsers)
        {
            try
            {
                IEnumerable <TokenVm> tokens = await tokensService.GetAllUsersTokensAsync(
                    notificationsUsers.Where(option => option.UserId != message.SenderId).Select(option => option.UserId),
                    true).ConfigureAwait(false);

                List <Task> noticeTasks = new List <Task>();
                foreach (var user in notificationsUsers)
                {
                    noticeTasks.Add(Task.Run(async() =>
                    {
                        var userTokens = GetOfflineUserTokens(tokens.ToList(), user.UserId);
                        var messageVm  = MessageConverter.GetMessageVm(message, user.UserId);
                        if (messageVm.Attachments?.Any(attach => attach.Type == AttachmentType.EncryptedMessage) ?? false)
                        {
                            messageVm.Attachments = null;
                        }

                        bool isCall = MarkdownHelper.ContainsMarkdownUserCalling(message.Text, user.UserId);
                        if (isCall || !user.IsMuted)
                        {
                            await SendNotificationRequestAsync(
                                userTokens.Select(opt => opt.DeviceTokenId),
                                new NewMessageNotice(messageVm, isCall)).ConfigureAwait(false);
                        }
                    }));
                }
                await Task.WhenAll(noticeTasks).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
        private async Task SendDialogMessageNotificationAsync(NotificationUser notificationUser, MessageDto message)
        {
            try
            {
                List <TokenVm> tokens = await tokensService.GetAllUsersTokensAsync(new List <long> {
                    notificationUser.UserId
                }).ConfigureAwait(false);

                var clientConnections = connectionsService.GetUserClientConnections(notificationUser.UserId);
                if (clientConnections != null)
                {
                    tokens = GetOfflineUserTokens(tokens, notificationUser.UserId);
                }
                if (tokens.Any())
                {
                    var messageVm = MessageConverter.GetMessageVm(message, notificationUser.UserId);
                    if (messageVm.Attachments?.Any(attach => attach.Type == AttachmentType.EncryptedMessage) ?? false)
                    {
                        messageVm.Attachments = null;
                    }

                    bool isCall = MarkdownHelper.ContainsMarkdownUserCalling(message.Text, notificationUser.UserId);
                    if (!notificationUser.IsMuted || isCall)
                    {
                        await SendNotificationRequestAsync(
                            tokens.Select(opt => opt.DeviceTokenId),
                            new NewMessageNotice(messageVm, isCall))
                        .ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
        public async void SendNewMessageNoticeToDialogUsers(IEnumerable <MessageDto> messages, ClientConnection senderClientConnection, long receiverId, bool saveMessageFlag = true)
        {
            try
            {
                long senderId = messages.FirstOrDefault().SenderId.GetValueOrDefault();
                var  dialogs  = await loadDialogsService.GetUsersDialogsAsync(senderId, receiverId);

                var    currentDialog = dialogs.FirstOrDefault(dial => dial.FirstUserId == receiverId);
                Notice senderNotice  = default;
                if (senderId != receiverId)
                {
                    var message = messages.FirstOrDefault(mess => mess.ConversationId != currentDialog.Id);
                    senderNotice = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, senderId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                }
                else
                {
                    var message = messages.FirstOrDefault();
                    senderNotice = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, senderId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                }
                var senderClients = connectionsService.GetUserClientConnections(messages.ElementAt(0).SenderId.GetValueOrDefault());
                if (senderClients != null)
                {
                    IEnumerable <ClientConnection> senderConnectionsExceptCurrent = senderClients.Where(connection => !Equals(senderClientConnection, connection));
                    await SendNoticeToClientsAsync(senderConnectionsExceptCurrent, senderNotice).ConfigureAwait(false);
                }
                if (messages.Count() == 2)
                {
                    var    message         = messages.FirstOrDefault(mess => mess.ConversationId == currentDialog.Id);
                    var    receiverClients = connectionsService.GetUserClientConnections(receiverId);
                    Notice receiverNotice  = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, receiverId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                    if (receiverClients != null && receiverClients.Any())
                    {
                        await SendNoticeToClientsAsync(receiverClients, receiverNotice).ConfigureAwait(false);
                    }
                    else
                    {
                        var receiver = await loadUsersService.GetUserAsync(receiverId).ConfigureAwait(false);

                        if (receiver.NodeId == NodeSettings.Configs.Node.Id && !saveMessageFlag)
                        {
                            await pendingMessagesService.AddUserPendingMessageAsync(receiverId, receiverNotice, message.GlobalId).ConfigureAwait(false);
                        }
                    }
                }
                DialogDto dialog = await loadDialogsService.GetDialogAsync(currentDialog.Id).ConfigureAwait(false);

                pushNotificationsService.SendMessageNotificationAsync(
                    messages.FirstOrDefault(opt => opt.ConversationId == currentDialog.Id),
                    new List <NotificationUser> {
                    new NotificationUser(dialog.FirstUserId, dialog.IsMuted)
                });
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }