mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: context item retrieval logic
This commit is contained in:
@@ -35,31 +35,14 @@ Contexts.getItems = async (uid, id, root = true) => {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type.startsWith('Ordered')) {
|
if (type.startsWith('Ordered') && orderedItems) {
|
||||||
items = orderedItems;
|
items = orderedItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (items) {
|
if (items) {
|
||||||
items = (await Promise.all(items.map(async (item) => {
|
items = await Promise.all(items
|
||||||
const { type, id } = await activitypub.helpers.resolveLocalId(item);
|
.map(async item => (activitypub.helpers.isUri(item) ? parseString(uid, item) : parseItem(uid, item))));
|
||||||
const pid = type === 'post' && id ? id : item;
|
items = items.filter(Boolean);
|
||||||
const postData = await posts.getPostData(pid);
|
|
||||||
if (postData) {
|
|
||||||
// Already cached
|
|
||||||
return postData;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No local copy, fetch from source
|
|
||||||
try {
|
|
||||||
const object = await activitypub.get('uid', uid, pid);
|
|
||||||
winston.verbose(`[activitypub/context] Retrieved ${pid}`);
|
|
||||||
return await activitypub.mocks.post(object);
|
|
||||||
} catch (e) {
|
|
||||||
// Unresolvable, either temporariliy or permanent, ignore for now.
|
|
||||||
winston.verbose(`[activitypub/context] Cannot retrieve ${id}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}))).filter(Boolean);
|
|
||||||
winston.verbose(`[activitypub/context] Found ${items.length} items.`);
|
winston.verbose(`[activitypub/context] Found ${items.length} items.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,3 +62,49 @@ Contexts.getItems = async (uid, id, root = true) => {
|
|||||||
|
|
||||||
return chain;
|
return chain;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function parseString(uid, item) {
|
||||||
|
const { type, id } = await activitypub.helpers.resolveLocalId(item);
|
||||||
|
const pid = type === 'post' && id ? id : item;
|
||||||
|
const postData = await posts.getPostData(pid);
|
||||||
|
if (postData) {
|
||||||
|
// Already cached
|
||||||
|
return postData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No local copy, fetch from source
|
||||||
|
try {
|
||||||
|
const object = await activitypub.get('uid', uid, pid);
|
||||||
|
winston.verbose(`[activitypub/context] Retrieved ${pid}`);
|
||||||
|
|
||||||
|
return parseItem(uid, object);
|
||||||
|
} catch (e) {
|
||||||
|
// Unresolvable, either temporarily or permanent, ignore for now.
|
||||||
|
winston.verbose(`[activitypub/context] Cannot retrieve ${id}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function parseItem(uid, item) {
|
||||||
|
const { type, id } = await activitypub.helpers.resolveLocalId(item.id);
|
||||||
|
const pid = type === 'post' && id ? id : item.id;
|
||||||
|
const postData = await posts.getPostData(pid);
|
||||||
|
if (postData) {
|
||||||
|
// Already cached
|
||||||
|
return postData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle activity wrapper
|
||||||
|
if (item.type === 'Create') {
|
||||||
|
item = item.object;
|
||||||
|
if (activitypub.helpers.isUri(item)) {
|
||||||
|
return parseString(uid, item);
|
||||||
|
}
|
||||||
|
} else if (!activitypub._constants.acceptedPostTypes.includes(item.type)) {
|
||||||
|
// Not a note, silently skip.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
winston.verbose(`[activitypub/context] Parsing ${pid}`);
|
||||||
|
return await activitypub.mocks.post(item);
|
||||||
|
}
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ Mocks.post = async (objects) => {
|
|||||||
// tid, --> purposely omitted
|
// tid, --> purposely omitted
|
||||||
name,
|
name,
|
||||||
content,
|
content,
|
||||||
sourceContent: source.mediaType === 'text/markdown' ? source.content : undefined,
|
sourceContent: source && source.mediaType === 'text/markdown' ? source.content : undefined,
|
||||||
timestamp,
|
timestamp,
|
||||||
toPid,
|
toPid,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user