Skip to content

Commit 17f2055

Browse files
committed
feat: utilise new parsePost types for better handling for federating out posts
1 parent 4b6a5da commit 17f2055

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

library.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ async function preview(url) {
8282
});
8383
}
8484

85-
async function process(content, opts) {
85+
async function process(content, { type, pid, tid }) {
86+
const inlineTypes = ['default', 'activitypub.article'];
87+
const processInline = inlineTypes.includes(type);
8688
const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
8789
if (![embedHtml, embedImage, embedAudio, embedVideo].some(prop => prop === 'on')) {
8890
return content;
@@ -91,8 +93,8 @@ async function process(content, opts) {
9193
const requests = new Map();
9294

9395
// Retrieve attachments
94-
if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
95-
const attachments = await posts.attachments.get(opts.pid);
96+
if (pid && await posts.exists(pid)) {
97+
const attachments = await posts.attachments.get(pid);
9698
attachments.forEach(({ url, _type }) => {
9799
const type = _type || 'attachment';
98100
requests.set(url, { type });
@@ -117,9 +119,11 @@ async function process(content, opts) {
117119
url = `${nconf.get('url')}${url.startsWith('/') ? url : `/${url}`}`;
118120
}
119121

120-
const special = await handleSpecialEmbed(url, $anchor);
121-
if (special) {
122-
continue;
122+
if (!processInline) {
123+
const special = await handleSpecialEmbed(url, $anchor);
124+
if (special) {
125+
continue;
126+
}
123127
}
124128

125129
// Inline url takes precedence over attachment
@@ -140,8 +144,10 @@ async function process(content, opts) {
140144
if (html) {
141145
switch (options.type) {
142146
case 'inline': {
143-
const $anchor = options.target;
144-
$anchor.replaceWith($(html));
147+
if (processInline) {
148+
const $anchor = options.target;
149+
$anchor.replaceWith($(html));
150+
}
145151
break;
146152
}
147153

@@ -174,8 +180,10 @@ async function process(content, opts) {
174180
if (html) {
175181
switch (options.type) {
176182
case 'inline': {
177-
const $anchor = options.target;
178-
$anchor.replaceWith($(html));
183+
if (processInline) {
184+
const $anchor = options.target;
185+
$anchor.replaceWith($(html));
186+
}
179187
break;
180188
}
181189

@@ -191,15 +199,15 @@ async function process(content, opts) {
191199
content += attachmentHtml ? `\n\n<div class="row">${attachmentHtml}</div>` : '';
192200

193201
// bust posts cache item
194-
if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
195-
postsCache.del(String(opts.pid));
202+
if (pid && await posts.exists(pid)) {
203+
posts.clearCachedPost(pid);
196204

197205
// 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', {
206+
if (tid && await topics.exists(tid)) {
207+
websockets.in(`topic_${tid}`).emit('event:post_edited', {
200208
post: {
201-
tid: opts.tid,
202-
pid: opts.pid,
209+
tid,
210+
pid,
203211
changed: true,
204212
content,
205213
},
@@ -289,10 +297,12 @@ async function handleSpecialEmbed(url, $anchor) {
289297

290298
plugin.onParse = async (payload) => {
291299
if (typeof payload === 'string') { // raw
292-
payload = await process(payload, {});
300+
const type = 'default';
301+
payload = await process(payload, { type });
293302
} else if (payload && payload.postData && payload.postData.content) { // post
294303
const { content, pid, tid } = payload.postData;
295-
payload.postData.content = await process(content, { pid, tid });
304+
const { type } = payload;
305+
payload.postData.content = await process(content, { type, pid, tid });
296306
}
297307

298308
return payload;

0 commit comments

Comments
 (0)