[mastodon-api] Only wait up to 1500 ms for mentions to update on /accounts/update_credentials

This commit is contained in:
Laura Hausmann 2023-10-17 23:51:31 +02:00
parent b8bd0c9f3b
commit 998bb1ae08
Signed by: zotan
GPG key ID: D044E84C5BE01605
2 changed files with 7 additions and 1 deletions

View file

@ -0,0 +1,5 @@
// Returns T if promise settles before timeout, otherwise returns void, finishing execution in the background.
export async function promiseEarlyReturn<T>(promise: Promise<T>, after: number): Promise<T | void> {
const timer: Promise<void> = new Promise((res) => setTimeout(() => res(undefined), after));
return Promise.race([promise, timer]);
}

View file

@ -43,6 +43,7 @@ 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";
import { updatePerson } from "@/remote/activitypub/models/person.js";
import { promiseEarlyReturn } from "@/prelude/promise.js";
export type AccountCache = {
locks: AsyncLock;
@ -197,7 +198,7 @@ export class UserHelpers {
if (Object.keys(updates).length > 0) await Users.update(user.id, updates);
if (Object.keys(profileUpdates).length > 0) {
await UserProfiles.update({ userId: user.id }, profileUpdates);
await UserProfiles.updateMentions(user.id);
await promiseEarlyReturn(UserProfiles.updateMentions(user.id), 1500);
}
return this.verifyCredentials(ctx);