Пример #1
0
        public override async Task <Empty> SubscribeToUsersUpdates(ConnectionsUpdatesRequest request,
                                                                   ServerCallContext context)
        {
            _logger.LogInformation(
                "Begin grpc call from method {Method} to subscribe to users 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 userSubscription = new UserSubscription(userId);
                await _subscriptionService.SubscribeAsync(request.ConnectionId, userSubscription);

                var relationshipSubscription = new ConnectionSubscription(request.UserId, userId);
                await _subscriptionService.SubscribeAsync(request.ConnectionId, relationshipSubscription);
            }

            return(new Empty());
        }
Пример #2
0
        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());
        }
Пример #3
0
        public async Task SubscribeToUsersStatusUpdatesAsync(string userId, string connectionId, params string[] usersIds)
        {
            await GrpcCallerService.CallService(_urls.GrpcNotifications, async channel =>
            {
                var client = new NotificationsGrpc.NotificationsGrpcClient(channel);

                var request = new ConnectionsUpdatesRequest
                {
                    UserId       = userId,
                    ConnectionId = connectionId
                };
                request.UsersIds.AddRange(usersIds);

                await client.SubscribeToUsersStatusUpdatesAsync(request);
            });
        }