-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
User
subscriptions without event senders
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/Tgstation.Server.Host/GraphQL/Subscriptions/UserSubscriptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using HotChocolate; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Subscriptions; | ||
using HotChocolate.Types; | ||
|
||
using Tgstation.Server.Api.Rights; | ||
using Tgstation.Server.Host.GraphQL.Types; | ||
using Tgstation.Server.Host.Models.Transformers; | ||
using Tgstation.Server.Host.Security; | ||
|
||
namespace Tgstation.Server.Host.GraphQL.Subscriptions | ||
{ | ||
/// <summary> | ||
/// Subscriptions for <see cref="User"/>. | ||
/// </summary> | ||
[ExtendObjectType(typeof(Subscription))] | ||
public sealed class UserSubscriptions | ||
{ | ||
/// <summary> | ||
/// The name of the topic for when any user is updated. | ||
/// </summary> | ||
const string UserUpdatedTopic = "UserUpdated"; | ||
|
||
/// <summary> | ||
/// Get the names of the topics to send to when a <see cref="Models.User"/> is updated. | ||
/// </summary> | ||
/// <param name="userId">The <see cref="Api.Models.EntityId.Id"/> of the updated <see cref="Models.User"/>.</param> | ||
/// <returns>An <see cref="IEnumerable{T}"/> of topic <see cref="string"/>s.</returns> | ||
public static IEnumerable<string> UserUpdatedTopics(long userId) | ||
{ | ||
yield return UserUpdatedTopic; | ||
yield return SpecificUserUpdatedTopic(userId); | ||
} | ||
|
||
/// <summary> | ||
/// The name of the topic for when a specific <see cref="Models.User"/> is updated. | ||
/// </summary> | ||
/// <param name="userId">The <see cref="Api.Models.EntityId.Id"/> of the updated <see cref="Models.User"/>.</param> | ||
/// <returns>The topic <see cref="string"/>.</returns> | ||
static string SpecificUserUpdatedTopic(long userId) | ||
=> $"{UserUpdatedTopic}.{userId}"; | ||
|
||
/// <summary> | ||
/// Receive an update for all <see cref="User"/> changes. | ||
/// </summary> | ||
/// <param name="user">The <see cref="Models.User"/> received from the publisher.</param> | ||
/// <returns>The updated <see cref="User"/>.</returns> | ||
[Subscribe] | ||
[TgsGraphQLAuthorize(AdministrationRights.ReadUsers)] | ||
public User UserUpdated([EventMessage] Models.User user) | ||
{ | ||
ArgumentNullException.ThrowIfNull(user); | ||
|
||
return ((Models.IApiTransformable<Models.User, User, UserGraphQLTransformer>)user).ToApi(); | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="ISourceStream"/> for <see cref="CurrentUserUpdated(Models.User)"/>. | ||
/// </summary> | ||
/// <param name="receiver">The <see cref="ITopicEventReceiver"/>.</param> | ||
/// <param name="authenticationContext">The <see cref="IAuthenticationContext"/> for the request.</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param> | ||
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in a <see cref="ISourceStream{TMessage}"/> of the <see cref="SessionInvalidationReason"/> for the <paramref name="authenticationContext"/>.</returns> | ||
public ValueTask<ISourceStream<Models.User>> CurrentUserUpdatedStream( | ||
[Service] ITopicEventReceiver receiver, | ||
[Service] IAuthenticationContext authenticationContext, | ||
CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(receiver); | ||
ArgumentNullException.ThrowIfNull(authenticationContext); | ||
return receiver.SubscribeAsync<Models.User>(SpecificUserUpdatedTopic(Models.ModelExtensions.Require(authenticationContext.User, user => user.Id)), cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Receive an update to the logged in <see cref="User"/> when it is changed. | ||
/// </summary> | ||
/// <param name="user">The <see cref="Models.User"/> received from the publisher.</param> | ||
/// <returns>The updated <see cref="User"/>.</returns> | ||
[Subscribe] | ||
[TgsGraphQLAuthorize] | ||
public User CurrentUserUpdated([EventMessage] Models.User user) | ||
{ | ||
ArgumentNullException.ThrowIfNull(user); | ||
|
||
return ((Models.IApiTransformable<Models.User, User, UserGraphQLTransformer>)user).ToApi(); | ||
} | ||
} | ||
} |