feat: some additional work to properly save and retrieve remote topic data

This commit is contained in:
Julian Lam
2024-01-12 16:39:29 -05:00
parent 273188632d
commit 4bd7a574c3
3 changed files with 29 additions and 16 deletions

View File

@@ -38,17 +38,17 @@ Notes.getParentChain = async (uid, input) => {
const traverse = async (uid, id) => { const traverse = async (uid, id) => {
const exists = await db.exists(`post:${id}`); const exists = await db.exists(`post:${id}`);
if (exists) { if (exists) {
const { toPid, timestamp } = await posts.getPostFields(id, ['toPid', 'timestamp']); const postData = await posts.getPostData(id);
chain.add({ id, timestamp }); chain.add(postData);
if (toPid) { if (postData.toPid) {
await traverse(uid, toPid); await traverse(uid, postData.toPid);
} }
} else { } else {
let object = await activitypub.get(uid, id); let object = await activitypub.get(uid, id);
object = await activitypub.mocks.post(object); object = await activitypub.mocks.post(object);
if (object) { if (object) {
chain.add({ id, timestamp: object.timestamp }); chain.add(object);
if (object.hasOwnProperty('toPid') && object.toPid) { if (object.toPid) {
await traverse(uid, object.toPid); await traverse(uid, object.toPid);
} }
} }
@@ -62,7 +62,7 @@ Notes.getParentChain = async (uid, input) => {
Notes.assertTopic = async (uid, id) => { Notes.assertTopic = async (uid, id) => {
// Given the id of any post, traverses up (and soon, down) to cache the entire threaded context // Given the id of any post, traverses up (and soon, down) to cache the entire threaded context
const chain = Array.from(await Notes.getParentChain(uid, id)); const chain = Array.from(await Notes.getParentChain(uid, id));
const tid = chain[chain.length - 1].id; const tid = chain[chain.length - 1].pid;
const sorted = chain.sort((a, b) => a.timestamp - b.timestamp); const sorted = chain.sort((a, b) => a.timestamp - b.timestamp);
const [ids, timestamps] = [ const [ids, timestamps] = [
@@ -70,13 +70,24 @@ Notes.assertTopic = async (uid, id) => {
sorted.map(n => n.timestamp), sorted.map(n => n.timestamp),
]; ];
await db.sortedSetAdd(`topicRemote:${tid}`, timestamps, ids); await Promise.all([
await Notes.assert(uid, chain); db.setObject(`topicRemote:${tid}`, {
tid,
uid,
cid: -1,
mainPid: tid,
title: 'TBD',
slug: `remote?resource=${encodeURIComponent(tid)}`,
postcount: sorted.length,
}),
db.sortedSetAdd(`tidRemote:${tid}:posts`, timestamps, ids),
Notes.assert(uid, chain),
]);
return tid; return tid;
}; };
Notes.getTopicPosts = async (tid, uid, start, stop) => { Notes.getTopicPosts = async (tid, uid, start, stop) => {
const pids = await db.getSortedSetRange(`topicRemote:${tid}`, start, stop); const pids = await db.getSortedSetRange(`tidRemote:${tid}:posts`, start, stop);
return await posts.getPostsByPids(pids, uid); return await posts.getPostsByPids(pids, uid);
}; };

View File

@@ -23,11 +23,8 @@ controller.get = async function (req, res, next) {
// topics.getTopicData(tid), // topics.getTopicData(tid),
]); ]);
const topicData = { const topicData = await topics.getTopicData(tid);
tid, topicData.category = {}; // todo
postCount: 6,
category: {}, // todo
};
let currentPage = parseInt(req.query.page, 10) || 1; let currentPage = parseInt(req.query.page, 10) || 1;
const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage)); const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));

View File

@@ -7,6 +7,7 @@ const categories = require('../categories');
const utils = require('../utils'); const utils = require('../utils');
const translator = require('../translator'); const translator = require('../translator');
const plugins = require('../plugins'); const plugins = require('../plugins');
const activitypub = require('../activitypub');
const intFields = [ const intFields = [
'tid', 'cid', 'uid', 'mainPid', 'postcount', 'tid', 'cid', 'uid', 'mainPid', 'postcount',
@@ -26,7 +27,7 @@ module.exports = function (Topics) {
fields.push('timestamp'); fields.push('timestamp');
} }
const keys = tids.map(tid => `topic:${tid}`); const keys = tids.map(tid => `${activitypub.helpers.isUri(tid) ? 'topicRemote' : 'topic'}:${tid}`);
const topics = await db.getObjects(keys, fields); const topics = await db.getObjects(keys, fields);
const result = await plugins.hooks.fire('filter:topic.getFields', { const result = await plugins.hooks.fire('filter:topic.getFields', {
tids: tids, tids: tids,
@@ -95,6 +96,10 @@ function modifyTopic(topic, fields) {
return; return;
} }
if (activitypub.helpers.isUri(topic.tid)) {
intFields.splice(intFields.indexOf('uid'), 1);
intFields.splice(intFields.indexOf('tid'), 1);
}
db.parseIntFields(topic, intFields, fields); db.parseIntFields(topic, intFields, fields);
if (topic.hasOwnProperty('title')) { if (topic.hasOwnProperty('title')) {