public async IAsyncEnumerable <List <string> > GetUserToNotifyOnlineAsync(string userId)
        {
            var dialogs = await dialogProvider.GetModelsBySearchPredicate(x => x.User1Id == userId || x.User2Id == userId);

            if (dialogs == null || dialogs.Count == 0)
            {
                yield break;
            }

            foreach (var dialog in dialogs)
            {
                string currentId = dialog.User1Id == userId ? dialog.User2Id : dialog.User1Id;

                var userConnections = await connectionCacheService.GetUserConnectionsAsync(currentId);

                if (userConnections != null && userConnections.Count > 0)
                {
                    List <string> result = new List <string>();
                    foreach (var userConnection in userConnections)
                    {
                        result.Add(userConnection.Key);
                    }

                    yield return(result);
                }
            }
        }
Exemple #2
0
        public async Task <Dictionary <string, string> > GetMessageToSendAsync(string message)
        {
            MessageDto model = JsonConvert.DeserializeObject <MessageDto>(message);

            var messagesToSend = await messangerService.GetMessagesToSendAsync(message);                      // SessionId - key, message - value

            var connections = await connectionCacheService.GetUserConnectionsAsync(model.Message.ReceiverId); // ConnectionId - key, SessionId - value

            Dictionary <string, string> result = new Dictionary <string, string>();                           //ключ - id-conneciton, значение - закриптованное сообщение

            foreach (var messageToSend in messagesToSend)
            {
                string currentConnection = connections?.Values?.FirstOrDefault(x => x == messageToSend.Key) ?? "";

                if (string.IsNullOrEmpty(currentConnection))                 // сейчас юзер с такой сессией находится в оффлайн либо в фоне
                {
                    //brockerService.PublishMessage(messageToSend.Key, messageToSend.Value);
                    continue;
                }

                result.Add(connections.FirstOrDefault(x => x.Value == currentConnection).Key, messageToSend.Value);
            }
            return(result);
        }