nodebb-plugin-link-preview 2.0.3 → 2.0.5

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.
Files changed (2) hide show
  1. package/library.js +30 -24
  2. package/package.json +1 -1
package/library.js CHANGED
@@ -14,9 +14,7 @@ const { isURL } = require('validator');
14
14
  const meta = require.main.require('./src/meta');
15
15
  const cache = require.main.require('./src/cache');
16
16
  const posts = require.main.require('./src/posts');
17
- const topics = require.main.require('./src/topics');
18
17
  const websockets = require.main.require('./src/socket.io');
19
- const postsCache = require.main.require('./src/posts/cache');
20
18
 
21
19
  const controllers = require('./lib/controllers');
22
20
 
@@ -82,18 +80,18 @@ async function preview(url) {
82
80
  });
83
81
  }
84
82
 
85
- async function process(content, opts) {
83
+ async function process(content, { type, pid, tid, attachments }) {
84
+ const inlineTypes = ['default', 'activitypub.article'];
85
+ const processInline = inlineTypes.includes(type);
86
86
  const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
87
87
  if (![embedHtml, embedImage, embedAudio, embedVideo].some(prop => prop === 'on')) {
88
88
  return content;
89
89
  }
90
90
 
91
91
  const requests = new Map();
92
-
93
- // Retrieve attachments
94
- if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
95
- const attachments = await posts.attachments.get(opts.pid);
96
- attachments.forEach(({ url, _type }) => {
92
+ if (pid && Array.isArray(attachments) && attachments.length) {
93
+ const attachmentData = await posts.attachments.getAttachments(attachments);
94
+ attachmentData.filter(Boolean).forEach(({ url, _type }) => {
97
95
  const type = _type || 'attachment';
98
96
  requests.set(url, { type });
99
97
  });
@@ -117,9 +115,11 @@ async function process(content, opts) {
117
115
  url = `${nconf.get('url')}${url.startsWith('/') ? url : `/${url}`}`;
118
116
  }
119
117
 
120
- const special = await handleSpecialEmbed(url, $anchor);
121
- if (special) {
122
- continue;
118
+ if (!processInline) {
119
+ const special = await handleSpecialEmbed(url, $anchor);
120
+ if (special) {
121
+ continue;
122
+ }
123
123
  }
124
124
 
125
125
  // Inline url takes precedence over attachment
@@ -140,8 +140,10 @@ async function process(content, opts) {
140
140
  if (html) {
141
141
  switch (options.type) {
142
142
  case 'inline': {
143
- const $anchor = options.target;
144
- $anchor.replaceWith($(html));
143
+ if (processInline) {
144
+ const $anchor = options.target;
145
+ $anchor.replaceWith($(html));
146
+ }
145
147
  break;
146
148
  }
147
149
 
@@ -174,8 +176,10 @@ async function process(content, opts) {
174
176
  if (html) {
175
177
  switch (options.type) {
176
178
  case 'inline': {
177
- const $anchor = options.target;
178
- $anchor.replaceWith($(html));
179
+ if (processInline) {
180
+ const $anchor = options.target;
181
+ $anchor.replaceWith($(html));
182
+ }
179
183
  break;
180
184
  }
181
185
 
@@ -191,15 +195,15 @@ async function process(content, opts) {
191
195
  content += attachmentHtml ? `\n\n<div class="row">${attachmentHtml}</div>` : '';
192
196
 
193
197
  // bust posts cache item
194
- if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
195
- postsCache.del(String(opts.pid));
198
+ if (pid) {
199
+ posts.clearCachedPost(pid);
196
200
 
197
201
  // fire post edit event with mocked data
198
- if (opts.hasOwnProperty('tid') && await topics.exists(opts.tid)) {
199
- websockets.in(`topic_${opts.tid}`).emit('event:post_edited', {
202
+ if (tid) {
203
+ websockets.in(`topic_${tid}`).emit('event:post_edited', {
200
204
  post: {
201
- tid: opts.tid,
202
- pid: opts.pid,
205
+ tid,
206
+ pid,
203
207
  changed: true,
204
208
  content,
205
209
  },
@@ -289,10 +293,12 @@ async function handleSpecialEmbed(url, $anchor) {
289
293
 
290
294
  plugin.onParse = async (payload) => {
291
295
  if (typeof payload === 'string') { // raw
292
- payload = await process(payload, {});
296
+ const type = 'default';
297
+ payload = await process(payload, { type });
293
298
  } else if (payload && payload.postData && payload.postData.content) { // post
294
- const { content, pid, tid } = payload.postData;
295
- payload.postData.content = await process(content, { pid, tid });
299
+ const { content, pid, tid, attachments } = payload.postData;
300
+ const { type } = payload;
301
+ payload.postData.content = await process(content, { type, pid, tid, attachments });
296
302
  }
297
303
 
298
304
  return payload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-link-preview",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {