-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountDisconnectService.cs
More file actions
73 lines (65 loc) · 2.94 KB
/
Copy pathAccountDisconnectService.cs
File metadata and controls
73 lines (65 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using RustPlusBot.Abstractions.Credentials;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Domain.Credentials;
using RustPlusBot.Features.Pairing.Supervisor;
using RustPlusBot.Persistence.Credentials;
using RustPlusBot.Persistence.Servers;
namespace RustPlusBot.Features.Pairing.Accounts;
/// <summary>Default <see cref="IAccountDisconnectService"/>.</summary>
/// <param name="supervisor">Stops the user's FCM listener.</param>
/// <param name="registrations">The FCM registration store.</param>
/// <param name="credentials">The player-credential pool store.</param>
/// <param name="servers">Resolves server names for the preview.</param>
/// <param name="eventBus">Publishes ServerCredentialsChangedEvent per affected server.</param>
internal sealed class AccountDisconnectService(
IPairingSupervisor supervisor,
IFcmRegistrationStore registrations,
ICredentialStore credentials,
IServerService servers,
IEventBus eventBus) : IAccountDisconnectService
{
/// <inheritdoc />
public async Task<AccountDisconnectPreview> PreviewAsync(
ulong guildId,
ulong ownerUserId,
CancellationToken cancellationToken = default)
{
var registration = await registrations.GetAsync(guildId, ownerUserId, cancellationToken).ConfigureAwait(false);
var serverIds = await credentials.ListServerIdsForOwnerAsync(guildId, ownerUserId, cancellationToken)
.ConfigureAwait(false);
var names = new List<string>(serverIds.Count);
foreach (var serverId in serverIds)
{
var server = await servers.GetAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (server is not null)
{
names.Add(server.Name);
}
}
var isConnected = (registration is not null && registration.Status != FcmRegistrationStatus.Disabled)
|| serverIds.Count > 0;
return new AccountDisconnectPreview(isConnected, names);
}
/// <inheritdoc />
public async Task<int> DisconnectAsync(
ulong guildId,
ulong ownerUserId,
CancellationToken cancellationToken = default)
{
await supervisor.StopListenerAsync(guildId, ownerUserId).ConfigureAwait(false);
var registration = await registrations.GetAsync(guildId, ownerUserId, cancellationToken).ConfigureAwait(false);
if (registration is not null)
{
await registrations.SetStatusAsync(registration.Id, FcmRegistrationStatus.Disabled, cancellationToken)
.ConfigureAwait(false);
}
var affected = await credentials.RemoveForOwnerAsync(guildId, ownerUserId, cancellationToken)
.ConfigureAwait(false);
foreach (var serverId in affected)
{
await eventBus.PublishAsync(new ServerCredentialsChangedEvent(guildId, serverId), cancellationToken)
.ConfigureAwait(false);
}
return affected.Count;
}
}