From 09846c524ced4baca6b18c1d3c17319201066be7 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 22 Aug 2024 15:34:03 +0000 Subject: [PATCH] fedi moji :3 --- .env.example | 2 ++ .gitignore | 1 + auth-fetch.js | 61 +++++++++++++++++++++++++++++++++++++++++++++ commands/restart.js | 2 +- index.js | 1 + lib/ext.js | 15 +++++++++++ lib/fedimbed.js | 12 ++++----- modules/fedimbed.js | 36 +++++++++----------------- package.json | 1 + 9 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 auth-fetch.js diff --git a/.env.example b/.env.example index a43985b..89eb564 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,5 @@ USER_ID=@bot:example.com ACCESS_TOKEN=1234567890 DEVICE_ID=Bot-Device LOG_CHANNEL=!1234567890:example.com +AP_FETCH_PORT=3000 +AP_FETCH_DOMAIN=example.com diff --git a/.gitignore b/.gitignore index 7720d63..354a4d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env node_modules data/* +pnpm-lock.yaml diff --git a/auth-fetch.js b/auth-fetch.js new file mode 100644 index 0000000..e86d80a --- /dev/null +++ b/auth-fetch.js @@ -0,0 +1,61 @@ +import express from "express"; +import fs from "fs"; + +import env from "dotenv"; +env.config(); + +const domain = process.env.AP_FETCH_DOMAIN; +const pubkey = fs.readFileSync("data/publickey.crt", 'utf8'); + +const app = express(); + +const notice = `Don't worry, this doesn't scrape your data.`; + +const actor = { + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1" + ], + + "id": "https://" + domain + "/actor", + "type": "Person", + "preferredUsername": "possumbot", + "inbox": "https://" + domain + "/inbox", + + "publicKey": { + "id": "https://" + domain + "/actor#main-key", + "owner": "https://" + domain + "/actor", + "publicKeyPem": pubkey + } +} + +const webfinger = { + "subject": "acct:possumbot@" + domain + "", + + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": "https://" + domain + "/actor" + } + ] +} + +app.get("/", (req, res) => { + res.setHeader('content-type', 'text/plain'); + res.send(notice); +}); + +app.get("/actor", (req, res) => { + res.setHeader('content-type', 'application/activity+json'); + res.send(JSON.stringify(actor)); +}); + +app.get("/.well-known/webfinger", (req, res) => { + res.setHeader('content-type', 'application/activity+json'); + res.send(JSON.stringify(webfinger)); +}); + +app.listen(process.env.AP_FETCH_PORT); + +export default true; diff --git a/commands/restart.js b/commands/restart.js index 9bc0166..afcf713 100644 --- a/commands/restart.js +++ b/commands/restart.js @@ -1,7 +1,7 @@ import { exec } from "node:child_process"; function execute(client, event, args) { - exec("systemctl --user restart possumbot"); + exec("systemctl restart possumbot"); } export default { diff --git a/index.js b/index.js index 3391534..53f8ce9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +import apfetch from "./auth-fetch.js"; import { encode } from "html-entities"; import * as sdk from "matrix-js-sdk"; import sdkExt from "./lib/ext.js"; diff --git a/lib/ext.js b/lib/ext.js index 4c575a9..ee4809a 100644 --- a/lib/ext.js +++ b/lib/ext.js @@ -88,6 +88,21 @@ export default function(client) { client.error = client.log; + client.uploadMedia = async function(url) { + var matrixUrl = client.cache.get(url); + if(!matrixUrl) { + const buffer = await fetch(url, { + headers: { + "User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)", + }, + }).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf)); + const uploadResponse = await client.uploadContent(buffer, { rawResponse: false }); + matrixUrl = uploadResponse.content_uri; + client.cache.set(url, matrixUrl); + } + return matrixUrl; + } + if(fs.existsSync("data/cache.json")) { var data = fs.readFileSync("data/cache.json"); diff --git a/lib/fedimbed.js b/lib/fedimbed.js index 5fa8f45..58021b3 100644 --- a/lib/fedimbed.js +++ b/lib/fedimbed.js @@ -71,7 +71,7 @@ async function resolvePlatform(url) { return nodeinfo.software.name; } -const keyId = "https://bot.possum.city/actor#main-key"; +const keyId = "https://" + process.env.AP_FETCH_DOMAIN + "/actor#main-key"; const privKey = fs.readFileSync("data/private.pem"); async function signedFetch(url, options) { const urlObj = new URL(url); @@ -498,10 +498,9 @@ async function processUrl(url) { cw = cw ?? ""; //content = htmlToMarkdown(content); - for(const emote of emotes) { - //content = content.replaceAll(emote.name, `[${emote.name}](${emote.url})`); - content = content.replaceAll(emote.name, `${emote.name}`); - } + //for(const emote of emotes) { + // content = content.replaceAll(emote.name, `${emote.name}`); + //} //cw = htmlToMarkdown(cw); @@ -562,7 +561,8 @@ async function processUrl(url) { ? `${url}` : "", embeds, - files + files, + emotes }; } diff --git a/modules/fedimbed.js b/modules/fedimbed.js index 8324eab..37a6927 100644 --- a/modules/fedimbed.js +++ b/modules/fedimbed.js @@ -20,18 +20,7 @@ async function onMessage(client, event) { if(emb.thumbnail && emb.thumbnail.url) { var avatar = document.createElement("img"); - var matrixUrl = client.cache.get("fedimbed_" + emb.thumbnail.url); - if(!matrixUrl) { - const buffer = await fetch(emb.thumbnail.url, { - headers: { - "User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)", - }, - }).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf)); - const uploadResponse = await client.uploadContent(buffer, { rawResponse: false }); - matrixUrl = uploadResponse.content_uri; - client.cache.set("fedimbed_" + emb.thumbnail.url, matrixUrl); - } - avatar.src = matrixUrl; + avatar.src = await client.uploadMedia(emb.thumbnail.url); avatar.height = "16"; link.appendChild(avatar); } @@ -44,20 +33,19 @@ async function onMessage(client, event) { var text = document.createElement("p"); text.innerHTML = emb.description; + + for(const emote of embed.emotes) { + console.log(text.innerHTML); + var img = document.createElement("img"); + img.src = await client.uploadMedia(emote.url); + img.height = "16"; + img.alt = emote.name; + text.innerHTML = text.innerHTML.replaceAll(emote.name, img.outerHTML); + } + quote.appendChild(text); } for(const file of embed.files) { - var matrixUrl = client.cache.get("fedimbed_" + file.url); - if(!matrixUrl) { - const buffer = await fetch(file.url, { - headers: { - "User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)", - }, - }).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf)); - const uploadResponse = await client.uploadContent(buffer, { rawResponse: false }); - matrixUrl = uploadResponse.content_uri; - client.cache.set("fedimbed_" + file.url, matrixUrl); - } var media; switch(file.type.split("/")[0]) { case "audio": @@ -70,7 +58,7 @@ async function onMessage(client, event) { media = document.createElement("img"); break; } - media.src = matrixUrl; + media.src = await client.uploadMedia(file.url); media.alt = file.desc; quote.appendChild(media); } diff --git a/package.json b/package.json index cac5e9a..8ef1ebf 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@peertube/http-signature": "^1.7.0", "cli-color": "^2.0.4", "dotenv": "^16.4.5", + "express": "^4.19.2", "html-entities": "^2.5.2", "jsdom": "^24.1.1", "matrix-js-sdk": "^32.0.0",