mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-06 17:34:58 +01:00
wip: cloud
This commit is contained in:
@@ -18,65 +18,70 @@ export const getActor = async (): Promise<Actor.Info> => {
|
|||||||
"use server"
|
"use server"
|
||||||
const evt = getRequestEvent()
|
const evt = getRequestEvent()
|
||||||
if (!evt) throw new Error("No request event")
|
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)
|
if (evt.locals.actor) return evt.locals.actor
|
||||||
const auth = await useAuthSession()
|
evt.locals.actor = (async () => {
|
||||||
auth.data.account = auth.data.account ?? {}
|
console.log("getActor")
|
||||||
const splits = url.pathname.split("/").filter(Boolean)
|
const url = new URL(evt.request.headers.has("x-server-id") ? evt.request.headers.get("referer")! : evt.request.url)
|
||||||
if (splits[0] !== "workspace") {
|
const auth = await useAuthSession()
|
||||||
const current = auth.data.account[auth.data.current ?? ""]
|
const splits = url.pathname.split("/").filter(Boolean)
|
||||||
if (current) {
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
type: "account",
|
type: "public",
|
||||||
properties: {
|
properties: {},
|
||||||
email: current.email,
|
|
||||||
accountID: current.id,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(auth.data.account ?? {}).length > 0) {
|
const workspaceHint = splits[1]
|
||||||
const current = Object.values(auth.data.account)[0]
|
const accounts = Object.keys(auth.data.account ?? {})
|
||||||
await auth.update((val) => ({
|
if (accounts.length) {
|
||||||
...val,
|
const result = await Database.transaction(async (tx) => {
|
||||||
current: current.id,
|
return await tx
|
||||||
}))
|
.select({
|
||||||
return {
|
user: UserTable,
|
||||||
type: "account",
|
})
|
||||||
properties: {
|
.from(AccountTable)
|
||||||
email: current.email,
|
.innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email)))
|
||||||
accountID: current.id,
|
.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,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
throw redirect("/auth/authorize")
|
||||||
type: "public",
|
})()
|
||||||
properties: {},
|
return evt.locals.actor
|
||||||
}
|
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Actor } from "@opencode/cloud-core/actor.js"
|
import { Actor } from "@opencode/cloud-core/actor.js"
|
||||||
import { getActor } from "./auth"
|
import { getActor } from "./auth"
|
||||||
|
import { getRequestEvent } from "solid-js/web"
|
||||||
|
|
||||||
export async function withActor<T>(fn: () => T) {
|
export async function withActor<T>(fn: () => T) {
|
||||||
const actor = await getActor()
|
const actor = await getActor()
|
||||||
|
|||||||
@@ -42,12 +42,14 @@ const getBillingInfo = query(async () => {
|
|||||||
"use server"
|
"use server"
|
||||||
return withActor(async () => {
|
return withActor(async () => {
|
||||||
const actor = Actor.assert("user")
|
const actor = Actor.assert("user")
|
||||||
|
const now = Date.now()
|
||||||
const [user, billing, payments, usage] = await Promise.all([
|
const [user, billing, payments, usage] = await Promise.all([
|
||||||
User.fromID(actor.properties.userID),
|
User.fromID(actor.properties.userID),
|
||||||
Billing.get(),
|
Billing.get(),
|
||||||
Billing.payments(),
|
Billing.payments(),
|
||||||
Billing.usages(),
|
Billing.usages(),
|
||||||
])
|
])
|
||||||
|
console.log("duration", Date.now() - now)
|
||||||
return { user, billing, payments, usage }
|
return { user, billing, payments, usage }
|
||||||
})
|
})
|
||||||
}, "billingInfo")
|
}, "billingInfo")
|
||||||
@@ -67,9 +69,7 @@ export default function () {
|
|||||||
/////////////////
|
/////////////////
|
||||||
// Keys section
|
// Keys section
|
||||||
/////////////////
|
/////////////////
|
||||||
const keys = createAsync(() => listKeys(), {
|
const keys = createAsync(() => listKeys())
|
||||||
deferStream: true,
|
|
||||||
})
|
|
||||||
const createKeyAction = useAction(createKey)
|
const createKeyAction = useAction(createKey)
|
||||||
const removeKeyAction = useAction(removeKey)
|
const removeKeyAction = useAction(removeKey)
|
||||||
const createKeySubmission = useSubmission(createKey)
|
const createKeySubmission = useSubmission(createKey)
|
||||||
|
|||||||
Reference in New Issue
Block a user