private Task <int> GetNumberOfListeners(EntityChanged message) { var entityChanged = ExtendedEntityChanged.Create(message, false); if (String.IsNullOrEmpty(entityChanged.OrganizationId)) { return(Task.FromResult(1)); // Return 1 as we have no idea if people are listening. } return(_connectionMapping.GetGroupConnectionCountAsync(entityChanged.OrganizationId)); }
private async Task OnEntityChangedAsync(EntityChanged ec, CancellationToken cancellationToken = default) { if (ec == null) { return; } var entityChanged = ExtendedEntityChanged.Create(ec); if (UserTypeName == entityChanged.Type) { // It's pointless to send a user added message to the new user. if (entityChanged.ChangeType == ChangeType.Added) { _logger.LogTrace("Ignoring {UserTypeName} message for added user: {user}.", UserTypeName, entityChanged.Id); return; } var userConnectionIds = await _connectionMapping.GetUserIdConnectionsAsync(entityChanged.Id); _logger.LogTrace("Sending {UserTypeName} message to user: {user} (to {UserConnectionCount} connections)", UserTypeName, entityChanged.Id, userConnectionIds.Count); foreach (string connectionId in userConnectionIds) { await TypedSendAsync(connectionId, entityChanged); } return; } // Only allow specific token messages to be sent down to the client. if (TokenTypeName == entityChanged.Type) { string userId = entityChanged.Data.GetValueOrDefault <string>(ExtendedEntityChanged.KnownKeys.UserId); if (userId != null) { var userConnectionIds = await _connectionMapping.GetUserIdConnectionsAsync(userId); _logger.LogTrace("Sending {TokenTypeName} message for added user: {user} (to {UserConnectionCount} connections)", TokenTypeName, userId, userConnectionIds.Count); foreach (string connectionId in userConnectionIds) { await TypedSendAsync(connectionId, entityChanged); } return; } if (entityChanged.Data.GetValueOrDefault <bool>(ExtendedEntityChanged.KnownKeys.IsAuthenticationToken)) { _logger.LogTrace("Ignoring {TokenTypeName} Authentication Token message: {user}.", TokenTypeName, entityChanged.Id); return; } entityChanged.Data.Clear(); } if (!String.IsNullOrEmpty(entityChanged.OrganizationId)) { _logger.LogTrace("Sending {MessageType} message to organization: {organization}", entityChanged.Type, entityChanged.OrganizationId); await GroupSendAsync(entityChanged.OrganizationId, entityChanged); } }