import { createClient, MatrixClient, MsgType } from "npm:matrix-js-sdk"; import * as sqlite from "jsr:@db/sqlite"; import { config } from "./config.ts"; const client: MatrixClient = createClient({ baseUrl: config.HOMESERVER_URL, accessToken: config.MATRIX_ACCESS_TOKEN, userId: config.USER_ID, }); await client.startClient(); console.log("Client Started"); async function sendMessage(msg: string) { await client.sendMessage(config.ROOM_ID, { msgtype: MsgType.Text, body: msg, format: "org.matrix.custom.html", formatted_body: msg, }); } const db = new sqlite.Database(config.DB_PATH); await db.exec(` CREATE TABLE IF NOT EXISTS topics ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, slug TEXT NOT NULL UNIQUE, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ) `); const API_ENDPOINT = `${config.FORUM_BASE_URL}/api/recent`; interface ForumTopic { title: string; slug: string; } async function checkForNewTopics() { const response = await fetch(API_ENDPOINT); const data = await response.json(); const topics = data.topics as ForumTopic[]; const stmt = db.prepare("SELECT * FROM topics WHERE slug = ?"); for (const topic of topics) { const result = await stmt.get(topic.slug); if (!result) { await db.prepare("INSERT INTO topics (title, slug) VALUES (?, ?)").run(topic.title, topic.slug); const topicUrl = `${config.FORUM_BASE_URL}/topic/${topic.slug}`; const message = `New forum post: ${topic.title}`; await sendMessage(message); } } } // Run every 5 minutes setInterval(checkForNewTopics, 5 * 60 * 1000); await checkForNewTopics(); // Run once immediately await db.close();