Node.js based forum software built for the modern web
fix: ActivityPub.get now throws on failure, handle in getActor
Julian Lam7ec75f58302c
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.jsonindex 75a8328aa1..c353fb6964 100644--- a/public/language/en-GB/error.json+++ b/public/language/en-GB/error.json@@ -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."}diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.jsindex 133064d438..fefeb8be06 100644--- a/src/activitypub/helpers.js+++ b/src/activitypub/helpers.js@@ -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);diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex 272206a168..13986b836f 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -34,24 +34,23 @@ ActivityPub.getActor = async (uid, input) => {return actorCache.get(uri);}- 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')) {+ try {+ const actor = await ActivityPub.get(uid, uri);++ // 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;diff --git a/src/api/activitypub.js b/src/api/activitypub.jsindex 86d9fb9d32..f6544f0962 100644--- a/src/api/activitypub.js+++ b/src/api/activitypub.js@@ -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, {