* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
130 lines
4.7 KiB
JavaScript
130 lines
4.7 KiB
JavaScript
const got = require('@/utils/got');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
|
|
const allowSiteList = ['mastodon.social', 'pawoo.net'];
|
|
|
|
const mediaParse = (media_attachments) =>
|
|
media_attachments
|
|
.map((item) => {
|
|
const selectedUrl = item.remote_url ?? item.url;
|
|
const description = item.description ?? '';
|
|
switch (item.type) {
|
|
case 'gifv':
|
|
return `<br><video src="${selectedUrl}" autoplay loop>gif ${description}</video>`;
|
|
case 'video':
|
|
return `<br><video src="${selectedUrl}" controls loop>video ${description}</video>`;
|
|
case 'image':
|
|
return `<br><img src="${selectedUrl}" alt="image ${description}">`;
|
|
case 'audio':
|
|
return `<br><audio controls src="${selectedUrl}">audio ${description}</audio>`;
|
|
case 'unknown':
|
|
default:
|
|
return `<br><a href="${selectedUrl}">media ${description}</a>`;
|
|
}
|
|
})
|
|
.join('');
|
|
|
|
const parseStatuses = (data) =>
|
|
data.map((item) => {
|
|
// docs on: https://docs.joinmastodon.org/entities/status/
|
|
|
|
const accountRepostedBy = item.reblog ? item.account : null;
|
|
item = item.reblog ?? item;
|
|
|
|
const content = item.content ? item.content.replaceAll(/<span.*?>|<\/span.*?>/gm, '') : '';
|
|
const contentRemovedHtml = content.replaceAll(/<(?:.|\n)*?>/gm, '\n');
|
|
|
|
const author = `${item.account.display_name} (@${item.account.acct})`;
|
|
const link = item.url;
|
|
const media = mediaParse(item.media_attachments);
|
|
|
|
const titleAuthor = accountRepostedBy ? `Re @${accountRepostedBy.username}` : `@${item.account.username}`;
|
|
const titleText = item.sensitive === true ? `(CW) ${item.spoiler_text}` : contentRemovedHtml;
|
|
const title = `${titleAuthor}: "${titleText}"`;
|
|
|
|
return {
|
|
title,
|
|
author,
|
|
description: item.spoiler_text + '<hr />' + content + media,
|
|
pubDate: parseDate(item.created_at),
|
|
link,
|
|
guid: item.uri,
|
|
};
|
|
});
|
|
|
|
async function getAccountStatuses(site, account_id, only_media) {
|
|
const statuses_url = `https://${site}/api/v1/accounts/${account_id}/statuses?only_media=${only_media}`;
|
|
const statuses_response = await got({
|
|
method: 'get',
|
|
url: statuses_url,
|
|
});
|
|
const data = statuses_response.data;
|
|
|
|
let account_data;
|
|
if (data.length !== 0 && data[0].account !== null) {
|
|
account_data = data[0].account;
|
|
} else {
|
|
const account_url = `https://${site}/api/v1/accounts/${account_id}`;
|
|
const account_response = await got({
|
|
method: 'get',
|
|
url: account_url,
|
|
});
|
|
account_data = account_response.data;
|
|
}
|
|
|
|
return { account_data, data };
|
|
}
|
|
|
|
async function getAccountIdByAcct(acct, ctx) {
|
|
const config = require('@/config').value;
|
|
const mastodonConfig = config.mastodon;
|
|
|
|
// acctHost is from the acct param of the request, and acctDomain is from either acctHost or the config
|
|
const acctHost = acct.split('@').filter(Boolean)[1];
|
|
const site = mastodonConfig.apiHost || acctHost;
|
|
const acctDomain = mastodonConfig.acctDomain || acctHost;
|
|
if (!(site && acctDomain)) {
|
|
throw new Error('Mastodon RSS is disabled due to the lack of <a href="https://docs.rsshub.app/en/install/#configuration-route-specific-configurations">relevant config</a>');
|
|
}
|
|
|
|
const search_url = `https://${site}/api/v2/search`;
|
|
const cacheUid = `mastodon_acct_id/${site}/${acct}`;
|
|
|
|
const account_id = await ctx.cache.tryGet(cacheUid, async () => {
|
|
const search_response = await got({
|
|
method: 'get',
|
|
url: search_url,
|
|
headers: {
|
|
...(mastodonConfig.accessToken ? { Authorization: `Bearer ${mastodonConfig.accessToken}` } : {}),
|
|
},
|
|
searchParams: {
|
|
q: acct,
|
|
type: 'accounts',
|
|
},
|
|
});
|
|
const [acctUser, acctHost] = acct.split('@').filter(Boolean);
|
|
let acctOnServer;
|
|
|
|
if (acctHost) {
|
|
acctOnServer = acctHost === acctDomain ? acctUser : acctUser + '@' + acctHost;
|
|
} else {
|
|
acctOnServer = acctUser;
|
|
}
|
|
|
|
const accountData = search_response.data.accounts.filter((item) => item.acct === acctOnServer);
|
|
|
|
if (accountData.length === 0) {
|
|
throw `acct ${acct} not found`;
|
|
}
|
|
return accountData[0].id;
|
|
});
|
|
return { site, account_id };
|
|
}
|
|
|
|
module.exports = {
|
|
parseStatuses,
|
|
getAccountStatuses,
|
|
getAccountIdByAcct,
|
|
allowSiteList,
|
|
};
|