-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
utils.js
130 lines (111 loc) · 4.67 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { config } from '@/config';
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) {
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 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 new Error(`acct ${acct} not found`);
}
return accountData[0].id;
});
return { site, account_id };
}
module.exports = {
parseStatuses,
getAccountStatuses,
getAccountIdByAcct,
allowSiteList,
};