diff --git a/packages/backend/src/remote/resolve-user.ts b/packages/backend/src/remote/resolve-user.ts index c2c70ef6d..6c78e13b9 100644 --- a/packages/backend/src/remote/resolve-user.ts +++ b/packages/backend/src/remote/resolve-user.ts @@ -12,6 +12,7 @@ import { Cache } from "@/misc/cache.js"; import { IMentionedRemoteUsers } from "@/models/entities/note.js"; import { UserProfile } from "@/models/entities/user-profile.js"; import { RecursionLimiter } from "@/models/repositories/user-profile.js"; +import { promiseEarlyReturn } from "@/prelude/promise.js"; const logger = remoteLogger.createSubLogger("resolve-user"); const uriHostCache = new Cache("resolveUserUriHost", 60 * 60 * 24); @@ -27,11 +28,12 @@ type ProfileMention = { }; }; +type refreshType = 'refresh' | 'refresh-in-background' | 'refresh-timeout-1500ms' | 'no-refresh'; + export async function resolveUser( username: string, host: string | null, - refresh: boolean = true, - awaitRefresh: boolean = true, + refresh: refreshType = 'refresh', limiter: RecursionLimiter = new RecursionLimiter(20) ): Promise { const usernameLower = username.toLowerCase(); @@ -118,7 +120,7 @@ export async function resolveUser( // If user information is out of date, return it by starting over from WebFinger if ( - refresh && awaitRefresh && ( + (refresh === 'refresh' || refresh === 'refresh-timeout-1500ms') && ( user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24 ) @@ -176,9 +178,15 @@ export async function resolveUser( ); } - await updatePerson(fingerRes.self.href); + if (refresh === 'refresh') { + await updatePerson(fingerRes.self.href); + logger.info(`return resynced remote user: ${finalAcctLower}`); + } + else if (refresh === 'refresh-timeout-1500ms') { + const res = await promiseEarlyReturn(updatePerson(fingerRes.self.href), 1500); + logger.info(`return possibly resynced remote user: ${finalAcctLower}`); + } - logger.info(`return resynced remote user: ${finalAcctLower}`); return await Users.findOneBy({ uri: fingerRes.self.href }).then((u) => { if (u == null) { throw new Error("user not found"); @@ -186,10 +194,10 @@ export async function resolveUser( return u; } }); - } else if (refresh && !awaitRefresh && (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24)) { + } else if (refresh === 'refresh-in-background' && (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, limiter); + resolveUser(username, host, 'refresh', limiter); } logger.info(`return existing remote user: ${acctLower}`); @@ -199,7 +207,7 @@ export async function resolveUser( export async function resolveMentionToUserAndProfile(username: string, host: string | null, objectHost: string | null, limiter: RecursionLimiter) { return profileMentionCache.fetch(`${username}@${host ?? objectHost}`, async () => { try { - const user = await resolveUser(username, host ?? objectHost, false, false, limiter); + const user = await resolveUser(username, host ?? objectHost, 'no-refresh', limiter); const profile = await UserProfiles.findOneBy({ userId: user.id }); const data = { username, host: host ?? objectHost }; diff --git a/packages/backend/src/server/api/mastodon/converters/user.ts b/packages/backend/src/server/api/mastodon/converters/user.ts index 07a3c3235..7ba7385d7 100644 --- a/packages/backend/src/server/api/mastodon/converters/user.ts +++ b/packages/backend/src/server/api/mastodon/converters/user.ts @@ -6,7 +6,7 @@ import { populateEmojis } from "@/misc/populate-emojis.js"; import { escapeMFM } from "@/server/api/mastodon/converters/mfm.js"; import mfm from "mfm-js"; import { awaitAll } from "@/prelude/await-all.js"; -import { AccountCache } from "@/server/api/mastodon/helpers/user.js"; +import { AccountCache, UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { MfmHelpers } from "@/server/api/mastodon/helpers/mfm.js"; import { MastoContext } from "@/server/api/mastodon/index.js"; import { IMentionedRemoteUsers } from "@/models/entities/note.js"; @@ -99,6 +99,8 @@ export class UserConverter { bot: u.isBot, discoverable: u.isExplorable }).then(p => { + // noinspection ES6MissingAwait + UserHelpers.updateUserInBackground(u); cache.accounts.push(p); return p; }); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 06c28e3aa..901334349 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -245,7 +245,7 @@ export class UserHelpers { if (p) return p; throw new MastoApiError(404); }) - : resolveUser(split[0], split[1], true, false).catch(() => { + : resolveUser(split[0], split[1], 'no-refresh').catch(() => { throw new MastoApiError(404); }); } @@ -522,9 +522,6 @@ export class UserHelpers { public static async getUserOr404(id: string): Promise { return getUser(id).catch(_ => { throw new MastoApiError(404); - }).then(u => { - this.updateUserInBackground(u); - return u; }); }