public override async Task <Empty> SubscribeToUsersStatusUpdates(ConnectionsUpdatesRequest request, ServerCallContext context) { _logger.LogInformation( "Begin grpc call from method {Method} to subscribe to users status updates {UsersIds} with connectionId {ConnectionId}", context.Method, string.Join(',', request.UsersIds), request.ConnectionId); var verifiedConnection = await _userConnectionService.VerifyConnectionAsync(request.UserId, request.ConnectionId); if (!verifiedConnection) { context.Status = new Status(StatusCode.InvalidArgument, "Invalid connection id."); return(new Empty()); } foreach (var userId in request.UsersIds) { var statusSubscription = new UserStatusSubscription(userId); await _subscriptionService.SubscribeAsync(request.ConnectionId, statusSubscription); var isOnline = await _userConnectionService.HasConnectionsAsync(userId); var method = UserStatusUpdatedMethod.WithArgs(userId, isOnline ? UserStatusUpdatedMethod.UserStatus.Online : UserStatusUpdatedMethod.UserStatus.Offline); await _publisherService.PublishAsync(request.ConnectionId, method); } return(new Empty()); }
public async Task Handle(ConnectionUpdatedIntegrationEvent @event) { var connectionSubscription = new ConnectionSubscription(@event.UserId, @event.OtherId); var connectionMethod = ConnectionUpdatedMethod.WithArgs(@event.OtherId, @event.Notes, (int)@event.Relationship); await _publisherService.PublishAsync(connectionSubscription, connectionMethod); var statusSubscription = new UserStatusSubscription(@event.OtherId); var connections = await _connectionService.GetConnectionsAsync(@event.UserId); foreach (var connection in connections) { if (@event.Relationship == ConnectionUpdatedIntegrationEvent.RelationshipEnum.Friend) { await _subscriptionService.SubscribeAsync(connection, statusSubscription); var isOnline = await _connectionService.HasConnectionsAsync(@event.OtherId); var statusMethod = UserStatusUpdatedMethod.WithArgs(@event.OtherId, isOnline ? UserStatus.Online : UserStatus.Offline); await _publisherService.PublishAsync(connection, statusMethod); } else { await _subscriptionService.UnsubscribeAsync(connection, statusSubscription); var statusMethod = UserStatusUpdatedMethod.WithArgs(@event.OtherId, UserStatus.Unknown); await _publisherService.PublishAsync(connection, statusMethod); } } }
public async Task RemoveConnectionAsync(string userId, string connectionId) { await _database.KeyDeleteAsync(GetConnectionKey(connectionId)); await _database.SetRemoveAsync(GetUserKey(userId), connectionId); if (!await HasConnectionsAsync(userId)) { var subscription = new UserStatusSubscription(userId); var method = UserStatusUpdatedMethod.WithArgs(userId, UserStatus.Offline); await _publisherService.PublishAsync(subscription, method); } }
public async Task AddConnectionAsync(string userId, string connectionId) { await _database.StringSetAsync(GetConnectionKey(connectionId), userId); await _database.SetAddAsync(GetUserKey(userId), connectionId); var connectionsCount = await _database.SetLengthAsync(GetUserKey(userId)); if (connectionsCount == 1) { var subscription = new UserStatusSubscription(userId); var method = UserStatusUpdatedMethod.WithArgs(userId, UserStatus.Online); await _publisherService.PublishAsync(subscription, method); } }