Revert "feat: changes to how a topic is presented via ActivityPub; conformance with upcoming changes to 7888"

This reverts commit 4fd7a9dc59.
This commit is contained in:
Julian Lam
2025-01-29 14:38:43 -05:00
parent 02fb99ebb6
commit 126ff28612
4 changed files with 31 additions and 61 deletions

View File

@@ -12,7 +12,6 @@ const acceptableTypes = ['Collection', 'CollectionPage', 'OrderedCollection', 'O
Contexts.get = async (uid, id) => { Contexts.get = async (uid, id) => {
let context; let context;
let type; let type;
let collection;
// Generate digest for If-None-Match if locally cached // Generate digest for If-None-Match if locally cached
const tid = await posts.getPostField(id, 'tid'); const tid = await posts.getPostField(id, 'tid');
@@ -28,15 +27,12 @@ Contexts.get = async (uid, id) => {
} }
try { try {
({ id, type, context, posts: collection } = await activitypub.get('uid', uid, id, { headers })); ({ context } = await activitypub.get('uid', uid, id, { headers }));
if (type === 'Conversation' && collection) { if (!context) {
activitypub.helpers.log(`[activitypub/context] ${id} is the context.`);
return { context: id, collection };
} else if (!context) {
activitypub.helpers.log(`[activitypub/context] ${id} contains no context.`); activitypub.helpers.log(`[activitypub/context] ${id} contains no context.`);
return false; return false;
} }
({ type, posts: collection } = await activitypub.get('uid', uid, context)); ({ type } = await activitypub.get('uid', uid, context));
} catch (e) { } catch (e) {
if (e.code === 'ap_get_304') { if (e.code === 'ap_get_304') {
activitypub.helpers.log(`[activitypub/context] ${id} context unchanged.`); activitypub.helpers.log(`[activitypub/context] ${id} context unchanged.`);
@@ -47,8 +43,8 @@ Contexts.get = async (uid, id) => {
return false; return false;
} }
if (type === 'Conversation') { if (acceptableTypes.includes(type)) {
return { context, collection }; return { context };
} }
return false; return false;
@@ -102,13 +98,9 @@ Contexts.getItems = async (uid, id, options) => {
const inputId = activitypub.helpers.isUri(options.input) ? options.input : options.input.id; const inputId = activitypub.helpers.isUri(options.input) ? options.input : options.input.id;
const inCollection = Array.from(chain).map(p => p.pid).includes(inputId); const inCollection = Array.from(chain).map(p => p.pid).includes(inputId);
if (!inCollection) { if (!inCollection) {
const item = activitypub.helpers.isUri(options.input) ? chain.add(activitypub.helpers.isUri(options.input) ?
await parseString(uid, options.input) : await parseString(uid, options.input) :
await parseItem(uid, options.input); await parseItem(uid, options.input));
if (item) {
chain.add(item);
}
} }
return chain; return chain;

View File

@@ -49,7 +49,7 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {
const { tid } = context; const { tid } = context;
return { tid, count: 0 }; return { tid, count: 0 };
} else if (context.context) { } else if (context.context) {
chain = Array.from(await activitypub.contexts.getItems(uid, context.collection, { input })); chain = Array.from(await activitypub.contexts.getItems(uid, context.context, { input }));
if (chain && chain.length) { if (chain && chain.length) {
// Context resolves, use in later topic creation // Context resolves, use in later topic creation
context = context.context; context = context.context;

View File

@@ -115,52 +115,12 @@ Actors.replies = async function (req, res) {
Actors.topic = async function (req, res, next) { Actors.topic = async function (req, res, next) {
const allowed = await privileges.topics.can('topics:read', req.params.tid, activitypub._constants.uid); const allowed = await privileges.topics.can('topics:read', req.params.tid, activitypub._constants.uid);
if (!allowed) { if (!allowed) {
return next(); return res.sendStatus(404);
}
const { cid, titleRaw: name, mainPid, slug } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug']);
let pids = await db.getSortedSetMembers(`tid:${req.params.tid}:posts`);
pids.push(mainPid);
pids = pids.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid));
// Generate digest for ETag
const digest = activitypub.helpers.generateDigest(new Set(pids));
const ifNoneMatch = (req.get('If-None-Match') || '').split(',').map((tag) => {
tag = tag.trim();
if (tag.startsWith('"') && tag.endsWith('"')) {
return tag.slice(1, tag.length - 1);
}
return tag;
});
if (ifNoneMatch.includes(digest)) {
return res.sendStatus(304);
}
res.set('ETag', digest);
const object = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${nconf.get('url')}/topic/${req.params.tid}`,
url: `${nconf.get('url')}/topic/${slug}`,
type: 'Conversation',
name,
attributedTo: `${nconf.get('url')}/category/${cid}`,
// audience: cid !== -1 ? `${nconf.get('url')}/category/${cid}` : undefined,
posts: `${nconf.get('url')}/topic/${req.params.tid}/posts`,
};
res.status(200).json(object);
};
Actors.topicPosts = async function (req, res, next) {
const allowed = await privileges.topics.can('topics:read', req.params.tid, activitypub._constants.uid);
if (!allowed) {
return next();
} }
const page = parseInt(req.query.page, 10) || undefined; const page = parseInt(req.query.page, 10) || undefined;
const perPage = meta.config.postsPerPage; const perPage = meta.config.postsPerPage;
const mainPid = await topics.getTopicField(req.params.tid, 'mainPid'); const { cid, titleRaw: name, mainPid, slug } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug']);
try { try {
let [collection, pids] = await Promise.all([ let [collection, pids] = await Promise.all([
activitypub.helpers.generateCollection({ activitypub.helpers.generateCollection({
@@ -176,6 +136,21 @@ Actors.topicPosts = async function (req, res, next) {
pids = pids.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid)); pids = pids.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid));
collection.totalItems += 1; // account for mainPid collection.totalItems += 1; // account for mainPid
// Generate digest for ETag
const digest = activitypub.helpers.generateDigest(new Set(pids));
const ifNoneMatch = (req.get('If-None-Match') || '').split(',').map((tag) => {
tag = tag.trim();
if (tag.startsWith('"') && tag.endsWith('"')) {
return tag.slice(1, tag.length - 1);
}
return tag;
});
if (ifNoneMatch.includes(digest)) {
return res.sendStatus(304);
}
res.set('ETag', digest);
// Convert pids to urls // Convert pids to urls
if (page || collection.totalItems < meta.config.postsPerPage) { if (page || collection.totalItems < meta.config.postsPerPage) {
collection.orderedItems = collection.orderedItems || []; collection.orderedItems = collection.orderedItems || [];
@@ -187,7 +162,11 @@ Actors.topicPosts = async function (req, res, next) {
const object = { const object = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
id: `${nconf.get('url')}/topic/${req.params.tid}/posts${collection.orderedItems && page ? `?page=${page}` : ''}`, id: `${nconf.get('url')}/topic/${req.params.tid}${collection.orderedItems && page ? `?page=${page}` : ''}`,
url: `${nconf.get('url')}/topic/${slug}`,
name,
attributedTo: `${nconf.get('url')}/category/${cid}`,
audience: cid !== -1 ? `${nconf.get('url')}/category/${cid}` : undefined,
...collection, ...collection,
}; };

View File

@@ -40,14 +40,13 @@ module.exports = function (app, middleware, controllers) {
app.get('/post/:pid', [...middlewares, middleware.assert.post], controllers.activitypub.actors.note); app.get('/post/:pid', [...middlewares, middleware.assert.post], controllers.activitypub.actors.note);
app.get('/post/:pid/replies', [...middlewares, middleware.assert.post], controllers.activitypub.actors.replies); app.get('/post/:pid/replies', [...middlewares, middleware.assert.post], controllers.activitypub.actors.replies);
app.get('/topic/:tid/posts', [...middlewares, middleware.assert.topic], controllers.activitypub.actors.topicPosts);
app.get('/topic/:tid/:slug?', [...middlewares, middleware.assert.topic], controllers.activitypub.actors.topic); app.get('/topic/:tid/:slug?', [...middlewares, middleware.assert.topic], controllers.activitypub.actors.topic);
app.get('/category/:cid/:slug?', [...middlewares, middleware.assert.category], controllers.activitypub.actors.category);
app.get('/category/:cid/inbox', [...middlewares, middleware.assert.category], controllers.activitypub.getInbox); app.get('/category/:cid/inbox', [...middlewares, middleware.assert.category], controllers.activitypub.getInbox);
app.post('/category/:cid/inbox', [...inboxMiddlewares, middleware.assert.category, ...inboxMiddlewares], controllers.activitypub.postInbox); app.post('/category/:cid/inbox', [...inboxMiddlewares, middleware.assert.category, ...inboxMiddlewares], controllers.activitypub.postInbox);
app.get('/category/:cid/outbox', [...middlewares, middleware.assert.category], controllers.activitypub.getCategoryOutbox); app.get('/category/:cid/outbox', [...middlewares, middleware.assert.category], controllers.activitypub.getCategoryOutbox);
app.post('/category/:cid/outbox', [...middlewares, middleware.assert.category], controllers.activitypub.postOutbox); app.post('/category/:cid/outbox', [...middlewares, middleware.assert.category], controllers.activitypub.postOutbox);
app.get('/category/:cid/:slug?', [...middlewares, middleware.assert.category], controllers.activitypub.actors.category);
app.get('/message/:mid', [...middlewares, middleware.assert.message], controllers.activitypub.actors.message); app.get('/message/:mid', [...middlewares, middleware.assert.message], controllers.activitypub.actors.message);
}; };