fix: template helper

This commit is contained in:
Julian Lam
2024-03-12 13:27:29 -04:00
parent 4b2495b5aa
commit 52c143aa03
5 changed files with 56 additions and 36 deletions

View File

@@ -205,7 +205,6 @@ Mocks.note = async (post) => {
const id = `${nconf.get('url')}/post/${post.pid}`; const id = `${nconf.get('url')}/post/${post.pid}`;
const published = new Date(parseInt(post.timestamp, 10)).toISOString(); const published = new Date(parseInt(post.timestamp, 10)).toISOString();
const raw = await posts.getPostField(post.pid, 'content');
// todo: post visibility // todo: post visibility
const to = new Set([activitypub._constants.publicAddress]); const to = new Set([activitypub._constants.publicAddress]);
@@ -230,7 +229,18 @@ Mocks.note = async (post) => {
})); }));
} }
const mentionsEnabled = await plugins.isActive('nodebb-plugin-mentions'); let source = null;
const [markdownEnabled, mentionsEnabled] = await Promise.all([
plugins.isActive('nodebb-plugin-markdown'),
plugins.isActive('nodebb-plugin-mentions'),
]);
if (markdownEnabled) {
const raw = await posts.getPostField(post.pid, 'content');
source = {
content: raw,
mediaType: 'text/markdown',
};
}
if (mentionsEnabled) { if (mentionsEnabled) {
const mentions = require.main.require('nodebb-plugin-mentions'); const mentions = require.main.require('nodebb-plugin-mentions');
const matches = await mentions.getMatches(post.content); const matches = await mentions.getMatches(post.content);
@@ -278,10 +288,7 @@ Mocks.note = async (post) => {
summary: null, summary: null,
name, name,
content: post.content, content: post.content,
source: { source,
content: raw,
mediaType: 'text/markdown',
},
tag, tag,
attachment: [], // todo... requires refactoring of link preview plugin attachment: [], // todo... requires refactoring of link preview plugin
// replies: {} todo... // replies: {} todo...

View File

@@ -230,13 +230,13 @@ Notes.assertTopic = async (uid, id) => {
let cid; let cid;
let title; let title;
if (hasTid) { if (hasTid) {
({ cid, title, mainPid } = await topics.getTopicFields(tid, ['tid', 'cid', 'title', 'mainPid'])); ({ cid, mainPid } = await topics.getTopicFields(tid, ['tid', 'cid', 'mainPid']));
} else { } else {
// mainPid ok to leave as-is // mainPid ok to leave as-is
cid = -1; cid = -1;
title = name || utils.decodeHTMLEntities(utils.stripHTMLTags(content)); title = name || utils.decodeHTMLEntities(utils.stripHTMLTags(content));
if (title.length > meta.config.maximumTitleLength) { if (title.length > meta.config.maximumTitleLength) {
title = `${title.slice(0, meta.config.maximumTitleLength)}...`; title = `${title.slice(0, meta.config.maximumTitleLength - 3)}...`;
} }
} }
mainPid = utils.isNumber(mainPid) ? parseInt(mainPid, 10) : mainPid; mainPid = utils.isNumber(mainPid) ? parseInt(mainPid, 10) : mainPid;
@@ -274,31 +274,43 @@ Notes.assertTopic = async (uid, id) => {
tags = (mainPost._activitypub.tag || []) tags = (mainPost._activitypub.tag || [])
.filter(o => o.type === 'Hashtag') .filter(o => o.type === 'Hashtag')
.map(o => o.name.slice(1)); .map(o => o.name.slice(1));
tags = await topics.filterTags(tags, cid);
await topics.create({ try {
tid, await topics.post({
uid: authorId, tid,
cid, uid: authorId,
mainPid, cid,
title, pid: mainPid,
timestamp, title,
tags, timestamp,
}); tags,
topics.onNewPostMade(mainPost); content: mainPost.content,
_activitypub: mainPost._activitypub,
});
} catch (e) {
console.log(e);
}
unprocessed.pop();
} }
await Promise.all([ unprocessed.reverse();
db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids), for (const post of unprocessed) {
Notes.assert(uid, unprocessed), // eslint-disable-next-line no-await-in-loop
]); await topics.reply(post);
await Promise.all([ // must be done after .assert() }
Notes.assertParentChain(chain),
Notes.updateTopicCounts(tid), await Notes.syncUserInboxes(tid);
Notes.syncUserInboxes(tid), // await Promise.all([
topics.updateLastPostTimeFromLastPid(tid), // db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids),
topics.updateTeaser(tid), // Notes.assert(uid, unprocessed),
]); // ]);
// await Promise.all([ // must be done after .assert()
// Notes.assertParentChain(chain),
// Notes.updateTopicCounts(tid),
// Notes.syncUserInboxes(tid),
// topics.updateLastPostTimeFromLastPid(tid),
// topics.updateTeaser(tid),
// ]);
return tid; return tid;
}; };

View File

@@ -10,12 +10,12 @@ const topics = require('../topics');
const categories = require('../categories'); const categories = require('../categories');
const groups = require('../groups'); const groups = require('../groups');
const privileges = require('../privileges'); const privileges = require('../privileges');
const utils = require('../utils');
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.create = async function (data) { Posts.create = async function (data) {
// This is an internal method, consider using Topics.reply instead // This is an internal method, consider using Topics.reply instead
const { uid } = data; const { uid, tid, _activitypub } = data;
const { tid } = data;
const content = data.content.toString(); const content = data.content.toString();
const timestamp = data.timestamp || Date.now(); const timestamp = data.timestamp || Date.now();
const isMain = data.isMain || false; const isMain = data.isMain || false;
@@ -28,13 +28,14 @@ module.exports = function (Posts) {
await checkToPid(data.toPid, uid); await checkToPid(data.toPid, uid);
} }
const pid = await db.incrObjectField('global', 'nextPid'); const pid = data.pid || await db.incrObjectField('global', 'nextPid');
let postData = { let postData = {
pid: pid, pid: pid,
uid: uid, uid: uid,
tid: tid, tid: tid,
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
_activitypub,
}; };
if (data.toPid) { if (data.toPid) {
@@ -56,7 +57,7 @@ module.exports = function (Posts) {
await Promise.all([ await Promise.all([
db.sortedSetAdd('posts:pid', timestamp, postData.pid), db.sortedSetAdd('posts:pid', timestamp, postData.pid),
db.incrObjectField('global', 'postCount'), utils.isNumber(pid) ? db.incrObjectField('global', 'postCount') : null,
user.onNewPostMade(postData), user.onNewPostMade(postData),
topics.onNewPostMade(postData), topics.onNewPostMade(postData),
categories.onNewPostMade(topicData.cid, topicData.pinned, postData), categories.onNewPostMade(topicData.cid, topicData.pinned, postData),

View File

@@ -135,7 +135,7 @@ module.exports = function (Topics) {
throw new Error('[[error:no-topic]]'); throw new Error('[[error:no-topic]]');
} }
if (uid > 0 && settings.followTopicsOnCreate) { if (utils.isNumber(uid) && uid > 0 && settings.followTopicsOnCreate) {
await Topics.follow(postData.tid, uid); await Topics.follow(postData.tid, uid);
} }
const topicData = topics[0]; const topicData = topics[0];

View File

@@ -151,7 +151,7 @@ describe('helpers', () => {
find: 'viewing', find: 'viewing',
read: 'viewing', read: 'viewing',
}; };
const html = helpers.spawnPrivilegeStates('guests', privs, types); const html = helpers.spawnPrivilegeStates(1, 'guests', privs, types);
assert.equal(html, ` assert.equal(html, `
<td data-privilege="find" data-value="true" data-type="viewing"> <td data-privilege="find" data-value="true" data-type="viewing">
<div class="form-check text-center"> <div class="form-check text-center">