From 998bb1ae086c95b608ce3494fe9920ad1e70f87d Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 17 Oct 2023 23:51:31 +0200 Subject: [PATCH] [mastodon-api] Only wait up to 1500 ms for mentions to update on /accounts/update_credentials --- packages/backend/src/prelude/promise.ts | 5 +++++ packages/backend/src/server/api/mastodon/helpers/user.ts | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/prelude/promise.ts diff --git a/packages/backend/src/prelude/promise.ts b/packages/backend/src/prelude/promise.ts new file mode 100644 index 000000000..058ed9711 --- /dev/null +++ b/packages/backend/src/prelude/promise.ts @@ -0,0 +1,5 @@ +// Returns T if promise settles before timeout, otherwise returns void, finishing execution in the background. +export async function promiseEarlyReturn(promise: Promise, after: number): Promise { + const timer: Promise = new Promise((res) => setTimeout(() => res(undefined), after)); + return Promise.race([promise, timer]); +} \ No newline at end of file diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 559745288..0a679c3e4 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -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);