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",
"version": "1.0.2"
"version": "1.0.3"
},
{
"name": "url-peertube",

View File

@ -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": []

View File

@ -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<boolean> {
async function mastodonResponse(
msg: IMessage,
url: string,
type = 'Mastodon',
limit = true
): Promise<boolean> {
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
let end = sanitizeEscapedText(
content
.replace(/<\/p>/g, '\n') // Add newlines to paragraph endings
.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) + '…';
}
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<boolean> => {
async (
url: string,
msg: IMessage,
title: string,
body: any
): Promise<boolean> => {
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;
}