feat: associate new topics created from asserted notes with their originating context, if that context is resolvable

re: #12647
This commit is contained in:
Julian Lam
2024-10-24 14:35:34 -04:00
parent 15516862ce
commit 6f237c57cd
2 changed files with 14 additions and 3 deletions

View File

@@ -58,7 +58,7 @@ Contexts.getItems = async (uid, id, options) => {
activitypub.helpers.log(`[activitypub/context] Retrieving context ${id}`); activitypub.helpers.log(`[activitypub/context] Retrieving context ${id}`);
let { type, items, orderedItems, first, next } = await activitypub.get('uid', uid, id); let { type, items, orderedItems, first, next } = await activitypub.get('uid', uid, id);
if (!acceptableTypes.includes(type)) { if (!acceptableTypes.includes(type)) {
return []; return false;
} }
if (type.startsWith('Ordered') && orderedItems) { if (type.startsWith('Ordered') && orderedItems) {
@@ -79,7 +79,7 @@ Contexts.getItems = async (uid, id, options) => {
// Early breakout on empty collection // Early breakout on empty collection
if (!next && !chain.size) { if (!next && !chain.size) {
return []; return new Set();
} }
if (next) { if (next) {

View File

@@ -43,13 +43,19 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {
} }
let chain; let chain;
const context = await activitypub.contexts.get(uid, id); let context = await activitypub.contexts.get(uid, id);
if (context.tid) { if (context.tid) {
unlock(id); unlock(id);
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.context, { input })); chain = Array.from(await activitypub.contexts.getItems(uid, context.context, { input }));
if (chain && chain.length) {
// Context resolves, use in later topic creation
context = context.context;
}
} else {
context = undefined;
} }
if (!chain || !chain.length) { if (!chain || !chain.length) {
@@ -169,6 +175,11 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {
posts.attachments.update(mainPid, attachment), posts.attachments.update(mainPid, attachment),
]); ]);
unprocessed.shift(); unprocessed.shift();
if (context) {
activitypub.helpers.log(`[activitypub/notes.assert] Associating tid ${tid} with context ${context}`);
await topics.setTopicField(tid, 'context', context);
}
} }
for (const post of unprocessed) { for (const post of unprocessed) {