| 38 | } |
| 39 | |
| 40 | export const getActor = async (workspace?: string): Promise<Actor.Info> => { |
| 41 | "use server" |
| 42 | const evt = getRequestEvent() |
| 43 | if (!evt) throw new Error("No request event") |
| 44 | if (evt.locals.actor) return evt.locals.actor |
| 45 | evt.locals.actor = (async () => { |
| 46 | const auth = await useAuthSession() |
| 47 | if (!workspace) { |
| 48 | const account = auth.data.account ?? {} |
| 49 | const current = account[auth.data.current ?? ""] |
| 50 | if (current) { |
| 51 | return { |
| 52 | type: "account", |
| 53 | properties: { |
| 54 | email: current.email, |
| 55 | accountID: current.id, |
| 56 | }, |
| 57 | } |
| 58 | } |
| 59 | if (Object.keys(account).length > 0) { |
| 60 | const current = Object.values(account)[0] |
| 61 | await auth.update((val) => ({ |
| 62 | ...val, |
| 63 | current: current.id, |
| 64 | })) |
| 65 | return { |
| 66 | type: "account", |
| 67 | properties: { |
| 68 | email: current.email, |
| 69 | accountID: current.id, |
| 70 | }, |
| 71 | } |
| 72 | } |
| 73 | return { |
| 74 | type: "public", |
| 75 | properties: {}, |
| 76 | } |
| 77 | } |
| 78 | const accounts = Object.keys(auth.data.account ?? {}) |
| 79 | if (accounts.length) { |
| 80 | const user = await Database.use((tx) => |
| 81 | tx |
| 82 | .select() |
| 83 | .from(UserTable) |
| 84 | .where( |
| 85 | and( |
| 86 | eq(UserTable.workspaceID, workspace), |
| 87 | isNull(UserTable.timeDeleted), |
| 88 | inArray(UserTable.accountID, accounts), |
| 89 | ), |
| 90 | ) |
| 91 | .limit(1) |
| 92 | .execute() |
| 93 | .then((x) => x[0]), |
| 94 | ) |
| 95 | if (user) { |
| 96 | await Database.use((tx) => |
| 97 | tx |