fedimbed: components v2 testing with bluesky

This commit is contained in:
Cynthia Foxwell 2025-04-18 14:06:37 -06:00
parent c51a3be091
commit 4f0f9afbb7
Signed by: Cynosphere
SSH key fingerprint: SHA256:H3SM8ufP/uxqLwKSH7xY89TDnbR9uOHzjLoBr0tlajk

View file

@ -357,7 +357,7 @@ async function blueskyQuoteEmbed(quote) {
timestamp: quote.value.createdAt, timestamp: quote.value.createdAt,
}; };
if (quote.author.handle.endsWith(".ap.brid.gy")) { /*if (quote.author.handle.endsWith(".ap.brid.gy")) {
const handle = quote.author.handle.replace(".ap.brid.gy", ""); const handle = quote.author.handle.replace(".ap.brid.gy", "");
const split = handle.split("."); const split = handle.split(".");
@ -398,7 +398,7 @@ async function blueskyQuoteEmbed(quote) {
mainEmbed.color = color; mainEmbed.color = color;
mainEmbed.thumbnail.url = authorData?.icon?.url ?? quote.author.avatar; mainEmbed.thumbnail.url = authorData?.icon?.url ?? quote.author.avatar;
} }
} }*/
if (quote.value.facets?.length > 0) { if (quote.value.facets?.length > 0) {
mainEmbed.description = processBlueskyFacets(mainEmbed.description, quote.value.facets); mainEmbed.description = processBlueskyFacets(mainEmbed.description, quote.value.facets);
@ -529,26 +529,82 @@ async function bluesky(msg, url, spoiler = false) {
} }
} }
const videos = []; const warnings = [];
const embeds = []; if (hidden) {
let sendWait = false; warnings.push(":warning: Post marked as hidden");
}
if (adult) {
if (channelNsfw || !msg.guildID) {
warnings.push(`:warning: Post contains adult content: ${Array.from(new Set(tags)).join(", ")}`);
} else {
return {
response: {
content: "Not embedding post due to adult content in SFW channel",
},
};
}
}
const mainEmbed = { const videos = [];
color: PLATFORM_COLORS.bluesky, const images = [];
url, //const embeds = [];
title: `${post.author.displayName} (@${post.author.handle})`, //let sendWait = false;
description: post.record.text,
thumbnail: { const warningText = {
url: post.author.avatar, type: 10,
}, content: `## ${warnings.join("\n## ")}`,
footer: {
text: `${getStatsBluesky(post)}\nBluesky`,
icon_url: "https://bsky.app/static/apple-touch-icon.png",
},
timestamp: post.record.createdAt,
}; };
if (post.author.handle.endsWith(".ap.brid.gy")) { const container = {
type: 17,
accent_color: PLATFORM_COLORS.bluesky,
components: [],
spoiler,
};
let context = "";
if (data.thread.parent) {
const reply = data.thread.parent.post;
context = `### <:i:1308640078825787412> Replying to: [${reply.author.displayName} (@${
reply.author.handle
})](https://bsky.app/profile/${reply.author.did}/post/${reply.uri.substring(reply.uri.lastIndexOf("/") + 1)})`;
}
const header = {
type: 9,
components: [
{
type: 10,
content:
`${context}\n## ${post.author.displayName}\n[@${post.author.handle}](https://bsky.app/profile/${post.author.did})`.trim(),
},
],
accessory: {
type: 11,
media: {
url: post.author.avatar,
},
},
};
container.components.push(header);
const postContent = {
type: 10,
content:
post.record.facets?.length ?? 0 > 0
? processBlueskyFacets(post.record.text, post.record.facets)
: post.record.text,
};
container.components.push(postContent);
const footer = {
type: 10,
content: `${getStatsBluesky(post)}\n-# Bluesky \u2022 <t:${Math.floor(
new Date(post.record.createdAt).getTime() / 1000
)}:F>`,
};
/*if (post.author.handle.endsWith(".ap.brid.gy")) {
const handle = post.author.handle.replace(".ap.brid.gy", ""); const handle = post.author.handle.replace(".ap.brid.gy", "");
const split = handle.split("."); const split = handle.split(".");
@ -589,29 +645,12 @@ async function bluesky(msg, url, spoiler = false) {
mainEmbed.color = color; mainEmbed.color = color;
mainEmbed.thumbnail.url = authorData?.icon?.url ?? post.author.avatar; mainEmbed.thumbnail.url = authorData?.icon?.url ?? post.author.avatar;
} }
} }*/
if (post.record.facets?.length > 0) {
mainEmbed.description = processBlueskyFacets(mainEmbed.description, post.record.facets);
}
if (data.thread.parent) {
const reply = data.thread.parent.post;
mainEmbed.author = {
name: `Replying to: ${reply.author.displayName} (@${reply.author.handle})`,
icon_url: "https://cdn.discordapp.com/emojis/1308640078825787412.png",
url: `https://bsky.app/profile/${reply.author.handle}/post/${reply.uri.substring(
reply.uri.lastIndexOf("/") + 1
)}`,
};
}
if (post.embed) { if (post.embed) {
switch (post.embed.$type) { switch (post.embed.$type) {
case "app.bsky.embed.images#view": { case "app.bsky.embed.images#view": {
embeds.push( images.push(post.embed.images.map((image) => ({media: {url: image.fullsize}, description: image.alt})));
...post.embed.images.map((image) => ({...mainEmbed, image: {url: image.fullsize, description: image.alt}}))
);
break; break;
} }
case "app.bsky.embed.video#view": { case "app.bsky.embed.video#view": {
@ -619,13 +658,7 @@ async function bluesky(msg, url, spoiler = false) {
const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint;
const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${post.author.did}&cid=${post.embed.cid}`; const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${post.author.did}&cid=${post.embed.cid}`;
const contentType = await fetch(videoUrl, { videos.push({media: {url: videoUrl}, description: post.embed.alt});
method: "HEAD",
}).then((res) => res.headers.get("Content-Type"));
videos.push({url: videoUrl, desc: post.embed.alt, type: contentType});
embeds.push({...mainEmbed, fields: [{name: "\u200b", value: `[Video Link](${videoUrl})`}]});
break; break;
} }
case "app.bsky.embed.record#view": { case "app.bsky.embed.record#view": {
@ -635,7 +668,7 @@ async function bluesky(msg, url, spoiler = false) {
if (quoteData.videos.length > 0) videos.push(...quoteData.videos); if (quoteData.videos.length > 0) videos.push(...quoteData.videos);
embeds.push(mainEmbed, ...quoteData.embeds); //embeds.push(mainEmbed, ...quoteData.embeds);
if (quoteData.adult) adult = true; if (quoteData.adult) adult = true;
if (quoteData.hidden) hidden = true; if (quoteData.hidden) hidden = true;
@ -644,16 +677,11 @@ async function bluesky(msg, url, spoiler = false) {
break; break;
} }
case "app.bsky.embed.recordWithMedia#view": { /*case "app.bsky.embed.recordWithMedia#view": {
hasQuote = true; hasQuote = true;
if (post.embed.media.$type === "app.bsky.embed.images#view") { if (post.embed.media.$type === "app.bsky.embed.images#view") {
embeds.push( images.push(post.embed.media.images.map((image) => ({media:{url: image.fullsize}, description: image.alt})));
...post.embed.media.images.map((image) => ({
...mainEmbed,
image: {url: image.fullsize, description: image.alt},
}))
);
} else if (post.embed.media.$type === "app.bsky.embed.video#view") { } else if (post.embed.media.$type === "app.bsky.embed.video#view") {
const lookup = await fetch(`https://plc.directory/${post.author.did}`).then((res) => res.json()); const lookup = await fetch(`https://plc.directory/${post.author.did}`).then((res) => res.json());
const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint;
@ -689,28 +717,20 @@ async function bluesky(msg, url, spoiler = false) {
if (quoteData.tags.length > 0) tags.push(...quoteData.tags); if (quoteData.tags.length > 0) tags.push(...quoteData.tags);
break; break;
} }*/
case "app.bsky.embed.external#view": { case "app.bsky.embed.external#view": {
if (post.embed.external.uri.includes("tenor.com")) { if (post.embed.external.uri.includes("tenor.com")) {
const url = new URL(post.embed.external.uri); const url = new URL(post.embed.external.uri);
url.searchParams.delete("hh"); url.searchParams.delete("hh");
url.searchParams.delete("ww"); url.searchParams.delete("ww");
embeds.push({...mainEmbed, image: {url: url.toString()}}); images.push({media: {url: url.toString()}});
} else {
embeds.push(mainEmbed);
} }
break; break;
} }
default: {
embeds.push(mainEmbed);
break;
}
} }
} else {
embeds.push(mainEmbed);
} }
if (videos.length > 0) { /*if (videos.length > 0) {
sendWait = true; sendWait = true;
if (msg instanceof Message) await msg.addReaction("\uD83D\uDCE4"); if (msg instanceof Message) await msg.addReaction("\uD83D\uDCE4");
} }
@ -748,31 +768,25 @@ async function bluesky(msg, url, spoiler = false) {
}); });
} }
} }
} }*/
const warnings = []; container.components.push({
if (hidden) { type: 12,
warnings.push(":warning: Post marked as hidden"); items: images,
} });
if (adult) { for (const video of videos) {
if (channelNsfw || !msg.guildID) { container.components.push({
warnings.push(`:warning: Post contains adult content: ${Array.from(new Set(tags)).join(", ")}`); type: 12,
} else { items: [video],
return { });
response: {
content: "Not embedding post due to adult content in SFW channel",
},
};
}
} }
container.components.push(footer);
if (quoteOnly && !hasQuote) return {}; if (quoteOnly && !hasQuote) return {};
return { return {
response: { response: {
content: `${warnings.length > 0 ? warnings.join("\n") + "\n" : ""}${spoiler ? `|| ${url} ||` : ""}`, components: [warnings.length > 0 ? warningText : false, container].filter((x) => !!x),
embeds,
attachments: files,
allowedMentions: { allowedMentions: {
repliedUser: false, repliedUser: false,
}, },
@ -780,7 +794,6 @@ async function bluesky(msg, url, spoiler = false) {
messageID: msg.id, messageID: msg.id,
}, },
}, },
sendWait,
}; };
} }