From 17c7f819acc75807067354b6e6e63dd81340bd7e Mon Sep 17 00:00:00 2001 From: vulet Date: Mon, 4 Sep 2023 17:56:31 +0800 Subject: [PATCH] feat(fediverse): add twitter crossblog [kakashi] chore(config): update maintained nitter instances --- commands/nitter.js | 14 ++++++++++---- config.example.js | 4 ++-- utils.js | 4 ++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/commands/nitter.js b/commands/nitter.js index 8412e6e..bec5c2f 100644 --- a/commands/nitter.js +++ b/commands/nitter.js @@ -63,19 +63,25 @@ const getInstance = (domain, config) => timeout: 10 * 1000, }); -const run = async (roomId, userInput) => { +const run = async (roomId, userInput, fedi) => { const cfg = config.nitter; const tweet = await matrix.utils.retryPromise(cfg.domains.redirect, domain => nitter(getInstance(domain, cfg), userInput)); - return matrixClient.sendHtmlNotice(roomId, ' ', card(tweet, cfg.check, userInput)); + const tweetCard = card(tweet, cfg.check, userInput); + return fedi ? axios({ + method: 'POST', + url: `${config.fediverse.domain}/api/v1/statuses`, + headers: { Authorization: `Bearer ${fediverse.auth.access_token}` }, + data: { status: tweetCard, content_type: 'text/html' } + }) : matrixClient.sendHtmlNotice(roomId, ' ', tweetCard); }; -exports.runQuery = async (roomId, event, userInput) => { +exports.runQuery = async (roomId, event, userInput, fedi) => { try { const url = new URL(userInput); const { redirect, original } = config.nitter.domains; if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw ''; if (!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw ''; - return await run(roomId, url.pathname); + return await run(roomId, url.pathname, fedi); } catch (e) { return matrixClient.sendHtmlNotice(roomId, 'Sad!', 'Sad!').catch(() => {}); } diff --git a/config.example.js b/config.example.js index 17f3fe3..53023ec 100644 --- a/config.example.js +++ b/config.example.js @@ -33,8 +33,8 @@ module.exports = { nitter: { userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', domains: { - redirect: [ 'nitter.fdn.fr', 'nitter.snopyta.org', 'nitter.net' ], - original: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com' ] + redirect: [ 'nitter.unixfox.eu', 'nitter.cz', 'nitter.privacydev.net', 'nitter.poast.org', 'nitter.fdn.fr', 'nitter.snopyta.org', 'nitter.net' ], + original: [ 'nitter.unixfox.eu', 'nitter.cz', 'nitter.privacydev.net', 'nitter.poast.org', 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com' ] }, check: '(✅)', }, diff --git a/utils.js b/utils.js index c633057..7ddf32f 100644 --- a/utils.js +++ b/utils.js @@ -64,6 +64,10 @@ const eventHandler = (args, roomId, command, event) => { }); command = 'post'; break; + case 'crossblog': + args.push(roomId, event, userInput, true); + command = 'nitter' + break; case 'proxy': case 'p': try { const url = new URL(userInput);