From e3e2233054a8b9bc336fbea108b643616a79c314 Mon Sep 17 00:00:00 2001 From: shay <43248357+shayypy@users.noreply.github.com> Date: Sat, 30 Dec 2023 22:05:57 -0600 Subject: [PATCH] feat: profile options --- README.md | 4 ++++ src/index.ts | 29 +++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 73941d1..8e13e09 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ Simply replace `media.guilded.gg` in your webhook URL with `guilded.shayy.worker Guilded sent errors about unknown event types through as a message, but this proxy does not. If you want to see everything without errors in your chat channel, just enable "Send me everything" (although you may want to disable `star` and `watch` for particularly large repositories). +### Custom profiles + +Guilded forced a webhook name of GitHub and avatar of the GitHub logo, but this can be disabled in GWP by passing `profile=none` as a query parameter. This will cause the messages to use the default name & avatar configured in Guilded settings. To customize even further, pass `username=xxx` and/or `avatarUrl=yyy`, where `xxx` is a URL-encoded string up to 128 characters, and `yyy` is a URL-encoded image URL for the profile picture. This is particularly useful if you want to use the same webhook for multiple repositories or organizations. This will be ignored for applicable [immersive mode](#immersive-discussion) messages. + ### Immersive discussion Pass `immersive=chat` at the end of the webhook URL to enable immersive discussion mode. This feature makes use of custom profiles to make it appear like GitHub users are chatting in your webhook channel. To display user messages with embeds (different sub/superset of markdown), use `immersive=embeds` instead. diff --git a/src/index.ts b/src/index.ts index 842aae8..e3a72a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -77,7 +77,21 @@ router const search = new URL(request.url).searchParams; const showReactions = search.get("reactions") !== "false", showDrafts = search.get("drafts") === "true", - immersiveRaw = search.get("immersive"); + immersiveRaw = search.get("immersive"), + forceProfile = search.get("profile") !== "none", + profileUsername = search.get("username"), + profileAvatarUrl = search.get("avatarUrl"); + + if (profileUsername && profileUsername.length > 128) { + return json({ code: "ProxyBadUsername", message: "Username is too long." }, { status: 400 }); + } + if (profileAvatarUrl) { + try { + new URL(profileAvatarUrl); + } catch { + return json({ code: "ProxyBadAvatar", message: "Avatar URL must be a valid URL." }, { status: 400 }); + } + } const immersiveMode = ["chat", "embeds"].includes(immersiveRaw || "") ? immersiveRaw as "chat" | "embeds" @@ -404,11 +418,14 @@ router embeds?: APIEmbed[]; username?: string; avatar_url?: string; - } = { - embeds, - username: "GitHub", - avatar_url: "https://cdn.gilcdn.com/UserAvatar/3f8e4273b8b9dcacd57379a637a773f4-Large.png", - }; + } = { embeds }; + + if (forceProfile) { + payload.username = "GitHub"; + payload.avatar_url = "https://cdn.gilcdn.com/UserAvatar/3f8e4273b8b9dcacd57379a637a773f4-Large.png"; + } + if (profileUsername) payload.username = profileUsername; + if (profileAvatarUrl) payload.avatar_url = profileAvatarUrl; if (immersiveMode && immContent !== undefined) { immContent = immContent