|
|
@@ -18,65 +18,70 @@ export const getActor = async (): Promise<Actor.Info> => {
|
|
|
"use server"
|
|
|
const evt = getRequestEvent()
|
|
|
if (!evt) throw new Error("No request event")
|
|
|
- const url = new URL(evt.request.headers.has("x-server-id") ? evt.request.headers.get("referer")! : evt.request.url)
|
|
|
- const auth = await useAuthSession()
|
|
|
- auth.data.account = auth.data.account ?? {}
|
|
|
- const splits = url.pathname.split("/").filter(Boolean)
|
|
|
- if (splits[0] !== "workspace") {
|
|
|
- const current = auth.data.account[auth.data.current ?? ""]
|
|
|
- if (current) {
|
|
|
- return {
|
|
|
- type: "account",
|
|
|
- properties: {
|
|
|
- email: current.email,
|
|
|
- accountID: current.id,
|
|
|
- },
|
|
|
+ if (evt.locals.actor) return evt.locals.actor
|
|
|
+ evt.locals.actor = (async () => {
|
|
|
+ console.log("getActor")
|
|
|
+ const url = new URL(evt.request.headers.has("x-server-id") ? evt.request.headers.get("referer")! : evt.request.url)
|
|
|
+ const auth = await useAuthSession()
|
|
|
+ const splits = url.pathname.split("/").filter(Boolean)
|
|
|
+ if (splits[0] !== "workspace") {
|
|
|
+ const account = auth.data.account ?? {}
|
|
|
+ const current = account[auth.data.current ?? ""]
|
|
|
+ if (current) {
|
|
|
+ return {
|
|
|
+ type: "account",
|
|
|
+ properties: {
|
|
|
+ email: current.email,
|
|
|
+ accountID: current.id,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Object.keys(account).length > 0) {
|
|
|
+ const current = Object.values(account)[0]
|
|
|
+ await auth.update((val) => ({
|
|
|
+ ...val,
|
|
|
+ current: current.id,
|
|
|
+ }))
|
|
|
+ return {
|
|
|
+ type: "account",
|
|
|
+ properties: {
|
|
|
+ email: current.email,
|
|
|
+ accountID: current.id,
|
|
|
+ },
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- if (Object.keys(auth.data.account ?? {}).length > 0) {
|
|
|
- const current = Object.values(auth.data.account)[0]
|
|
|
- await auth.update((val) => ({
|
|
|
- ...val,
|
|
|
- current: current.id,
|
|
|
- }))
|
|
|
return {
|
|
|
- type: "account",
|
|
|
- properties: {
|
|
|
- email: current.email,
|
|
|
- accountID: current.id,
|
|
|
- },
|
|
|
+ type: "public",
|
|
|
+ properties: {},
|
|
|
}
|
|
|
}
|
|
|
- return {
|
|
|
- type: "public",
|
|
|
- properties: {},
|
|
|
- }
|
|
|
- }
|
|
|
- const workspaceHint = splits[1]
|
|
|
- const accounts = Object.keys(auth.data.account ?? {})
|
|
|
- if (accounts.length) {
|
|
|
- const result = await Database.transaction(async (tx) => {
|
|
|
- return await tx
|
|
|
- .select({
|
|
|
- user: UserTable,
|
|
|
- })
|
|
|
- .from(AccountTable)
|
|
|
- .innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email)))
|
|
|
- .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID))
|
|
|
- .where(and(inArray(AccountTable.id, accounts), eq(WorkspaceTable.id, workspaceHint)))
|
|
|
- .limit(1)
|
|
|
- .execute()
|
|
|
- .then((x) => x[0])
|
|
|
- })
|
|
|
- if (result) {
|
|
|
- return {
|
|
|
- type: "user",
|
|
|
- properties: {
|
|
|
- userID: result.user.id,
|
|
|
- workspaceID: result.user.workspaceID,
|
|
|
- },
|
|
|
+ const workspaceHint = splits[1]
|
|
|
+ const accounts = Object.keys(auth.data.account ?? {})
|
|
|
+ if (accounts.length) {
|
|
|
+ const result = await Database.transaction(async (tx) => {
|
|
|
+ return await tx
|
|
|
+ .select({
|
|
|
+ user: UserTable,
|
|
|
+ })
|
|
|
+ .from(AccountTable)
|
|
|
+ .innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email)))
|
|
|
+ .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID))
|
|
|
+ .where(and(inArray(AccountTable.id, accounts), eq(WorkspaceTable.id, workspaceHint)))
|
|
|
+ .limit(1)
|
|
|
+ .execute()
|
|
|
+ .then((x) => x[0])
|
|
|
+ })
|
|
|
+ if (result) {
|
|
|
+ return {
|
|
|
+ type: "user",
|
|
|
+ properties: {
|
|
|
+ userID: result.user.id,
|
|
|
+ workspaceID: result.user.workspaceID,
|
|
|
+ },
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- throw redirect("/auth/authorize")
|
|
|
+ throw redirect("/auth/authorize")
|
|
|
+ })()
|
|
|
+ return evt.locals.actor
|
|
|
}
|