refactor: generate a topic id when asserting a new topic

This commit is contained in:
Julian Lam
2024-01-19 11:31:04 -05:00
parent 981b4f146d
commit 2ff70fdde2
9 changed files with 39 additions and 34 deletions

View File

@@ -73,14 +73,17 @@ Notes.getParentChain = async (uid, input) => {
return chain;
};
Notes.assertParentChain = async (chain) => {
Notes.assertParentChain = async (chain, tid) => {
const data = [];
chain.reduce((child, parent) => {
data.push([`pid:${parent.pid}:replies`, child.timestamp, child.pid]);
return parent;
});
await db.sortedSetAddBulk(data);
await Promise.all([
db.sortedSetAddBulk(data),
db.setObjectBulk(chain.map(post => [`post:${post.pid}`, { tid }])),
]);
};
Notes.assertTopic = async (uid, id) => {
@@ -92,22 +95,22 @@ Notes.assertTopic = async (uid, id) => {
*/
const chain = Array.from(await Notes.getParentChain(uid, id));
const { pid: tid, uid: authorId, timestamp, name, content } = chain[chain.length - 1];
let { pid: mainPid, tid, uid: authorId, timestamp, name, content } = chain[chain.length - 1];
const members = await db.isSortedSetMembers(`tidRemote:${tid}:posts`, chain.map(p => p.pid));
if (members.every(Boolean)) {
if (tid && members.every(Boolean)) {
// All cached, return early.
winston.info('[notes/assertTopic] No new notes to process.');
return tid;
}
let title = name || utils.stripHTMLTags(content);
if (title.length > 256) {
title = `${title.slice(0, 256)}...`;
}
tid = tid || utils.generateUUID();
const cid = await topics.getTopicField(tid, 'cid');
let title = name || utils.decodeHTMLEntities(utils.stripHTMLTags(content));
if (title.length > 64) {
title = `${title.slice(0, 64)}...`;
}
const unprocessed = chain.filter((p, idx) => !members[idx]);
winston.info(`[notes/assertTopic] ${unprocessed.length} new note(s) found.`);
@@ -121,16 +124,16 @@ Notes.assertTopic = async (uid, id) => {
tid,
uid: authorId,
cid: cid || -1,
mainPid: tid,
mainPid,
title,
slug: `remote?resource=${encodeURIComponent(tid)}`,
slug: `../world/${tid}`,
timestamp,
}),
db.sortedSetAdd(`tidRemote:${tid}:posts`, timestamps, ids),
Notes.assert(uid, unprocessed),
]);
await Promise.all([ // must be done after .assert()
Notes.assertParentChain(chain),
Notes.assertParentChain(chain, tid),
Notes.updateTopicCounts(tid),
topics.updateLastPostTimeFromLastPid(tid),
topics.updateTeaser(tid),