[mastodon-client] Refresh user profile data on /accounts/lookup

This commit is contained in:
Laura Hausmann 2023-10-15 23:01:20 +02:00
parent 75c9873796
commit 7c7c6a09a2
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 15 additions and 5 deletions

View file

@ -18,7 +18,8 @@ const mentionUriCache = new Cache<string>("resolveMentionUserUri", 60 * 60 * 72)
export async function resolveUser(
username: string,
host: string | null,
refresh: boolean = true
refresh: boolean = true,
awaitRefresh: boolean = true
): Promise<User> {
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}`);

View file

@ -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,10 +234,14 @@ export class UserHelpers {
public static async getUserFromAcct(acct: string): Promise<User> {
const split = acct.toLowerCase().split('@');
if (split.length > 2) throw new Error('Invalid acct');
return Users.findOneBy({ usernameLower: split[0], host: split[1] ?? IsNull() })
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);
});
}