nodebb-plugin-link-preview 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library.js +28 -4
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* eslint-disable no-continue */
|
|
2
|
+
|
|
1
3
|
'use strict';
|
|
2
4
|
|
|
3
5
|
const winston = require.main.require('winston');
|
|
@@ -9,6 +11,8 @@ const { load } = require('cheerio');
|
|
|
9
11
|
const meta = require.main.require('./src/meta');
|
|
10
12
|
const cache = require.main.require('./src/cache');
|
|
11
13
|
const posts = require.main.require('./src/posts');
|
|
14
|
+
const topics = require.main.require('./src/topics');
|
|
15
|
+
const websockets = require.main.require('./src/socket.io');
|
|
12
16
|
const postsCache = require.main.require('./src/posts/cache');
|
|
13
17
|
|
|
14
18
|
const controllers = require('./lib/controllers');
|
|
@@ -54,9 +58,11 @@ async function process(content, opts) {
|
|
|
54
58
|
|
|
55
59
|
const cached = cache.get(`link-preview:${url}`);
|
|
56
60
|
if (cached) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
if (cached.contentType) {
|
|
62
|
+
// eslint-disable-next-line no-await-in-loop
|
|
63
|
+
$anchor.replaceWith($(await render(cached)));
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
// Generate the preview, but return false for now so as to not block response
|
|
@@ -93,8 +99,23 @@ async function process(content, opts) {
|
|
|
93
99
|
winston.verbose(`[link-preview] ${preview.url} (${preview.contentType}, cache: miss)`);
|
|
94
100
|
cache.set(`link-preview:${url}`, preview);
|
|
95
101
|
|
|
102
|
+
// bust posts cache item
|
|
96
103
|
if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
|
|
97
104
|
postsCache.del(String(opts.pid));
|
|
105
|
+
|
|
106
|
+
// fire post edit event with mocked data
|
|
107
|
+
if (opts.hasOwnProperty('tid') && await topics.exists(opts.tid)) {
|
|
108
|
+
$anchor.replaceWith($(await render(preview)));
|
|
109
|
+
websockets.in(`topic_${opts.tid}`).emit('event:post_edited', {
|
|
110
|
+
post: {
|
|
111
|
+
tid: opts.tid,
|
|
112
|
+
pid: opts.pid,
|
|
113
|
+
changed: true,
|
|
114
|
+
content: $.html(),
|
|
115
|
+
},
|
|
116
|
+
topic: {},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
98
119
|
}
|
|
99
120
|
}).catch(() => {
|
|
100
121
|
winston.verbose(`[link-preview] ${url} (invalid, cache: miss)`);
|
|
@@ -138,7 +159,10 @@ plugin.onParse = async (payload) => {
|
|
|
138
159
|
if (typeof payload === 'string') { // raw
|
|
139
160
|
payload = await process(payload, {});
|
|
140
161
|
} else if (payload && payload.postData && payload.postData.content) { // post
|
|
141
|
-
payload.postData.content = await process(payload.postData.content, {
|
|
162
|
+
payload.postData.content = await process(payload.postData.content, {
|
|
163
|
+
pid: payload.postData.pid,
|
|
164
|
+
tid: payload.postData.tid,
|
|
165
|
+
});
|
|
142
166
|
}
|
|
143
167
|
|
|
144
168
|
return payload;
|