Skip to content

Node.js based forum software built for the modern web

refactor: use topics.create internal method instead of directly setting topic hash, handle incoming tags for OP

Julian Lamf5a610797cfc
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js
index b86f3516fe..f99b32d8c9 100644
--- a/src/activitypub/mocks.js
+++ b/src/activitypub/mocks.js
@@ -81,17 +81,11 @@ Mocks.post = async (objects) => {
const {
id: pid,
- published,
- updated,
attributedTo: uid,
- // conversation,
- name,
- content,
- sourceContent,
inReplyTo: toPid,
- to,
- cc,
- attachment,
+ published, updated, name, content, sourceContent,
+ to, cc, attachment, tag,
+ // conversation, // mastodon-specific, ignored.
} = object;
const timestamp = new Date(published).getTime();
@@ -110,7 +104,7 @@ Mocks.post = async (objects) => {
edited,
editor: edited ? uid : undefined,
- _activitypub: { to, cc, attachment },
+ _activitypub: { to, cc, attachment, tag },
};
return payload;
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index f3cf0220d3..c2beef2055 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -10,8 +10,6 @@ const user = require('../user');
const topics = require('../topics');
const posts = require('../posts');
const utils = require('../utils');
-// const pubsub = require('../pubsub');
-const slugify = require('../slugify');
const activitypub = module.parent.exports;
const Notes = module.exports;
@@ -204,20 +202,22 @@ Notes.assertTopic = async (uid, id) => {
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 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)) {
// All cached, return early.
- winston.info('[notes/assertTopic] No new notes to process.');
+ winston.verbose('[notes/assertTopic] No new notes to process.');
return tid;
}
let cid;
let title;
if (hasTid) {
- ({ cid, title, mainPid } = await topics.getTopicFields(tid, ['cid', 'title', 'mainPid']));
+ ({ cid, title, mainPid } = await topics.getTopicFields(tid, ['tid', 'cid', 'title', 'mainPid']));
} else {
// mainPid ok to leave as-is
cid = -1;
@@ -236,9 +236,10 @@ Notes.assertTopic = async (uid, id) => {
}
tid = tid || utils.generateUUID();
+ mainPost.tid = tid;
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] = [
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);
}
- await Promise.all([
- !hasTid ? db.setObject(`topic:${tid}`, {
+ let tags;
+ if (!hasTid) {
+ tags = (mainPost._activitypub.tag || []).map(o => o.name.slice(1));
+ tags = await topics.filterTags(tags, cid);
+
+ await topics.create({
tid,
uid: authorId,
- cid: cid,
+ cid,
mainPid,
title,
- slug: `${tid}/${slugify(title)}`,
timestamp,
- }) : null,
+ tags,
+ });
+ topics.onNewPostMade(mainPost);
+ }
+
+ await Promise.all([
db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids),
Notes.assert(uid, unprocessed),
]);
diff --git a/src/topics/create.js b/src/topics/create.js
index e292e4ba11..0dea1e3133 100644
--- a/src/topics/create.js
+++ b/src/topics/create.js
@@ -20,7 +20,7 @@ module.exports = function (Topics) {
// This is an internal method, consider using Topics.post instead
const timestamp = data.timestamp || Date.now();
- const tid = await db.incrObjectField('global', 'nextTid');
+ const tid = data.tid || await db.incrObjectField('global', 'nextTid');
let topicData = {
tid: tid,