import { MatrixClient, SimpleFsStorageProvider } from "@vector-im/matrix-bot-sdk"; import escapeHTML from "escape-html"; import { resolveServer, formatBytes, formatDuration } from "./util.js"; import { homeserver_url, access_token } from "./cred.js"; var storage = new SimpleFsStorageProvider("storage.json"); export var client = new MatrixClient(homeserver_url, access_token, storage); client.on("room.message", async (roomId, event) => { console.log(roomId, JSON.stringify(event)); if (event.sender != await client.getUserId()) return; if (!event.content) return; if (event.content["m.relates_to"]?.rel_type == "m.replace" || event.content["m.new_content"]) return; if (!["m.video","m.file"].includes(event.content.msgtype)) return; if (!event.content.url?.startsWith("mxc://kitty.haus")) return; let [server_name, media_id] = event.content.url.replace("mxc://", '').split("/"); let body = event.content.filename ? event.content.body : null; let formatted_body = event.content.formatted_body; let filename = event.content.filename || event.content.body; let i = event.content.info || {}; let direct_url = `https://mxc.kitty.haus/${media_id}/${encodeURIComponent(filename)}?type=${encodeURIComponent(i.mimetype || "application/octet-stream")}`; let details = []; if (i.duration || i.size || i.w || i.h) { if (i.duration) details.push(formatDuration(i.duration)); if (i.size) details.push(formatBytes(i.size)); if (i.w || i.h) details.push(`${i.w}x${i.h}`); } details = details.length ? ` (${details.join(", ")})` : ""; let plain = `Direct URL: ${direct_url}${details}`; let html = `${escapeHTML(filename)}${escapeHTML(details)}`; if (formatted_body) html = `${formatted_body}

${html}`; else if (body) html = `${escapeHTML(body)}

${html}`; if (body) plain = `${body}\n\n${plain}`; let m_new_content = Object.assign(structuredClone(event.content), { format: "org.matrix.custom.html", formatted_body: html, body: plain, filename }); let content = Object.assign(structuredClone(m_new_content), { "m.new_content": m_new_content, "m.relates_to": { event_id: event.event_id, rel_type: "m.replace" }, body: `* ${m_new_content.body}`, formatted_body: `* ${m_new_content.formatted_body}` }); client.sendEvent(roomId, "m.room.message", content); }); client.start().then(() => console.log("Bot started!"));