private async Task OnEntityChangedAsync(ExtendedEntityChanged entityChanged, CancellationToken cancellationToken = default(CancellationToken)) { if (entityChanged == null) { return; } if (UserTypeName == entityChanged.Type) { // It's pointless to send a user added message to the new user. if (entityChanged.ChangeType == ChangeType.Added) { _logger.Trace(() => $"Ignoring {UserTypeName} message for added user: {entityChanged.Id}."); return; } var userConnectionIds = await _connectionMapping.GetUserIdConnectionsAsync(entityChanged.Id); _logger.Trace(() => $"Sending {UserTypeName} message to user: {entityChanged.Id} (to {userConnectionIds.Count} connections)"); foreach (string connectionId in userConnectionIds) { await Context.Connection.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>("UserId"); if (userId != null) { var userConnectionIds = await _connectionMapping.GetUserIdConnectionsAsync(userId); _logger.Trace(() => $"Sending {TokenTypeName} message for added user: {userId} (to {userConnectionIds.Count} connections)"); foreach (string connectionId in userConnectionIds) { await Context.Connection.TypedSendAsync(connectionId, entityChanged); } return; } if (entityChanged.Data.GetValueOrDefault <bool>("IsAuthenticationToken")) { _logger.Trace(() => $"Ignoring {TokenTypeName} Authentication Token message: {entityChanged.Id}."); return; } entityChanged.Data.Clear(); } if (!String.IsNullOrEmpty(entityChanged.OrganizationId)) { _logger.Trace(() => $"Sending {entityChanged.Type} message to organization: {entityChanged.OrganizationId})"); await GroupSendAsync(entityChanged.OrganizationId, entityChanged); } }
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(ExtendedEntityChanged entityChanged, CancellationToken cancellationToken = default(CancellationToken)) { if (entityChanged == null) { return; } if (entityChanged.Type == typeof(User).Name) { foreach (var connectionId in _userIdConnections.GetConnections(entityChanged.Id)) { await Context.Connection.TypedSend(connectionId, entityChanged); } return; } if (!String.IsNullOrEmpty(entityChanged.OrganizationId)) { await Context.Groups.TypedSend(entityChanged.OrganizationId, entityChanged); } }
private async Task OnEntityChangedAsync(ExtendedEntityChanged entityChanged, CancellationToken cancellationToken = default(CancellationToken)) { if (entityChanged == null) { return; } if (entityChanged.Type == typeof(User).Name) { foreach (var connectionId in await _connectionMapping.GetConnectionsAsync(entityChanged.Id)) { await GroupSendAsync(connectionId, entityChanged); } return; } if (!String.IsNullOrEmpty(entityChanged.OrganizationId)) { await GroupSendAsync(entityChanged.OrganizationId, entityChanged); } }
public static ExtendedEntityChanged Create(EntityChanged entityChanged, bool removeWhenSettingProperties = true) { var model = new ExtendedEntityChanged { Id = entityChanged.Id, Type = entityChanged.Type, ChangeType = entityChanged.ChangeType, Data = entityChanged.Data }; if (model.Data.TryGetValue(KnownKeys.OrganizationId, out object organizationId)) { model.OrganizationId = organizationId.ToString(); if (removeWhenSettingProperties) { model.Data.Remove(KnownKeys.OrganizationId); } } if (model.Data.TryGetValue(KnownKeys.ProjectId, out object projectId)) { model.ProjectId = projectId.ToString(); if (removeWhenSettingProperties) { model.Data.Remove(KnownKeys.ProjectId); } } if (model.Data.TryGetValue(KnownKeys.StackId, out object stackId)) { model.StackId = stackId.ToString(); if (removeWhenSettingProperties) { model.Data.Remove(KnownKeys.StackId); } } return(model); }