-
Notifications
You must be signed in to change notification settings - Fork 0
Quiet boot: skip no-op Discord embed edits + sweep only on real drops #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f5ffb6d
feat(discord): add RenderCanonicalizer + RustPlusBot.Discord.Tests pr…
HandyS11 7beea08
test(discord): cover canonicalizer fallback; walk non-row components
HandyS11 41b4cc5
feat(discord): add RenderGate no-op edit detector
HandyS11 d2cb081
feat(discord): gate no-op embed edits in DiscordChannelMessenger sing…
HandyS11 964578c
feat(connections): carry IsConnected/WasConnected on ConnectionStatus…
HandyS11 92434a2
feat(switches): sweep embeds unreachable only on a drop from Connected
HandyS11 ae24b34
feat(alarms): sweep embeds unreachable only on a drop from Connected
HandyS11 9084ad1
feat(storage): sweep embeds unreachable only on a drop from Connected
HandyS11 10190c2
fix(discord): register slash commands off the gateway Ready handler
HandyS11 d451678
fix(discord): offload workspace heal + connection start off the Ready…
HandyS11 7d60f30
docs: fix Ready-offload comment + stale storage relay summary
HandyS11 8a97780
fix(discord): include select-menu option emote in canonical render
HandyS11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,136 @@ | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using Discord; | ||
|
|
||
| namespace RustPlusBot.Discord.Posting; | ||
|
|
||
| /// <summary> | ||
| /// Flattens an embed + components render into a deterministic canonical string so identical | ||
| /// re-renders can be detected and skipped before hitting Discord's per-channel PATCH bucket. | ||
| /// Values are length-prefixed to make adjacent user-controlled strings collision-proof. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This repo's renderers only ever emit <see cref="ActionRowComponent" />s of buttons and select | ||
| /// menus, which are canonicalized field-by-field. Any other component kind (e.g. Components-V2 | ||
| /// layouts, text inputs) is canonicalized shallowly: only its <see cref="ComponentType" /> and, | ||
| /// when it implements <see cref="IInteractableComponent" />, its custom id are captured. Add a | ||
| /// dedicated case in <c>AppendComponent</c> if this repo ever sends a richer unmodeled kind. | ||
| /// </remarks> | ||
| public static class RenderCanonicalizer | ||
| { | ||
| /// <summary>Builds the canonical string for a render.</summary> | ||
| /// <param name="embed">The embed about to be sent, or null.</param> | ||
| /// <param name="components">The message components about to be sent, or null.</param> | ||
| /// <returns>A deterministic string that is equal iff the visible render is equal.</returns> | ||
| public static string Canonicalize(Embed? embed, MessageComponent? components) | ||
| { | ||
| var sb = new StringBuilder(); | ||
| if (embed is not null) | ||
| { | ||
| AppendEmbed(sb, embed); | ||
| } | ||
|
|
||
| if (components is not null) | ||
| { | ||
| AppendComponents(sb, components); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| private static void AppendEmbed(StringBuilder sb, Embed embed) | ||
| { | ||
| Append(sb, "title", embed.Title); | ||
| Append(sb, "description", embed.Description); | ||
| Append(sb, "url", embed.Url); | ||
| Append(sb, "color", embed.Color?.RawValue.ToString(CultureInfo.InvariantCulture)); | ||
| Append(sb, "timestamp", embed.Timestamp?.UtcDateTime.ToString("O", CultureInfo.InvariantCulture)); | ||
| Append(sb, "author.name", embed.Author?.Name); | ||
| Append(sb, "author.url", embed.Author?.Url); | ||
| Append(sb, "author.icon", embed.Author?.IconUrl); | ||
| Append(sb, "footer.text", embed.Footer?.Text); | ||
| Append(sb, "footer.icon", embed.Footer?.IconUrl); | ||
| Append(sb, "thumbnail", embed.Thumbnail?.Url); | ||
| Append(sb, "image", embed.Image?.Url); | ||
| foreach (var field in embed.Fields) | ||
| { | ||
| Append(sb, "field.name", field.Name); | ||
| Append(sb, "field.value", field.Value); | ||
| Append(sb, "field.inline", field.Inline ? "1" : "0"); | ||
| } | ||
| } | ||
|
|
||
| private static void AppendComponents(StringBuilder sb, MessageComponent components) | ||
| { | ||
| foreach (var component in components.Components) | ||
| { | ||
| if (component is ActionRowComponent row) | ||
| { | ||
| Append(sb, "row", null); | ||
| foreach (var child in row.Components) | ||
| { | ||
| AppendComponent(sb, child); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Top-level non-ActionRow component (e.g. a Components-V2 layout piece): fall back | ||
| // to the same shallow append used for unmodeled component kinds. | ||
| AppendComponent(sb, component); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void AppendComponent(StringBuilder sb, IMessageComponent component) | ||
| { | ||
| switch (component) | ||
| { | ||
| case ButtonComponent button: | ||
| Append(sb, "button.id", button.CustomId); | ||
| Append(sb, "button.label", button.Label); | ||
| Append(sb, "button.style", ((int)button.Style).ToString(CultureInfo.InvariantCulture)); | ||
| Append(sb, "button.url", button.Url); | ||
| Append(sb, "button.disabled", button.IsDisabled ? "1" : "0"); | ||
| Append(sb, "button.emote", button.Emote?.ToString()); | ||
| break; | ||
| case SelectMenuComponent menu: | ||
| Append(sb, "menu.id", menu.CustomId); | ||
| Append(sb, "menu.placeholder", menu.Placeholder); | ||
| Append(sb, "menu.min", menu.MinValues.ToString(CultureInfo.InvariantCulture)); | ||
| Append(sb, "menu.max", menu.MaxValues.ToString(CultureInfo.InvariantCulture)); | ||
| Append(sb, "menu.disabled", menu.IsDisabled ? "1" : "0"); | ||
| foreach (var option in menu.Options) | ||
| { | ||
| Append(sb, "option.label", option.Label); | ||
| Append(sb, "option.value", option.Value); | ||
| Append(sb, "option.description", option.Description); | ||
| Append(sb, "option.emote", option.Emote?.ToString()); | ||
| Append(sb, "option.default", option.IsDefault == true ? "1" : "0"); | ||
| } | ||
|
|
||
| break; | ||
| default: | ||
| // Unmodeled component kind: type + custom id (when the component exposes one) keeps | ||
| // the canonical string honest without building a per-kind serializer for kinds this | ||
| // repo never sends (see the class remarks for the accepted depth). | ||
| Append(sb, "component.type", component.Type.ToString()); | ||
| Append(sb, "component.id", (component as IInteractableComponent)?.CustomId); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private static void Append(StringBuilder sb, string key, string? value) | ||
| { | ||
| sb.Append(key).Append('='); | ||
| if (value is null) | ||
| { | ||
| sb.Append("<null>"); | ||
| } | ||
| else | ||
| { | ||
| sb.Append(value.Length.ToString(CultureInfo.InvariantCulture)).Append(':').Append(value); | ||
| } | ||
|
|
||
| sb.Append('\n'); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8a97780 —
option.emoteis now part of the canonical walk (select menus are a fully modeled kind here, and buttons already included theirs), with a regression test (Select_menu_option_emote_change_changes_the_canonical_string).