tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
UserSubscriptions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Threading;
4using System.Threading.Tasks;
5
6using HotChocolate;
7using HotChocolate.Execution;
8using HotChocolate.Types;
9using HotChocolate.Types.Relay;
10
13
15{
19 [ExtendObjectType(typeof(Subscription))]
20 [GraphQLDescription(Subscription.GraphQLDescription)]
21 public sealed class UserSubscriptions
22 {
26 const string UserUpdatedTopic = "UserUpdated";
27
33 public static IEnumerable<string> UserUpdatedTopics(long userId)
34 {
35 yield return UserUpdatedTopic;
36 yield return SpecificUserUpdatedTopic(userId);
37 }
38
44 static string SpecificUserUpdatedTopic(long userId)
45 => $"{UserUpdatedTopic}.{userId}";
46
54 public ValueTask<ISourceStream<User>> UserUpdatedStream(
55 [ID(nameof(User))] long? userId,
56 [Service] ITopicEventReceiver receiver,
57 CancellationToken cancellationToken)
58 {
59 ArgumentNullException.ThrowIfNull(receiver);
60 var topic = userId.HasValue ? SpecificUserUpdatedTopic(userId.Value) : UserUpdatedTopic;
61 return receiver.SubscribeAsync<User>(topic, cancellationToken);
62 }
63
69 [Subscribe(With = nameof(UserUpdatedStream))]
70 public User UserUpdated([EventMessage] User user)
71 {
72 ArgumentNullException.ThrowIfNull(user);
73 return user;
74 }
75
83 public ValueTask<ISourceStream<User>> CurrentUserUpdatedStream(
84 [Service] ITopicEventReceiver receiver,
85 [Service] IAuthenticationContext authenticationContext,
86 CancellationToken cancellationToken)
87 {
88 ArgumentNullException.ThrowIfNull(receiver);
89 ArgumentNullException.ThrowIfNull(authenticationContext);
90 return receiver.SubscribeAsync<User>(SpecificUserUpdatedTopic(Models.ModelExtensions.Require(authenticationContext.User, user => user.Id)), cancellationToken);
91 }
92
98 [Subscribe(With = nameof(CurrentUserUpdatedStream))]
99 public User CurrentUserUpdated([EventMessage] User user)
100 {
101 ArgumentNullException.ThrowIfNull(user);
102
103 return user;
104 }
105 }
106}
Root type for GraphQL subscriptions.
const string GraphQLDescription
Description to show on the Subscription type.
static string SpecificUserUpdatedTopic(long userId)
The name of the topic for when a specific Models.User is updated.
static IEnumerable< string > UserUpdatedTopics(long userId)
Get the names of the topics to send to when a Models.User is updated.
ValueTask< ISourceStream< User > > CurrentUserUpdatedStream([Service] ITopicEventReceiver receiver, [Service] IAuthenticationContext authenticationContext, CancellationToken cancellationToken)
ISourceStream for CurrentUserUpdated(User).
ValueTask< ISourceStream< User > > UserUpdatedStream([ID(nameof(User))] long? userId, [Service] ITopicEventReceiver receiver, CancellationToken cancellationToken)
ISourceStream for UserUpdated(User).
User CurrentUserUpdated([EventMessage] User user)
Receive an update to the logged in User when it is changed.
User UserUpdated([EventMessage] User user)
Receive an update for all User changes.
const string UserUpdatedTopic
The name of the topic for when any user is updated.
A user registered in the server.
Definition User.cs:22
Implementation of HotChocolate.Subscriptions.ITopicEventReceiver that works around the global::System...
For creating and accessing authentication contexts.