From ea2d0339788076359806a948f7b6e8ba1c637479 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 14 Aug 2023 17:08:05 +0300 Subject: [PATCH] fedi urlreply fixes --- squeebot.repo.json | 2 +- url-fediverse/plugin.json | 2 +- url-fediverse/plugin.ts | 69 ++++++++++++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/squeebot.repo.json b/squeebot.repo.json index 00dcf45..5bd8458 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -27,7 +27,7 @@ }, { "name": "url-fediverse", - "version": "1.0.2" + "version": "1.0.3" }, { "name": "url-peertube", diff --git a/url-fediverse/plugin.json b/url-fediverse/plugin.json index ad9cfb4..c521606 100644 --- a/url-fediverse/plugin.json +++ b/url-fediverse/plugin.json @@ -2,7 +2,7 @@ "main": "plugin.js", "name": "url-fediverse", "description": "URLReply Fediverse", - "version": "1.0.2", + "version": "1.0.3", "tags": ["urlreply"], "dependencies": ["urlreply"], "npmDependencies": [] diff --git a/url-fediverse/plugin.ts b/url-fediverse/plugin.ts index 17fe359..edc368a 100644 --- a/url-fediverse/plugin.ts +++ b/url-fediverse/plugin.ts @@ -2,17 +2,26 @@ import { httpGET, sanitizeEscapedText } from '@squeebot/core/lib/common'; import { Plugin, EventListener, - DependencyLoad + DependencyLoad, } from '@squeebot/core/lib/plugin'; -import { IMessage } from '@squeebot/core/lib/types'; +import { IMessage, ProtocolFeatureFlag } from '@squeebot/core/lib/types'; -async function mastodonResponse(msg: IMessage, url: string, type = 'Mastodon'): Promise { +async function mastodonResponse( + msg: IMessage, + url: string, + type = 'Mastodon', + limit = true +): Promise { let murl; if (type === 'Mastodon') { - murl = url.match(/^(https?):\/\/((?:[\w\d-]+\.)*[\w\d-]+\.\w{2,16})\/(?:users\/)?@?([\w-_]+)\/(?:statuses\/)?(\d+[^&#?\s/])/i); + murl = url.match( + /^(https?):\/\/((?:[\w\d-]+\.)*[\w\d-]+\.\w{2,16})\/(?:users\/)?@?([\w-_]+)\/(?:statuses\/)?(\d+[^&#?\s/])/i + ); } else if (type === 'Pleroma') { - murl = url.match(/^(https?):\/\/((?:[\w\d-]+\.)*[\w\d-]+\.\w{2,16})\/(notice)\/([^&#?\s/]+)/i); + murl = url.match( + /^(https?):\/\/((?:[\w\d-]+\.)*[\w\d-]+\.\w{2,16})\/(notice)\/([^&#?\s/]+)/i + ); } if (!murl) { return false; @@ -42,23 +51,38 @@ async function mastodonResponse(msg: IMessage, url: string, type = 'Mastodon'): } const keys = []; - let end = sanitizeEscapedText(content.replace(/<\/p>/g, '\n') // Add newlines to paragraph endings - .replace(/
/g, '\n') // Add newlines instead of
- .replace(/(<([^>]+)>)/ig, '')); // Strip the rest of the HTML out + let end = sanitizeEscapedText( + content + .replace(/<\/p>/g, '\n') // Add newlines to paragraph endings + .replace(/
/g, '\n') // Add newlines instead of
+ .replace(/(<([^>]+)>)/gi, '') + ); // Strip the rest of the HTML out - if (end.length > 220) { + if (end.length > 220 && limit) { end = end.substring(0, 220) + '…'; } keys.push(['field', type, { color: 'cyan', type: 'title' }]); - keys.push(['field', data.favourites_count.toString(), { color: 'red', label: ['★', 'Favourites'], type: 'metric' }]); - keys.push(['field', data.reblogs_count.toString(), { color: 'green', label: ['↱↲', 'Reblogs'], type: 'metric' }]); + keys.push([ + 'field', + data.favourites_count.toString(), + { color: 'red', label: ['★', 'Favourites'], type: 'metric' }, + ]); + keys.push([ + 'field', + data.reblogs_count.toString(), + { color: 'green', label: ['↱↲', 'Reblogs'], type: 'metric' }, + ]); keys.push(['bold', '@' + data.account.acct + ':']); keys.push(['field', end, { type: 'content' }]); if (data.media_attachments?.length) { const amount = data.media_attachments.length; - keys.push(['field', `[${amount} attachment${amount !== 1 ? 's' : ''}]`, { color: 'brown' }]); + keys.push([ + 'field', + `[${amount} attachment${amount !== 1 ? 's' : ''}]`, + { color: 'brown' }, + ]); } msg.resolve(keys); @@ -78,7 +102,12 @@ class FediResponsePlugin extends Plugin { urlreply.registerHandler( this.name, 'html/mastodon', - async (url: string, msg: IMessage, title: string, body: any): Promise => { + async ( + url: string, + msg: IMessage, + title: string, + body: any + ): Promise => { const type = url.match('/notice/') ? 'Pleroma' : 'Mastodon'; let pass = false; @@ -86,13 +115,23 @@ class FediResponsePlugin extends Plugin { pass = true; } else { const tag = body('a[href="https://joinmastodon.org/"]'); - if (tag && tag.text() && tag.text().indexOf('Mastodon') !== -1) { + if ( + tag && + tag.text() && + ((tag.text() as string).includes('Mastodon') || + (tag.text() as string).includes('About')) + ) { pass = true; } } if (pass) { - const mastodonTest = await mastodonResponse(msg, url, type); + const mastodonTest = await mastodonResponse( + msg, + url, + type, + msg.source.supports(ProtocolFeatureFlag.SHORT_FORM_MESSAGING) + ); if (mastodonTest) { return true; }