fedi moji :3

This commit is contained in:
Ashley 2024-08-22 15:34:03 +00:00
parent a9b3d84366
commit 09846c524c
9 changed files with 100 additions and 31 deletions

View file

@ -5,3 +5,5 @@ USER_ID=@bot:example.com
ACCESS_TOKEN=1234567890 ACCESS_TOKEN=1234567890
DEVICE_ID=Bot-Device DEVICE_ID=Bot-Device
LOG_CHANNEL=!1234567890:example.com LOG_CHANNEL=!1234567890:example.com
AP_FETCH_PORT=3000
AP_FETCH_DOMAIN=example.com

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.env .env
node_modules node_modules
data/* data/*
pnpm-lock.yaml

61
auth-fetch.js Normal file
View file

@ -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;

View file

@ -1,7 +1,7 @@
import { exec } from "node:child_process"; import { exec } from "node:child_process";
function execute(client, event, args) { function execute(client, event, args) {
exec("systemctl --user restart possumbot"); exec("systemctl restart possumbot");
} }
export default { export default {

View file

@ -1,3 +1,4 @@
import apfetch from "./auth-fetch.js";
import { encode } from "html-entities"; import { encode } from "html-entities";
import * as sdk from "matrix-js-sdk"; import * as sdk from "matrix-js-sdk";
import sdkExt from "./lib/ext.js"; import sdkExt from "./lib/ext.js";

View file

@ -88,6 +88,21 @@ export default function(client) {
client.error = client.log; 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")) { if(fs.existsSync("data/cache.json")) {
var data = fs.readFileSync("data/cache.json"); var data = fs.readFileSync("data/cache.json");

View file

@ -71,7 +71,7 @@ async function resolvePlatform(url) {
return nodeinfo.software.name; 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"); const privKey = fs.readFileSync("data/private.pem");
async function signedFetch(url, options) { async function signedFetch(url, options) {
const urlObj = new URL(url); const urlObj = new URL(url);
@ -498,10 +498,9 @@ async function processUrl(url) {
cw = cw ?? ""; cw = cw ?? "";
//content = htmlToMarkdown(content); //content = htmlToMarkdown(content);
for(const emote of emotes) { //for(const emote of emotes) {
//content = content.replaceAll(emote.name, `[${emote.name}](${emote.url})`); // content = content.replaceAll(emote.name, `<img src="${emote.url}" alt="${emote.name}" />`);
content = content.replaceAll(emote.name, `<img src="${emote.url}" alt="${emote.name}" />`); //}
}
//cw = htmlToMarkdown(cw); //cw = htmlToMarkdown(cw);
@ -562,7 +561,8 @@ async function processUrl(url) {
? `<span data-mx-spoiler>${url}</span>` ? `<span data-mx-spoiler>${url}</span>`
: "", : "",
embeds, embeds,
files files,
emotes
}; };
} }

View file

@ -20,18 +20,7 @@ async function onMessage(client, event) {
if(emb.thumbnail && emb.thumbnail.url) { if(emb.thumbnail && emb.thumbnail.url) {
var avatar = document.createElement("img"); var avatar = document.createElement("img");
var matrixUrl = client.cache.get("fedimbed_" + emb.thumbnail.url); avatar.src = await client.uploadMedia(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.height = "16"; avatar.height = "16";
link.appendChild(avatar); link.appendChild(avatar);
} }
@ -44,20 +33,19 @@ async function onMessage(client, event) {
var text = document.createElement("p"); var text = document.createElement("p");
text.innerHTML = emb.description; 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); quote.appendChild(text);
} }
for(const file of embed.files) { 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; var media;
switch(file.type.split("/")[0]) { switch(file.type.split("/")[0]) {
case "audio": case "audio":
@ -70,7 +58,7 @@ async function onMessage(client, event) {
media = document.createElement("img"); media = document.createElement("img");
break; break;
} }
media.src = matrixUrl; media.src = await client.uploadMedia(file.url);
media.alt = file.desc; media.alt = file.desc;
quote.appendChild(media); quote.appendChild(media);
} }

View file

@ -11,6 +11,7 @@
"@peertube/http-signature": "^1.7.0", "@peertube/http-signature": "^1.7.0",
"cli-color": "^2.0.4", "cli-color": "^2.0.4",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "^4.19.2",
"html-entities": "^2.5.2", "html-entities": "^2.5.2",
"jsdom": "^24.1.1", "jsdom": "^24.1.1",
"matrix-js-sdk": "^32.0.0", "matrix-js-sdk": "^32.0.0",