From 770c2b061377811ad567cae183c4bd7ca15bf960 Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Fri, 13 Dec 2024 14:34:43 +0800 Subject: [PATCH] models/crate: Add `hasOwnerUser()` fn that relies on `loadOwnerUserTask` This extracts functionality from `isOwner` as a more generic method. The data is loaded explicitly with `loadOwnerUserTask` to avoid deprecations. A debugging assert is also included to inform if perform() is not called before using it. --- app/models/crate.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/models/crate.js b/app/models/crate.js index 1bf87694a8..2c8db61646 100644 --- a/app/models/crate.js +++ b/app/models/crate.js @@ -68,6 +68,12 @@ export default class Crate extends Model { return new Set(map.values()); } + hasOwnerUser(userId) { + let { last } = this.loadOwnerUserTask; + assert('`loadOwnerUserTask.perform()` must be called before calling `hasOwnerUser()`', last != null); + return (last?.value ?? []).some(({ id }) => id === userId); + } + get owners() { let { last } = this.loadOwnersTask; assert('`loadOwnersTask.perform()` must be called before accessing `owners`', last != null); @@ -104,6 +110,10 @@ export default class Crate extends Model { } } + loadOwnerUserTask = task(async () => { + return (await this.owner_user) ?? []; + }); + loadOwnersTask = task(async () => { let [teams, users] = await Promise.all([this.owner_team, this.owner_user]); return [...(teams ?? []), ...(users ?? [])];