From 7c7c6a09a22d5b655ff34937e93f085be339c71d Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sun, 15 Oct 2023 23:01:20 +0200 Subject: [PATCH] [mastodon-client] Refresh user profile data on /accounts/lookup --- packages/backend/src/remote/resolve-user.ts | 9 +++++++-- .../backend/src/server/api/mastodon/helpers/user.ts | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/remote/resolve-user.ts b/packages/backend/src/remote/resolve-user.ts index 94f27977f..9206dc6bd 100644 --- a/packages/backend/src/remote/resolve-user.ts +++ b/packages/backend/src/remote/resolve-user.ts @@ -18,7 +18,8 @@ const mentionUriCache = new Cache("resolveMentionUserUri", 60 * 60 * 72) export async function resolveUser( username: string, host: string | null, - refresh: boolean = true + refresh: boolean = true, + awaitRefresh: boolean = true ): Promise { const usernameLower = username.toLowerCase(); @@ -104,7 +105,7 @@ export async function resolveUser( // If user information is out of date, return it by starting over from WebFinger if ( - refresh && ( + refresh && awaitRefresh && ( user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24 ) @@ -172,6 +173,10 @@ export async function resolveUser( return u; } }); + } else if (refresh && !awaitRefresh && (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24)) { + // Run the refresh in the background + // noinspection ES6MissingAwait + resolveUser(username, host, true, true); } logger.info(`return existing remote user: ${acctLower}`); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 4a000e405..15b664daf 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -41,6 +41,7 @@ import { UserProfile } from "@/models/entities/user-profile.js"; import { verifyLink } from "@/services/fetch-rel-me.js"; import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; import { MastoContext } from "@/server/api/mastodon/index.js"; +import { resolveUser } from "@/remote/resolve-user.js"; export type AccountCache = { locks: AsyncLock; @@ -233,9 +234,13 @@ export class UserHelpers { public static async getUserFromAcct(acct: string): Promise { const split = acct.toLowerCase().split('@'); if (split.length > 2) throw new Error('Invalid acct'); - return Users.findOneBy({ usernameLower: split[0], host: split[1] ?? IsNull() }) - .then(p => { - if (p) return p; + return split[1] == null + ? Users.findOneBy({ usernameLower: split[0], host: split[1] ?? IsNull() }) + .then(p => { + if (p) return p; + throw new MastoApiError(404); + }) + : resolveUser(split[0], split[1], true, false).catch(() => { throw new MastoApiError(404); }); }