fix: ActivityPub.get now throws on failure, handle in getActor

This commit is contained in:
Julian Lam
2024-01-08 14:30:09 -05:00
parent a1c8c3a043
commit 7ec75f5830
4 changed files with 24 additions and 20 deletions

View File

@@ -274,5 +274,8 @@
"api.500": "An unexpected error was encountered while attempting to service your request.",
"api.501": "The route you are trying to call is not implemented yet, please try again tomorrow",
"api.503": "The route you are trying to call is not currently available due to a server configuration",
"api.reauth-required": "The resource you are trying to access requires (re-)authentication."
"api.reauth-required": "The resource you are trying to access requires (re-)authentication.",
"activitypub.invalid-id": "Unable to resolve the input id, likely as it is malformed.",
"activitypub.get-failed": "Unable to retrieve the specified resource."
}

View File

@@ -96,12 +96,12 @@ Helpers.resolveLocalUid = async (input) => {
if (host === nconf.get('url_parsed').host) {
slug = pathname.replace(nconf.get('relative_path'), '').split('/').filter(Boolean)[1];
} else {
throw new Error('[[activitypub:invalid-id]]');
throw new Error('[[error:activitypub.invalid-id]]');
}
} else if (input.indexOf('@') !== -1) { // Webfinger
([slug] = input.replace(/^acct:/, '').split('@'));
} else {
throw new Error('[[activitypub:invalid-id]]');
throw new Error('[[error:activitypub.invalid-id]]');
}
return await user.getUidByUserslug(slug);

View File

@@ -34,24 +34,23 @@ ActivityPub.getActor = async (uid, input) => {
return actorCache.get(uri);
}
const actor = await ActivityPub.get(uid, uri);
try {
const actor = await ActivityPub.get(uid, uri);
// todo: remove this after ActivityPub.get is updated to handle errors more effectively
if (typeof actor === 'string' || actor.hasOwnProperty('error')) {
// Follow counts
const [followers, following] = await Promise.all([
actor.followers ? ActivityPub.get(uid, actor.followers) : { totalItems: 0 },
actor.following ? ActivityPub.get(uid, actor.following) : { totalItems: 0 },
]);
actor.followerCount = followers.totalItems;
actor.followingCount = following.totalItems;
actorCache.set(uri, actor);
return actor;
} catch (e) {
winston.warn(`[activitypub/getActor] Unable to retrieve actor "${uri}", error: ${e.message}`);
return null;
}
const [followers, following] = await Promise.all([
actor.followers ? ActivityPub.get(uid, actor.followers) : { totalItems: 0 },
actor.following ? ActivityPub.get(uid, actor.following) : { totalItems: 0 },
]);
actor.hostname = new URL(uri).hostname;
actor.followerCount = followers.totalItems;
actor.followingCount = following.totalItems;
actorCache.set(uri, actor);
return actor;
};
ActivityPub.mockProfile = async (actors, callerUid = 0) => {
@@ -237,6 +236,8 @@ ActivityPub.get = async (uid, uri) => {
if (body.hasOwnProperty('error')) {
winston.error(`[activitypub/get] Error received: ${body.error}`);
}
throw new Error(`[[error:activitypub.get-failed]]`);
}
return body;

View File

@@ -19,7 +19,7 @@ const activitypubApi = module.exports;
activitypubApi.follow = async (caller, { actorId } = {}) => {
const object = await activitypub.getActor(caller.uid, actorId);
if (!object) {
throw new Error('[[error:invalid-uid]]'); // should be activitypub-specific
throw new Error('[[error:activitypub.invalid-id]]');
}
await activitypub.send(caller.uid, actorId, {
@@ -32,7 +32,7 @@ activitypubApi.unfollow = async (caller, { actorId }) => {
const object = await activitypub.getActor(caller.uid, actorId);
const userslug = await user.getUserField(caller.uid, 'userslug');
if (!object) {
throw new Error('[[error:invalid-uid]]'); // should be activitypub-specific
throw new Error('[[error:activitypub.invalid-id]]');
}
await activitypub.send(caller.uid, actorId, {