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 exists = await db.exists(`post:${id}`);
if (exists) {
const { toPid, timestamp } = await posts.getPostFields(id, ['toPid', 'timestamp']);
chain.add({ id, timestamp });
if (toPid) {
await traverse(uid, toPid);
const postData = await posts.getPostData(id);
chain.add(postData);
if (postData.toPid) {
await traverse(uid, postData.toPid);
}
} else {
let object = await activitypub.get(uid, id);
object = await activitypub.mocks.post(object);
if (object) {
chain.add({ id, timestamp: object.timestamp });
if (object.hasOwnProperty('toPid') && object.toPid) {
chain.add(object);
if (object.toPid) {
await traverse(uid, object.toPid);
}
}
@@ -62,7 +62,7 @@ Notes.getParentChain = async (uid, input) => {
Notes.assertTopic = async (uid, id) => {
// 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 tid = chain[chain.length - 1].id;
const tid = chain[chain.length - 1].pid;
const sorted = chain.sort((a, b) => a.timestamp - b.timestamp);
const [ids, timestamps] = [
@@ -70,13 +70,24 @@ Notes.assertTopic = async (uid, id) => {
sorted.map(n => n.timestamp),
];
await db.sortedSetAdd(`topicRemote:${tid}`, timestamps, ids);
await Notes.assert(uid, chain);
await Promise.all([
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;
};
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);
};

View File

@@ -23,11 +23,8 @@ controller.get = async function (req, res, next) {
// topics.getTopicData(tid),
]);
const topicData = {
tid,
postCount: 6,
category: {}, // todo
};
const topicData = await topics.getTopicData(tid);
topicData.category = {}; // todo
let currentPage = parseInt(req.query.page, 10) || 1;
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 translator = require('../translator');
const plugins = require('../plugins');
const activitypub = require('../activitypub');
const intFields = [
'tid', 'cid', 'uid', 'mainPid', 'postcount',
@@ -26,7 +27,7 @@ module.exports = function (Topics) {
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 result = await plugins.hooks.fire('filter:topic.getFields', {
tids: tids,
@@ -95,6 +96,10 @@ function modifyTopic(topic, fields) {
return;
}
if (activitypub.helpers.isUri(topic.tid)) {
intFields.splice(intFields.indexOf('uid'), 1);
intFields.splice(intFields.indexOf('tid'), 1);
}
db.parseIntFields(topic, intFields, fields);
if (topic.hasOwnProperty('title')) {