From 89012bd072a25cffd594c2810f299dd3e83a7fb1 Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 19 Jul 2022 18:42:44 -0400 Subject: [PATCH] feat: add delete account and invite permissions to plugin --- client/ExtraChat/Client.cs | 41 +++++++++++++++++++ client/ExtraChat/Configuration.cs | 1 + client/ExtraChat/ExtraChat.csproj | 14 +++---- .../Formatters/RequestKindFormatter.cs | 18 +++++++- .../Formatters/ResponseKindFormatter.cs | 8 ++++ .../ExtraChat/Protocol/AllowInvitesRequest.cs | 17 ++++++++ client/ExtraChat/Protocol/AnnounceResponse.cs | 1 + .../ExtraChat/Protocol/AuthenticateRequest.cs | 3 ++ .../Protocol/DeleteAccountRequest.cs | 8 ++++ .../Protocol/DeleteAccountResponse.cs | 8 ++++ client/ExtraChat/Protocol/RequestKind.cs | 6 +++ client/ExtraChat/Protocol/ResponseKind.cs | 6 +++ client/ExtraChat/Ui/PluginUi.cs | 17 ++++++++ 13 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 client/ExtraChat/Protocol/AllowInvitesRequest.cs create mode 100644 client/ExtraChat/Protocol/DeleteAccountRequest.cs create mode 100644 client/ExtraChat/Protocol/DeleteAccountResponse.cs diff --git a/client/ExtraChat/Client.cs b/client/ExtraChat/Client.cs index b5635f1..4bc36e1 100644 --- a/client/ExtraChat/Client.cs +++ b/client/ExtraChat/Client.cs @@ -289,6 +289,27 @@ internal class Client : IDisposable { } } + internal async Task DeleteAccount() { + var response = await this.QueueMessageAndWait(new RequestKind.DeleteAccount(new DeleteAccountRequest())); + return response switch { + ResponseKind.Error { Response.Error: var error } => error, + ResponseKind.DeleteAccount => null, + _ => throw new Exception("Unexpected response"), + }; + } + + internal async Task DeleteAccountToast() { + var message = await this.DeleteAccount(); + if (message != null) { + this.Plugin.ShowError($"Could not delete account: {message}"); + return; + } + + this.Plugin.Config.Configs.Remove(this.Plugin.ClientState.LocalContentId); + this.Plugin.SaveConfig(); + this.StopLoop(); + } + /// /// Attempts to register the user after the challenge has been completed. /// @@ -337,6 +358,7 @@ internal class Client : IDisposable { var response = await this.QueueMessageAndWait(new RequestKind.Authenticate(new AuthenticateRequest { Key = key, PublicKey = this.KeyPair.GetPublicKey(), + AllowInvites = true, })); var success = response switch { @@ -488,6 +510,21 @@ internal class Client : IDisposable { })); } + internal async Task AllowInvites(bool allow) { + var resp = await this.QueueMessageAndWait(new RequestKind.AllowInvites(new AllowInvitesRequest { + Allowed = allow, + })); + + + return resp is ResponseKind.AllowInvites { Response.Allowed: var respAllowed } && respAllowed == allow; + } + + internal async Task AllowInvitesToast(bool allow) { + if (!await this.AllowInvites(allow)) { + this.Plugin.ShowError("Could not set invite permissions."); + } + } + private bool _up; #pragma warning disable CS4014 @@ -503,6 +540,10 @@ internal class Client : IDisposable { return; } + this.Channels.Clear(); + this.InvitedChannels.Clear(); + this.ChannelRanks.Clear(); + this.Waiters.Clear(); this.ToSend = System.Threading.Channels.Channel.CreateUnbounded<(RequestContainer, ChannelWriter>?)>(); await this._waitersSemaphore.WaitAsync(); try { diff --git a/client/ExtraChat/Configuration.cs b/client/ExtraChat/Configuration.cs index e18e9d3..901b925 100644 --- a/client/ExtraChat/Configuration.cs +++ b/client/ExtraChat/Configuration.cs @@ -42,6 +42,7 @@ internal class ConfigInfo { public Dictionary ChannelMarkers = new(); public Dictionary ChannelChannels = new(); public int TutorialStep; + public bool AllowInvites = true; internal string GetName(Guid id) => this.Channels.TryGetValue(id, out var channel) ? channel.Name diff --git a/client/ExtraChat/ExtraChat.csproj b/client/ExtraChat/ExtraChat.csproj index 03b6772..c0d6ac9 100644 --- a/client/ExtraChat/ExtraChat.csproj +++ b/client/ExtraChat/ExtraChat.csproj @@ -53,16 +53,16 @@ - - - - - - + + + + + + - + diff --git a/client/ExtraChat/Formatters/RequestKindFormatter.cs b/client/ExtraChat/Formatters/RequestKindFormatter.cs index 2628766..c97a064 100644 --- a/client/ExtraChat/Formatters/RequestKindFormatter.cs +++ b/client/ExtraChat/Formatters/RequestKindFormatter.cs @@ -25,6 +25,8 @@ public class RequestKindFormatter : IMessagePackFormatter { RequestKind.Promote => "promote", RequestKind.Update => "update", RequestKind.Version => "version", + RequestKind.DeleteAccount => "delete_account", + RequestKind.AllowInvites => "allow_invites", _ => throw new ArgumentOutOfRangeException(nameof(value)), }; @@ -82,6 +84,12 @@ public class RequestKindFormatter : IMessagePackFormatter { case RequestKind.Version version: options.Resolver.GetFormatterWithVerify().Serialize(ref writer, version.Request, options); break; + case RequestKind.DeleteAccount deleteAccount: + options.Resolver.GetFormatterWithVerify().Serialize(ref writer, deleteAccount.Request, options); + break; + case RequestKind.AllowInvites allowInvites: + options.Resolver.GetFormatterWithVerify().Serialize(ref writer, allowInvites.Request, options); + break; } } @@ -94,7 +102,7 @@ public class RequestKindFormatter : IMessagePackFormatter { switch (key) { case "ping": { - var request = MessagePackSerializer.Deserialize(ref reader, options); + var request = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); return new RequestKind.Ping(request); } case "authenticate": { @@ -161,6 +169,14 @@ public class RequestKindFormatter : IMessagePackFormatter { var request = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); return new RequestKind.Version(request); } + case "delete_account": { + var request = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); + return new RequestKind.DeleteAccount(request); + } + case "allow_invites": { + var request = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); + return new RequestKind.AllowInvites(request); + } default: throw new MessagePackSerializationException("Invalid RequestKind"); } diff --git a/client/ExtraChat/Formatters/ResponseKindFormatter.cs b/client/ExtraChat/Formatters/ResponseKindFormatter.cs index 542e2ea..765ded5 100644 --- a/client/ExtraChat/Formatters/ResponseKindFormatter.cs +++ b/client/ExtraChat/Formatters/ResponseKindFormatter.cs @@ -106,6 +106,14 @@ public class ResponseKindFormatter : IMessagePackFormatter { var response = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); return new ResponseKind.Announce(response); } + case "delete_account": { + var response = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); + return new ResponseKind.DeleteAccount(response); + } + case "allow_invites": { + var response = options.Resolver.GetFormatterWithVerify().Deserialize(ref reader, options); + return new ResponseKind.AllowInvites(response); + } default: throw new MessagePackSerializationException("Invalid ResponseKind"); } diff --git a/client/ExtraChat/Protocol/AllowInvitesRequest.cs b/client/ExtraChat/Protocol/AllowInvitesRequest.cs new file mode 100644 index 0000000..aa0a8a2 --- /dev/null +++ b/client/ExtraChat/Protocol/AllowInvitesRequest.cs @@ -0,0 +1,17 @@ +using MessagePack; + +namespace ExtraChat.Protocol; + +[Serializable] +[MessagePackObject] +public class AllowInvitesRequest { + [Key(0)] + public bool Allowed; +} + +[Serializable] +[MessagePackObject] +public class AllowInvitesResponse { + [Key(0)] + public bool Allowed; +} diff --git a/client/ExtraChat/Protocol/AnnounceResponse.cs b/client/ExtraChat/Protocol/AnnounceResponse.cs index eedbde8..8791e12 100644 --- a/client/ExtraChat/Protocol/AnnounceResponse.cs +++ b/client/ExtraChat/Protocol/AnnounceResponse.cs @@ -2,6 +2,7 @@ using MessagePack; namespace ExtraChat.Protocol; +[Serializable] [MessagePackObject] public class AnnounceResponse { [Key(0)] diff --git a/client/ExtraChat/Protocol/AuthenticateRequest.cs b/client/ExtraChat/Protocol/AuthenticateRequest.cs index 542d28d..59cf5fb 100644 --- a/client/ExtraChat/Protocol/AuthenticateRequest.cs +++ b/client/ExtraChat/Protocol/AuthenticateRequest.cs @@ -10,4 +10,7 @@ public class AuthenticateRequest { [Key(1)] public byte[] PublicKey; + + [Key(2)] + public bool AllowInvites; } diff --git a/client/ExtraChat/Protocol/DeleteAccountRequest.cs b/client/ExtraChat/Protocol/DeleteAccountRequest.cs new file mode 100644 index 0000000..05fc06e --- /dev/null +++ b/client/ExtraChat/Protocol/DeleteAccountRequest.cs @@ -0,0 +1,8 @@ +using MessagePack; + +namespace ExtraChat.Protocol; + +[Serializable] +[MessagePackObject] +public class DeleteAccountRequest { +} diff --git a/client/ExtraChat/Protocol/DeleteAccountResponse.cs b/client/ExtraChat/Protocol/DeleteAccountResponse.cs new file mode 100644 index 0000000..6926683 --- /dev/null +++ b/client/ExtraChat/Protocol/DeleteAccountResponse.cs @@ -0,0 +1,8 @@ +using MessagePack; + +namespace ExtraChat.Protocol; + +[Serializable] +[MessagePackObject] +public class DeleteAccountResponse { +} diff --git a/client/ExtraChat/Protocol/RequestKind.cs b/client/ExtraChat/Protocol/RequestKind.cs index 378c93b..24f4928 100644 --- a/client/ExtraChat/Protocol/RequestKind.cs +++ b/client/ExtraChat/Protocol/RequestKind.cs @@ -57,4 +57,10 @@ public abstract record RequestKind { [MessagePackObject] public record Version(VersionRequest Request) : RequestKind; + + [MessagePackObject] + public record DeleteAccount(DeleteAccountRequest Request) : RequestKind; + + [MessagePackObject] + public record AllowInvites(AllowInvitesRequest Request) : RequestKind; } diff --git a/client/ExtraChat/Protocol/ResponseKind.cs b/client/ExtraChat/Protocol/ResponseKind.cs index 5bceade..5c6a6a5 100644 --- a/client/ExtraChat/Protocol/ResponseKind.cs +++ b/client/ExtraChat/Protocol/ResponseKind.cs @@ -72,4 +72,10 @@ public abstract record ResponseKind { [MessagePackObject] public record Announce(AnnounceResponse Response) : ResponseKind; + + [MessagePackObject] + public record DeleteAccount(DeleteAccountResponse Response) : ResponseKind; + + [MessagePackObject] + public record AllowInvites(AllowInvitesResponse Response) : ResponseKind; } diff --git a/client/ExtraChat/Ui/PluginUi.cs b/client/ExtraChat/Ui/PluginUi.cs index b5c41d9..20beaca 100644 --- a/client/ExtraChat/Ui/PluginUi.cs +++ b/client/ExtraChat/Ui/PluginUi.cs @@ -186,6 +186,23 @@ internal class PluginUi : IDisposable { // // ImGui.EndCombo(); // } + + if (ImGui.Checkbox("Allow receiving invites", ref this.Plugin.ConfigInfo.AllowInvites)) { + anyChanged = true; + Task.Run(async () => await this.Plugin.Client.AllowInvitesToast(this.Plugin.ConfigInfo.AllowInvites)); + } + + if (ImGui.CollapsingHeader("Delete account")) { + if (this.Plugin.Client.Channels.Count > 0) { + ImGui.TextUnformatted("You must leave or disband all ExtraChat linkshells you are currently in before you can delete your account."); + } else { + ImGui.TextUnformatted("Clicking the button below will permanently and irreversibly delete your account from ExtraChat's servers."); + + if (ImGui.Button("Delete account")) { + Task.Run(async () => await this.Plugin.Client.DeleteAccountToast()); + } + } + } } private void DrawSettingsLinkshells(ref bool anyChanged) {