From 50d9c78b45008e06e6ee241ed23c51399a5f520a Mon Sep 17 00:00:00 2001 From: Ean Milligan Date: Thu, 17 Jul 2025 04:29:20 -0400 Subject: [PATCH] Upgrade web view system with auto timeout --- .../managers/handler/workerComplete.ts | 5 +- src/artigen/utils/embeds.ts | 18 +++- src/endpoints/gets/webView.ts | 100 ++++++++++++++---- 3 files changed, 95 insertions(+), 28 deletions(-) diff --git a/src/artigen/managers/handler/workerComplete.ts b/src/artigen/managers/handler/workerComplete.ts index a9d4184..668c522 100644 --- a/src/artigen/managers/handler/workerComplete.ts +++ b/src/artigen/managers/handler/workerComplete.ts @@ -194,9 +194,8 @@ export const onWorkerComplete = async (workerMessage: MessageEvent, const respMessage: Embed[] = [ { color: infoColor1, - description: `This message contains information for a previous roll.\nPlease click on "<@${botId}> *Click to see attachment*" above this message to see the previous roll. - -As anyone with the Web View link can view the roll, Web View is disabled by default for privacy. Click the button below to enable Web View and generate a link for this roll.`, + description: `**This message contains information for a previous roll.** +Please click on "<@${botId}> *Click to see attachment*" above this message to see the previous roll.`, }, ]; diff --git a/src/artigen/utils/embeds.ts b/src/artigen/utils/embeds.ts index 058005f..1a89553 100644 --- a/src/artigen/utils/embeds.ts +++ b/src/artigen/utils/embeds.ts @@ -292,8 +292,22 @@ export const toggleWebView = (attachmentMessage: DiscordenoMessage, ownerId: str ...attachmentMessage.embeds[0], fields: [ { - name: 'Web View:', - value: enableWebView ? `[Open Web View](${config.api.publicDomain}api/webview?c=${attachmentMessage.channelId}&m=${attachmentMessage.id})` : `Web View is ${disabledStr}.`, + name: 'Web View Link:', + value: enableWebView + ? `[Open Web View](${config.api.publicDomain}api/webview?c=${attachmentMessage.channelId}&m=${attachmentMessage.id}#${new Date().getTime()}#${ownerId})` + : `Web View is ${disabledStr}.\n- Click the button below to enable Web View and generate a link for this roll.`, + inline: true, + }, + { + name: enableWebView ? 'Web View Status:' : 'What is Web View?', + value: enableWebView + ? `For privacy, Web View will automatically time out on this roll . The link will still show on this message after it has timed out, so clicking on the link after it has been timed out will show an error and remove the link.` + : '- Web View is a system for viewing extremely large or complex rolls with full formatting.\n- As anyone with the Web View link can view the roll, Web View is disabled by default for privacy.', + inline: true, }, ], }, diff --git a/src/endpoints/gets/webView.ts b/src/endpoints/gets/webView.ts index e15d8f5..a519774 100644 --- a/src/endpoints/gets/webView.ts +++ b/src/endpoints/gets/webView.ts @@ -5,7 +5,7 @@ import { STATUS_CODE, STATUS_TEXT } from '@std/http/status'; import config from '~config'; -import { disabledStr } from 'artigen/utils/embeds.ts'; +import { disabledStr, toggleWebView } from 'artigen/utils/embeds.ts'; import utils from 'utils/utils.ts'; @@ -21,6 +21,7 @@ interface ModernUserHOTFIX extends User { const converter = new showdown.Converter({ emoji: true, underline: true, + openLinksInNewWindow: true, }); // Utilize the pre-existing stylesheets, do a little tweaking to make it ours @@ -71,13 +72,39 @@ font-weight: 900; ${str} `; + +const headerHeight = '3rem'; +const wrapHeader = ( + str: string, +) => + `
+${config.name} Roll Web View +${str} +
`; const centerHTML = (str: string) => `
${str}
`; -const badRequestMD = '# Invalid URL for Web View!'; -const badRequestHTML = wrapBasic(centerHTML(converter.makeHtml(badRequestMD))); +const badRequestHTML = wrapBasic(`${wrapHeader('')}${centerHTML(converter.makeHtml('# Invalid URL for Web View!'))}`); +const badMessageHTML = wrapBasic( + `${wrapHeader('')}${ + centerHTML( + converter.makeHtml(`# Discord Attachment message malformed or corrupt! +Not sure how this happened, but Web View was unable to read the required data to generate the formatted output.`), + ) + }`, +); +const notAuthorizedHTML = wrapBasic(`${wrapHeader('')}${centerHTML(converter.makeHtml('# Web View is Disabled for this roll!'))}`); -const notAuthorizedMD = '# Web View is Disabled for this roll!'; -const notAuthorizedHTML = wrapBasic(centerHTML(converter.makeHtml(notAuthorizedMD))); +const oldHTML = (guildId: bigint | string, channelId: bigint, messageId: bigint) => + wrapBasic( + `${wrapHeader('')}${ + centerHTML( + converter.makeHtml(`# Web View has been Disabled for this roll! +For privacy, Web View automatically disables itself 1 hour after being enabled. + +Need to see this roll in Web View again? Head back to [this message](https://discord.com/channels/${guildId}/${channelId}/${messageId}) and click the \`Enable Web View\` button.`), + ) + }`, + ); const failedToGetAttachmentMD = '# Failed to get attachment from Discord!'; const failedToGetAttachmentHTML = wrapBasic(centerHTML(converter.makeHtml(failedToGetAttachmentMD))); @@ -87,23 +114,21 @@ interface HtmlResp { html: string; } -const headerHeight = '3rem'; const generatePage = (files: HtmlResp[]): string => - wrapBasic(`
-${config.name} Roll Web View -Available Files: + wrapBasic(`${ + wrapHeader(`Available Files: ${ - files - .map( - (f, idx) => - ``, - ) - .join('') + files + .map( + (f, idx) => + ``, + ) + .join('') + } +`) } - -
${files.map((f, idx) => `
${f.html}
`).join('')}
`); @@ -149,11 +174,19 @@ export const generateWebView = async (query: Map): Promise utils.commonLoggers.messageGetError('webView.ts:23', channelId, messageId, e)); const discordAttachments = attachmentMessage?.attachments ?? []; - const embed = attachmentMessage?.embeds.shift(); - const webViewField = embed?.fields?.shift(); + const embed = attachmentMessage?.embeds[0]; - if (!attachmentMessage || discordAttachments.length === 0 || !embed || !webViewField) { - return new Response(badRequestHTML, { + if (!attachmentMessage || discordAttachments.length === 0 || !embed || !embed.fields?.length) { + return new Response(badMessageHTML, { + status: STATUS_CODE.BadRequest, + statusText: STATUS_TEXT[STATUS_CODE.BadRequest], + headers, + }); + } + + const webViewField = embed.fields[0]; + if (!webViewField.value) { + return new Response(badMessageHTML, { status: STATUS_CODE.BadRequest, statusText: STATUS_TEXT[STATUS_CODE.BadRequest], headers, @@ -168,6 +201,27 @@ export const generateWebView = async (query: Map): Promise log(LT.LOG, `Failed to get attachment: ${discordAttachment}`, e));