fedi urlreply fixes

This commit is contained in:
Evert Prants 2023-08-14 17:08:05 +03:00
parent fdc9494fbd
commit ea2d033978
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 56 additions and 17 deletions

View File

@ -27,7 +27,7 @@
}, },
{ {
"name": "url-fediverse", "name": "url-fediverse",
"version": "1.0.2" "version": "1.0.3"
}, },
{ {
"name": "url-peertube", "name": "url-peertube",

View File

@ -2,7 +2,7 @@
"main": "plugin.js", "main": "plugin.js",
"name": "url-fediverse", "name": "url-fediverse",
"description": "URLReply Fediverse", "description": "URLReply Fediverse",
"version": "1.0.2", "version": "1.0.3",
"tags": ["urlreply"], "tags": ["urlreply"],
"dependencies": ["urlreply"], "dependencies": ["urlreply"],
"npmDependencies": [] "npmDependencies": []

View File

@ -2,17 +2,26 @@ import { httpGET, sanitizeEscapedText } from '@squeebot/core/lib/common';
import { import {
Plugin, Plugin,
EventListener, EventListener,
DependencyLoad DependencyLoad,
} from '@squeebot/core/lib/plugin'; } 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<boolean> { async function mastodonResponse(
msg: IMessage,
url: string,
type = 'Mastodon',
limit = true
): Promise<boolean> {
let murl; let murl;
if (type === 'Mastodon') { 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') { } 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) { if (!murl) {
return false; return false;
@ -42,23 +51,38 @@ async function mastodonResponse(msg: IMessage, url: string, type = 'Mastodon'):
} }
const keys = []; const keys = [];
let end = sanitizeEscapedText(content.replace(/<\/p>/g, '\n') // Add newlines to paragraph endings let end = sanitizeEscapedText(
content
.replace(/<\/p>/g, '\n') // Add newlines to paragraph endings
.replace(/<br \/>/g, '\n') // Add newlines instead of <br /> .replace(/<br \/>/g, '\n') // Add newlines instead of <br />
.replace(/(<([^>]+)>)/ig, '')); // Strip the rest of the HTML out .replace(/(<([^>]+)>)/gi, '')
); // Strip the rest of the HTML out
if (end.length > 220) { if (end.length > 220 && limit) {
end = end.substring(0, 220) + '…'; end = end.substring(0, 220) + '…';
} }
keys.push(['field', type, { color: 'cyan', type: 'title' }]); keys.push(['field', type, { color: 'cyan', type: 'title' }]);
keys.push(['field', data.favourites_count.toString(), { color: 'red', label: ['★', 'Favourites'], type: 'metric' }]); keys.push([
keys.push(['field', data.reblogs_count.toString(), { color: 'green', label: ['↱↲', 'Reblogs'], type: 'metric' }]); '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(['bold', '@' + data.account.acct + ':']);
keys.push(['field', end, { type: 'content' }]); keys.push(['field', end, { type: 'content' }]);
if (data.media_attachments?.length) { if (data.media_attachments?.length) {
const amount = 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); msg.resolve(keys);
@ -78,7 +102,12 @@ class FediResponsePlugin extends Plugin {
urlreply.registerHandler( urlreply.registerHandler(
this.name, this.name,
'html/mastodon', 'html/mastodon',
async (url: string, msg: IMessage, title: string, body: any): Promise<boolean> => { async (
url: string,
msg: IMessage,
title: string,
body: any
): Promise<boolean> => {
const type = url.match('/notice/') ? 'Pleroma' : 'Mastodon'; const type = url.match('/notice/') ? 'Pleroma' : 'Mastodon';
let pass = false; let pass = false;
@ -86,13 +115,23 @@ class FediResponsePlugin extends Plugin {
pass = true; pass = true;
} else { } else {
const tag = body('a[href="https://joinmastodon.org/"]'); 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; pass = true;
} }
} }
if (pass) { 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) { if (mastodonTest) {
return true; return true;
} }