-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.js
346 lines (288 loc) · 9.54 KB
/
library.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* eslint-disable no-await-in-loop */
/* eslint-disable no-continue */
'use strict';
const winston = require.main.require('winston');
const nconf = require.main.require('nconf');
const dns = require('dns');
const { getLinkPreview } = require('link-preview-js');
const { load } = require('cheerio');
const { isURL } = require('validator');
const meta = require.main.require('./src/meta');
const cache = require.main.require('./src/cache');
const posts = require.main.require('./src/posts');
const postsCache = require.main.require('./src/posts/cache');
const websockets = require.main.require('./src/socket.io');
const controllers = require('./lib/controllers');
const routeHelpers = require.main.require('./src/routes/helpers');
const plugin = {};
plugin.init = async (params) => {
const { router /* , middleware , controllers */ } = params;
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/link-preview', [], controllers.renderAdminPage);
};
plugin.applyDefaults = async (data) => {
const { plugin, values } = data;
if (plugin === 'link-preview') {
['embedHtml', 'embedImage', 'embedAudio', 'embedVideo'].forEach((prop) => {
if (!values.hasOwnProperty(prop)) {
values[prop] = 'on';
}
});
}
return data;
};
async function preview(url) {
return getLinkPreview(url, {
resolveDNSHost: async url => new Promise((resolve, reject) => {
const { hostname } = new URL(url);
dns.lookup(hostname, (err, address) => {
if (err) {
reject(err);
return;
}
resolve(address); // if address resolves to localhost or '127.0.0.1' library will throw an error
});
}),
followRedirects: `manual`,
handleRedirects: (baseURL, forwardedURL) => {
const urlObj = new URL(baseURL);
const forwardedURLObj = new URL(forwardedURL);
if (
forwardedURLObj.hostname === urlObj.hostname ||
forwardedURLObj.hostname === `www.${urlObj.hostname}` ||
`www.${forwardedURLObj.hostname}` === urlObj.hostname
) {
return true;
}
return false;
},
}).then((preview) => {
// winston.verbose(`[link-preview] ${preview.url} (${preview.contentType}, cache: miss)`);
cache.set(`link-preview:${url}`, preview);
return preview;
}).catch(() => {
// winston.verbose(`[link-preview] ${url} (invalid, cache: miss)`);
cache.set(`link-preview:${url}`, { url });
});
}
async function process(content, { type, pid, tid, attachments }) {
const inlineTypes = ['default', 'activitypub.article'];
const processInline = inlineTypes.includes(type);
const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
if (![embedHtml, embedImage, embedAudio, embedVideo].some(prop => prop === 'on')) {
return content;
}
const requests = new Map();
if (pid && Array.isArray(attachments) && attachments.length) {
const attachmentData = await posts.attachments.getAttachments(attachments);
attachmentData.filter(Boolean).forEach(({ url, _type }) => {
const type = _type || 'attachment';
requests.set(url, { type });
});
}
// Parse inline urls
const $ = load(content, null, false);
for (const anchor of $('a')) {
const $anchor = $(anchor);
// Skip if the anchor has link text, or has text on the same line.
let url = $anchor.attr('href');
const text = $anchor.text();
const hasSiblings = !!anchor.prev || !!anchor.next;
if (hasSiblings || url !== text || anchor.parent.name !== 'p') {
continue;
}
// Handle relative URLs
if (!url.startsWith('http')) {
url = `${nconf.get('url')}${url.startsWith('/') ? url : `/${url}`}`;
}
if (processInline) {
const special = await handleSpecialEmbed(url, $anchor);
if (special) {
requests.delete(url);
continue;
}
}
// Inline url takes precedence over attachment
requests.set(url, {
type: 'inline',
target: $anchor,
});
}
// Render cache hits immediately
let attachmentHtml = '';
let placeholderHtml = '';
const cold = new Set();
await Promise.all(Array.from(requests.keys()).map(async (url) => {
const options = requests.get(url);
const cached = cache.get(`link-preview:${url}`);
if (cached) {
const html = await render(cached);
if (html) {
switch (options.type) {
case 'inline': {
if (processInline) {
const $anchor = options.target;
$anchor.replaceWith($(html));
}
break;
}
case 'attachment': {
attachmentHtml += html;
break;
}
}
}
} else {
if (options.type === 'attachment') {
placeholderHtml += `<p><a href="${url}" rel="nofollow ugc">${url}</a></p>`;
}
cold.add(url);
}
}));
// Start preview for cache misses, but continue for now so as to not block response
if (cold.size) {
const coldArr = Array.from(cold);
Promise.all(coldArr.map(preview)).then(async (previews) => {
await Promise.all(previews.map(async (preview, idx) => {
if (!preview) {
return;
}
const url = coldArr[idx];
const options = requests.get(url);
const parsedUrl = new URL(url);
preview.hostname = parsedUrl.hostname;
const html = await render(preview);
if (html) {
switch (options.type) {
case 'inline': {
if (processInline) {
const $anchor = options.target;
$anchor.replaceWith($(html));
}
break;
}
case 'attachment': {
attachmentHtml += html;
break;
}
}
}
}));
let content = $.html();
content += attachmentHtml ? `\n\n<div class="row">${attachmentHtml}</div>` : '';
// bust posts cache item
if (pid) {
const cache = postsCache.getOrCreate();
const cacheKey = `${String(pid)}|default`;
cache.set(cacheKey, content);
// fire post edit event with mocked data
if (tid) {
websockets.in(`topic_${tid}`).emit('event:post_edited', {
post: {
tid,
pid,
changed: true,
content,
},
topic: {},
});
}
}
});
}
content = $.html();
content += attachmentHtml ? `\n\n<div class="row"><div class="col-12">${attachmentHtml}</div></div>` : '';
content += placeholderHtml ? `\n\n<div class="row"><div class="col-12">${placeholderHtml}</div></div>` : '';
return content;
}
async function render(preview) {
const { app } = require.main.require('./src/webserver');
const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
// winston.verbose(`[link-preview] ${preview.url} (${preview.contentType || 'invalid'}, cache: hit)`);
if (!preview.contentType) {
return false;
}
if (embedHtml && preview.contentType.startsWith('text/html')) {
return await app.renderAsync('partials/link-preview/html', preview);
}
if (embedImage && preview.contentType.startsWith('image/')) {
return await app.renderAsync('partials/link-preview/image', preview);
}
if (embedAudio && preview.contentType.startsWith('audio/')) {
return await app.renderAsync('partials/link-preview/audio', preview);
}
if (embedVideo && preview.contentType.startsWith('video/')) {
return await app.renderAsync('partials/link-preview/video', preview);
}
return false;
}
async function handleSpecialEmbed(url, $anchor) {
const { app } = require.main.require('./src/webserver');
const { hostname, searchParams, pathname } = new URL(url);
const { embedYoutube, embedVimeo, embedTiktok } = await meta.settings.get('link-preview');
if (embedYoutube === 'on' && ['youtube.com', 'www.youtube.com', 'youtu.be'].some(x => hostname === x)) {
let video;
let short = false;
if (hostname === 'youtu.be') {
video = pathname.slice(1);
} else if (pathname.startsWith('/shorts')) {
video = pathname.split('/')[2];
short = true;
} else if (pathname.startsWith('/live')) {
video = pathname.split('/')[2];
} else {
video = searchParams.get('v');
}
const html = await app.renderAsync(short ? 'partials/link-preview/youtube-short' : 'partials/link-preview/youtube', { video });
$anchor.replaceWith(html);
return true;
}
if (embedVimeo === 'on' && hostname === 'vimeo.com') {
const video = pathname.slice(1);
const html = await app.renderAsync('partials/link-preview/vimeo', { video });
$anchor.replaceWith(html);
return true;
}
if (embedTiktok === 'on' && ['tiktok.com', 'www.tiktok.com'].some(x => hostname === x)) {
const video = pathname.split('/')[3];
const html = await app.renderAsync('partials/link-preview/tiktok', { video });
$anchor.replaceWith(html);
return true;
}
return false;
}
plugin.onParse = async (payload) => {
if (typeof payload === 'string') { // raw
const type = 'default';
payload = await process(payload, { type });
} else if (payload && payload.type !== 'plaintext' && payload.postData && payload.postData.content) { // post
const { content, pid, tid, attachments } = payload.postData;
const { type } = payload;
payload.postData.content = await process(content, { type, pid, tid, attachments });
}
return payload;
};
plugin.onPost = async ({ post }) => {
if (post.hasOwnProperty('_activitypub')) {
return; // no attachment parsing for content from activitypub; attachments saved via notes.assert
}
// Only match standalone URLs on their own line
const lines = post.content.split('\n');
const urls = lines.filter(line => isURL(line));
let previews = await Promise.all(urls.map(async url => await preview(url)));
previews = previews.filter(Boolean);
previews = previews.map(({ url, contentType: mediaType }) => ({
type: 'inline',
url,
mediaType,
})).filter(Boolean);
posts.attachments.update(post.pid, previews);
};
plugin.addAdminNavigation = (header) => {
header.plugins.push({
route: '/plugins/link-preview',
icon: 'fa-tint',
name: 'Link Preview',
});
return header;
};
module.exports = plugin;