mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 08:20:36 +01:00
refactor: use topics.create internal method instead of directly setting topic hash, handle incoming tags for OP
This commit is contained in:
@@ -81,17 +81,11 @@ Mocks.post = async (objects) => {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
id: pid,
|
id: pid,
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
attributedTo: uid,
|
attributedTo: uid,
|
||||||
// conversation,
|
|
||||||
name,
|
|
||||||
content,
|
|
||||||
sourceContent,
|
|
||||||
inReplyTo: toPid,
|
inReplyTo: toPid,
|
||||||
to,
|
published, updated, name, content, sourceContent,
|
||||||
cc,
|
to, cc, attachment, tag,
|
||||||
attachment,
|
// conversation, // mastodon-specific, ignored.
|
||||||
} = object;
|
} = object;
|
||||||
|
|
||||||
const timestamp = new Date(published).getTime();
|
const timestamp = new Date(published).getTime();
|
||||||
@@ -110,7 +104,7 @@ Mocks.post = async (objects) => {
|
|||||||
|
|
||||||
edited,
|
edited,
|
||||||
editor: edited ? uid : undefined,
|
editor: edited ? uid : undefined,
|
||||||
_activitypub: { to, cc, attachment },
|
_activitypub: { to, cc, attachment, tag },
|
||||||
};
|
};
|
||||||
|
|
||||||
return payload;
|
return payload;
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ const user = require('../user');
|
|||||||
const topics = require('../topics');
|
const topics = require('../topics');
|
||||||
const posts = require('../posts');
|
const posts = require('../posts');
|
||||||
const utils = require('../utils');
|
const utils = require('../utils');
|
||||||
// const pubsub = require('../pubsub');
|
|
||||||
const slugify = require('../slugify');
|
|
||||||
|
|
||||||
const activitypub = module.parent.exports;
|
const activitypub = module.parent.exports;
|
||||||
const Notes = module.exports;
|
const Notes = module.exports;
|
||||||
@@ -204,20 +202,22 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { pid: mainPid, tid, uid: authorId, timestamp, name, content } = chain[chain.length - 1];
|
const mainPost = chain[chain.length - 1];
|
||||||
|
let { pid: mainPid, tid, uid: authorId, timestamp, name, content } = mainPost;
|
||||||
const hasTid = !!tid;
|
const hasTid = !!tid;
|
||||||
|
|
||||||
const members = await db.isSortedSetMembers(`tid:${tid}:posts`, chain.map(p => p.pid));
|
const members = await db.isSortedSetMembers(`tid:${tid}:posts`, chain.slice(0, -1).map(p => p.pid));
|
||||||
|
members.push(await posts.exists(mainPid));
|
||||||
if (tid && members.every(Boolean)) {
|
if (tid && members.every(Boolean)) {
|
||||||
// All cached, return early.
|
// All cached, return early.
|
||||||
winston.info('[notes/assertTopic] No new notes to process.');
|
winston.verbose('[notes/assertTopic] No new notes to process.');
|
||||||
return tid;
|
return tid;
|
||||||
}
|
}
|
||||||
|
|
||||||
let cid;
|
let cid;
|
||||||
let title;
|
let title;
|
||||||
if (hasTid) {
|
if (hasTid) {
|
||||||
({ cid, title, mainPid } = await topics.getTopicFields(tid, ['cid', 'title', 'mainPid']));
|
({ cid, title, mainPid } = await topics.getTopicFields(tid, ['tid', 'cid', 'title', 'mainPid']));
|
||||||
} else {
|
} else {
|
||||||
// mainPid ok to leave as-is
|
// mainPid ok to leave as-is
|
||||||
cid = -1;
|
cid = -1;
|
||||||
@@ -236,9 +236,10 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tid = tid || utils.generateUUID();
|
tid = tid || utils.generateUUID();
|
||||||
|
mainPost.tid = tid;
|
||||||
|
|
||||||
const unprocessed = chain.filter((p, idx) => !members[idx]);
|
const unprocessed = chain.filter((p, idx) => !members[idx]);
|
||||||
winston.info(`[notes/assertTopic] ${unprocessed.length} new note(s) found.`);
|
winston.verbose(`[notes/assertTopic] ${unprocessed.length} new note(s) found.`);
|
||||||
|
|
||||||
const [ids, timestamps] = [
|
const [ids, timestamps] = [
|
||||||
unprocessed.map(n => (utils.isNumber(n.pid) ? parseInt(n.pid, 10) : n.pid)),
|
unprocessed.map(n => (utils.isNumber(n.pid) ? parseInt(n.pid, 10) : n.pid)),
|
||||||
@@ -252,16 +253,24 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
timestamps.splice(idx, 1);
|
timestamps.splice(idx, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
let tags;
|
||||||
!hasTid ? db.setObject(`topic:${tid}`, {
|
if (!hasTid) {
|
||||||
|
tags = (mainPost._activitypub.tag || []).map(o => o.name.slice(1));
|
||||||
|
tags = await topics.filterTags(tags, cid);
|
||||||
|
|
||||||
|
await topics.create({
|
||||||
tid,
|
tid,
|
||||||
uid: authorId,
|
uid: authorId,
|
||||||
cid: cid,
|
cid,
|
||||||
mainPid,
|
mainPid,
|
||||||
title,
|
title,
|
||||||
slug: `${tid}/${slugify(title)}`,
|
|
||||||
timestamp,
|
timestamp,
|
||||||
}) : null,
|
tags,
|
||||||
|
});
|
||||||
|
topics.onNewPostMade(mainPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids),
|
db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids),
|
||||||
Notes.assert(uid, unprocessed),
|
Notes.assert(uid, unprocessed),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ module.exports = function (Topics) {
|
|||||||
// This is an internal method, consider using Topics.post instead
|
// This is an internal method, consider using Topics.post instead
|
||||||
const timestamp = data.timestamp || Date.now();
|
const timestamp = data.timestamp || Date.now();
|
||||||
|
|
||||||
const tid = await db.incrObjectField('global', 'nextTid');
|
const tid = data.tid || await db.incrObjectField('global', 'nextTid');
|
||||||
|
|
||||||
let topicData = {
|
let topicData = {
|
||||||
tid: tid,
|
tid: tid,
|
||||||
|
|||||||
Reference in New Issue
Block a user