fedimbed: minimal mode

This commit is contained in:
Cynthia Foxwell 2025-05-06 20:47:47 -06:00
parent 79da0933cf
commit 37c09e1944
Signed by: Cynosphere
SSH key fingerprint: SHA256:H3SM8ufP/uxqLwKSH7xY89TDnbR9uOHzjLoBr0tlajk

View file

@ -480,7 +480,7 @@ async function blueskyQuoteEmbed(quote) {
return {components, adult, hidden, spoiler, tags}; return {components, adult, hidden, spoiler, tags};
} }
async function bluesky(msg, url, spoiler = false) { async function bluesky(msg, url, spoiler = false, minimal = false) {
const quoteOnly = await hasFlag(msg.guildID, "bskyQuoteOnly"); const quoteOnly = await hasFlag(msg.guildID, "bskyQuoteOnly");
if (url.includes("bsky.brid.gy")) url = url.replace("bsky.brid.gy/r/https://", ""); if (url.includes("bsky.brid.gy")) url = url.replace("bsky.brid.gy/r/https://", "");
@ -637,7 +637,7 @@ async function bluesky(msg, url, spoiler = false) {
const warningText = { const warningText = {
type: 10, type: 10,
content: `## ${warnings.join("\n## ")}`, content: minimal ? `${warnings.join("\n")}` : `## ${warnings.join("\n## ")}`,
}; };
const container = { const container = {
@ -650,7 +650,7 @@ async function bluesky(msg, url, spoiler = false) {
let context = ""; let context = "";
if (data.thread.parent) { if (data.thread.parent) {
const reply = data.thread.parent.post; const reply = data.thread.parent.post;
context = `-# ${Icons.fedimbed.reply} Replying to: [${reply.author.displayName} (@${ context = `-# ${Icons.fedimbed.reply} ${minimal ? "" : "Replying to: "}[${reply.author.displayName} (@${
reply.author.handle reply.author.handle
})](https://bsky.app/profile/${reply.author.did}/post/${reply.uri.substring(reply.uri.lastIndexOf("/") + 1)})`; })](https://bsky.app/profile/${reply.author.did}/post/${reply.uri.substring(reply.uri.lastIndexOf("/") + 1)})`;
} }
@ -661,46 +661,57 @@ async function bluesky(msg, url, spoiler = false) {
} }
content = content.replaceAll("`", "\u02cb").replaceAll("*", "\u2217").replaceAll("||", "|\u200b|"); content = content.replaceAll("`", "\u02cb").replaceAll("*", "\u2217").replaceAll("||", "|\u200b|");
const contentComponent = {
type: 10,
content: minimal
? `### [${post.author.displayName} (@${post.author.handle})](${url})`
: `## ${post.author.displayName}\n-# [@${post.author.handle}](https://bsky.app/profile/${post.author.did})\n${content}`,
};
const header = [ const header = [
context && { context && {
type: 10, type: 10,
content: context, content: context,
}, },
{ minimal
type: 9, ? contentComponent
components: [ : {
{ type: 9,
type: 10, components: [contentComponent],
content: `## ${post.author.displayName}\n-# [@${post.author.handle}](https://bsky.app/profile/${post.author.did})\n${content}`, accessory: {
type: 11,
media: {
url: post.author.avatar,
},
},
}, },
],
accessory: {
type: 11,
media: {
url: post.author.avatar,
},
},
},
].filter((x) => !!x); ].filter((x) => !!x);
container.components.push(...header); container.components.push(...header);
const footer = { const footer = minimal
type: 9, ? {
components: [
{
type: 10, type: 10,
content: `### -# ${getStatsBluesky(post)}\n${Icons.fedimbed.bluesky} Bluesky \u2022 <t:${Math.floor( content: `${Icons.fedimbed.bluesky} Bluesky \u2022 <t:${Math.floor(
new Date(post.record.createdAt).getTime() / 1000 new Date(post.record.createdAt).getTime() / 1000
)}:F>`, )}:F>`,
}, }
], : {
accessory: { type: 9,
type: 2, components: [
style: 5, {
label: "View Post", type: 10,
url, content: `### -# ${getStatsBluesky(post)}\n${Icons.fedimbed.bluesky} Bluesky \u2022 <t:${Math.floor(
}, new Date(post.record.createdAt).getTime() / 1000
}; )}:F>`,
},
],
accessory: {
type: 2,
style: 5,
label: "View Post",
url,
},
};
if (images.length > 0) { if (images.length > 0) {
container.components.push({ container.components.push({
@ -936,7 +947,7 @@ function getStatsMasto(post) {
return stats.join("\u3000"); return stats.join("\u3000");
} }
async function processUrl(msg, url, spoiler = false, command = false, inQuote = false) { async function processUrl(msg, url, spoiler = false, command = false, inQuote = false, minimal = false) {
let canFedi = await hasFlag(msg.guildID, "fedimbed"); let canFedi = await hasFlag(msg.guildID, "fedimbed");
let canBsky = await hasFlag(msg.guildID, "bskyEmbeds"); let canBsky = await hasFlag(msg.guildID, "bskyEmbeds");
let canTwitter = await hasFlag(msg.guildID, "twitterEmbeds"); let canTwitter = await hasFlag(msg.guildID, "twitterEmbeds");
@ -959,7 +970,7 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
if (BSKY_DOMAINS.includes(urlObj.hostname.toLowerCase())) { if (BSKY_DOMAINS.includes(urlObj.hostname.toLowerCase())) {
if (canBsky) { if (canBsky) {
return await bluesky(msg, url, spoiler); return await bluesky(msg, url, spoiler, minimal);
} else { } else {
return {}; return {};
} }
@ -1303,7 +1314,7 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
!inQuote ? "" : `<small>\u2198 Quoting <a href="${quoteUrl}">$1</a></small>` !inQuote ? "" : `<small>\u2198 Quoting <a href="${quoteUrl}">$1</a></small>`
); );
if (!inQuote) quoteRes = await processUrl(msg, quoteUrl, spoiler, command, true); if (!inQuote) quoteRes = await processUrl(msg, quoteUrl, spoiler, command, true, minimal);
} }
content = content content = content
@ -1384,9 +1395,11 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
spoiler, spoiler,
}; };
let headerContent = `${author.name ? `## ${author.name}\n` : ""}-# [${author.handle}](${author.url})`; let headerContent = minimal
? `### [${author.name ? `${author.name} (${author.handle})` : author.handle}](${realUrl})`
: `${author.name ? `## ${author.name}\n` : ""}-# [${author.handle}](${author.url})`;
if (title) headerContent += "\n### " + title; if (title) headerContent += minimal ? "\n**__" + title + "__**" : "\n### " + title;
MAX_LENGTH -= headerContent.length + 1; MAX_LENGTH -= headerContent.length + 1;
@ -1400,26 +1413,32 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
} }
headerContent += "\n" + desc.trim(); headerContent += "\n" + desc.trim();
const contentComponent = {
type: 10,
content: headerContent,
};
if (minimal && context) {
context = context.replace("Replying to: ", "");
}
const header = [ const header = [
context && { context && {
type: 10, type: 10,
content: context, content: context,
}, },
{ minimal
type: 9, ? contentComponent
components: [ : {
{ type: 9,
type: 10, components: [contentComponent],
content: headerContent, accessory: {
type: 11,
media: {
url: author.avatar,
},
},
}, },
],
accessory: {
type: 11,
media: {
url: author.avatar,
},
},
},
].filter((x) => !!x); ].filter((x) => !!x);
container.components.push(...header); container.components.push(...header);
@ -1445,23 +1464,30 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
} }
const footerIcon = PLATFORM_ICONS[platformName.toLowerCase()]; const footerIcon = PLATFORM_ICONS[platformName.toLowerCase()];
const footer = { const footer = minimal
type: 9, ? {
components: [
{
type: 10, type: 10,
content: `${stats.length > 0 ? "### -# " + stats + "\n" : ""}${ content: `${footerIcon ? footerIcon + " " : ""}${platformName} \u2022 <t:${Math.floor(
footerIcon ? footerIcon + " " : "" new Date(timestamp).getTime() / 1000
}${platformName} \u2022 <t:${Math.floor(new Date(timestamp).getTime() / 1000)}:F>`, )}:F>`,
}, }
], : {
accessory: { type: 9,
type: 2, components: [
style: 5, {
label: "View Post", type: 10,
url: realUrl, content: `${stats.length > 0 ? "### -# " + stats + "\n" : ""}${
}, footerIcon ? footerIcon + " " : ""
}; }${platformName} \u2022 <t:${Math.floor(new Date(timestamp).getTime() / 1000)}:F>`,
},
],
accessory: {
type: 2,
style: 5,
label: "View Post",
url: realUrl,
},
};
if (images.length > 0) { if (images.length > 0) {
const overMax = images.length + videos.length > 40; const overMax = images.length + videos.length > 40;
if (images.length > 10) { if (images.length > 10) {
@ -1525,7 +1551,8 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
const quoteComponents = quoteRes.response.components[0].components; const quoteComponents = quoteRes.response.components[0].components;
const quoteContext = `-# ${Icons.fedimbed.quote} Quoted Post`; const quoteContext = `-# ${Icons.fedimbed.quote} Quoted Post`;
if (quoteComponents[0].type == 10) { if (quoteComponents[0].type == 10) {
quoteComponents[0].content = quoteContext + "\n" + quoteComponents[0].content; quoteComponents[0].content =
(minimal ? Icons.fedimbed.quote + " " : quoteContext + "\n") + quoteComponents[0].content;
} else { } else {
quoteComponents.splice(0, 0, {type: 10, content: quoteContext}); quoteComponents.splice(0, 0, {type: 10, content: quoteContext});
} }
@ -1611,10 +1638,18 @@ fedimbedCommand.options.spoiler = {
required: false, required: false,
default: false, default: false,
}; };
fedimbedCommand.options.minimal = {
name: "minimal",
type: ApplicationCommandOptionTypes.BOOLEAN,
description: "Remove stats, avatars and other visual noise",
required: false,
default: false,
};
fedimbedCommand.permissions = Permissions.embedLinks | Permissions.attachFiles; fedimbedCommand.permissions = Permissions.embedLinks | Permissions.attachFiles;
fedimbedCommand.callback = async function (interaction) { fedimbedCommand.callback = async function (interaction) {
let url = this.getOption(interaction, "url"); let url = this.getOption(interaction, "url");
const spoiler = this.getOption(interaction, "spoiler"); const spoiler = this.getOption(interaction, "spoiler");
const minimal = this.getOption(interaction, "minimal");
url = url url = url
.replace(/\|/g, "") .replace(/\|/g, "")
@ -1644,7 +1679,7 @@ fedimbedCommand.callback = async function (interaction) {
if (hasService || BSKY_DOMAINS.includes(urlObj.hostname.toLowerCase())) { if (hasService || BSKY_DOMAINS.includes(urlObj.hostname.toLowerCase())) {
try { try {
const {response} = await processUrl(interaction, url, spoiler, true); const {response} = await processUrl(interaction, url, spoiler, true, false, minimal);
if (!response) if (!response)
return { return {