mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-19 16:54:22 +01:00
wip: zen
This commit is contained in:
@@ -9,7 +9,7 @@ export default createHandler(
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<link rel="icon" href="/favicon-zen.svg" />
|
<link rel="icon" href="/favicon.svg" />
|
||||||
<meta property="og:image" content="/social-share.png" />
|
<meta property="og:image" content="/social-share.png" />
|
||||||
<meta property="twitter:image" content="/social-share.png" />
|
<meta property="twitter:image" content="/social-share.png" />
|
||||||
{assets}
|
{assets}
|
||||||
|
|||||||
@@ -11,13 +11,17 @@ import { createAsync, query, useParams } from "@solidjs/router"
|
|||||||
import { Actor } from "@opencode/console-core/actor.js"
|
import { Actor } from "@opencode/console-core/actor.js"
|
||||||
import { withActor } from "~/context/auth.withActor"
|
import { withActor } from "~/context/auth.withActor"
|
||||||
import { User } from "@opencode/console-core/user.js"
|
import { User } from "@opencode/console-core/user.js"
|
||||||
|
import { Resource } from "@opencode/console-resource"
|
||||||
|
|
||||||
const getUser = query(async (workspaceID: string) => {
|
const getUser = query(async (workspaceID: string) => {
|
||||||
"use server"
|
"use server"
|
||||||
return withActor(async () => {
|
return withActor(async () => {
|
||||||
const actor = Actor.assert("user")
|
const actor = Actor.assert("user")
|
||||||
const user = await User.fromID(actor.properties.userID)
|
const user = await User.fromID(actor.properties.userID)
|
||||||
return { isAdmin: user?.role === "admin" }
|
return {
|
||||||
|
isAdmin: user?.role === "admin",
|
||||||
|
isBeta: Resource.App.stage === "production" ? workspaceID === "wrk_01K46JDFR0E75SG2Q8K172KF3Y" : true,
|
||||||
|
}
|
||||||
}, workspaceID)
|
}, workspaceID)
|
||||||
}, "user.get")
|
}, "user.get")
|
||||||
|
|
||||||
@@ -41,7 +45,7 @@ export default function () {
|
|||||||
<NewUserSection />
|
<NewUserSection />
|
||||||
<KeySection />
|
<KeySection />
|
||||||
<Show when={data()?.isAdmin}>
|
<Show when={data()?.isAdmin}>
|
||||||
<Show when={isBeta(params.id)}>
|
<Show when={data()?.isBeta}>
|
||||||
<MemberSection />
|
<MemberSection />
|
||||||
</Show>
|
</Show>
|
||||||
<BillingSection />
|
<BillingSection />
|
||||||
@@ -55,11 +59,3 @@ export default function () {
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBeta(workspaceID: string) {
|
|
||||||
return [
|
|
||||||
"wrk_01K46JDFR0E75SG2Q8K172KF3Y", // production
|
|
||||||
"wrk_01K4NFRR5P7FSYWH88307B4DDS", // dev
|
|
||||||
"wrk_01K6G7HBZ7C046A4XK01CVD0NS", // frank
|
|
||||||
].includes(workspaceID)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { and, eq, getTableColumns, isNull, sql } from "drizzle-orm"
|
import { and, eq, getTableColumns, inArray, isNull, or, sql } from "drizzle-orm"
|
||||||
import { fn } from "./util/fn"
|
import { fn } from "./util/fn"
|
||||||
import { Database } from "./drizzle"
|
import { Database } from "./drizzle"
|
||||||
import { UserRole, UserTable } from "./schema/user.sql"
|
import { UserRole, UserTable } from "./schema/user.sql"
|
||||||
import { Actor } from "./actor"
|
import { Actor } from "./actor"
|
||||||
import { Identifier } from "./identifier"
|
import { Identifier } from "./identifier"
|
||||||
import { render } from "@jsx-email/render"
|
import { render } from "@jsx-email/render"
|
||||||
import { InviteEmail } from "@opencode/console-mail/InviteEmail.jsx"
|
|
||||||
import { AWS } from "./aws"
|
import { AWS } from "./aws"
|
||||||
import { Account } from "./account"
|
import { Account } from "./account"
|
||||||
import { AccountTable } from "./schema/account.sql"
|
import { AccountTable } from "./schema/account.sql"
|
||||||
|
import { Key } from "./key"
|
||||||
|
|
||||||
export namespace User {
|
export namespace User {
|
||||||
const assertAdmin = async () => {
|
const assertAdmin = async () => {
|
||||||
@@ -74,71 +74,66 @@ export namespace User {
|
|||||||
const workspaceID = Actor.workspace()
|
const workspaceID = Actor.workspace()
|
||||||
await Database.transaction(async (tx) => {
|
await Database.transaction(async (tx) => {
|
||||||
const account = await Account.fromEmail(email)
|
const account = await Account.fromEmail(email)
|
||||||
const members = await tx.select().from(UserTable).where(eq(UserTable.workspaceID, Actor.workspace()))
|
const existing = await tx
|
||||||
|
.select()
|
||||||
|
.from(UserTable)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(UserTable.workspaceID, Actor.workspace()),
|
||||||
|
account ? eq(UserTable.oldAccountID, account.id) : eq(UserTable.oldEmail, email),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.then((rows) => rows[0])
|
||||||
|
|
||||||
await (async () => {
|
// case: previously invited and removed
|
||||||
if (account) {
|
if (existing) {
|
||||||
// case: account previously invited and removed
|
await tx
|
||||||
if (members.some((m) => m.oldAccountID === account.id)) {
|
.update(UserTable)
|
||||||
await tx
|
.set({
|
||||||
.update(UserTable)
|
role,
|
||||||
.set({
|
timeDeleted: null,
|
||||||
timeDeleted: null,
|
...(account
|
||||||
oldAccountID: null,
|
? {
|
||||||
accountID: account.id,
|
oldAccountID: null,
|
||||||
})
|
accountID: account.id,
|
||||||
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.accountID, account.id)))
|
}
|
||||||
return
|
: {
|
||||||
}
|
oldEmail: null,
|
||||||
// case: account previously not invited
|
email,
|
||||||
await tx
|
}),
|
||||||
.insert(UserTable)
|
})
|
||||||
.values({
|
.where(and(eq(UserTable.workspaceID, existing.workspaceID), eq(UserTable.id, existing.id)))
|
||||||
id: Identifier.create("user"),
|
}
|
||||||
name: "",
|
// case: account previously not invited
|
||||||
accountID: account.id,
|
else {
|
||||||
workspaceID,
|
|
||||||
role,
|
|
||||||
})
|
|
||||||
.catch((e: any) => {
|
|
||||||
if (e.message.match(/Duplicate entry '.*' for key 'user.user_account_id'/))
|
|
||||||
throw new Error("A user with this email has already been invited.")
|
|
||||||
throw e
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// case: email previously invited and removed
|
|
||||||
if (members.some((m) => m.oldEmail === email)) {
|
|
||||||
await tx
|
|
||||||
.update(UserTable)
|
|
||||||
.set({
|
|
||||||
timeDeleted: null,
|
|
||||||
oldEmail: null,
|
|
||||||
email,
|
|
||||||
})
|
|
||||||
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.email, email)))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// case: email previously not invited
|
|
||||||
await tx
|
await tx
|
||||||
.insert(UserTable)
|
.insert(UserTable)
|
||||||
.values({
|
.values({
|
||||||
id: Identifier.create("user"),
|
id: Identifier.create("user"),
|
||||||
name: "",
|
name: "",
|
||||||
email,
|
...(account
|
||||||
|
? {
|
||||||
|
accountID: account.id,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
email,
|
||||||
|
}),
|
||||||
workspaceID,
|
workspaceID,
|
||||||
role,
|
role,
|
||||||
})
|
})
|
||||||
.catch((e: any) => {
|
.catch((e: any) => {
|
||||||
|
if (e.message.match(/Duplicate entry '.*' for key 'user.user_account_id'/))
|
||||||
|
throw new Error("A user with this email has already been invited.")
|
||||||
if (e.message.match(/Duplicate entry '.*' for key 'user.user_email'/))
|
if (e.message.match(/Duplicate entry '.*' for key 'user.user_email'/))
|
||||||
throw new Error("A user with this email has already been invited.")
|
throw new Error("A user with this email has already been invited.")
|
||||||
throw e
|
throw e
|
||||||
})
|
})
|
||||||
})()
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// send email, ignore errors
|
// send email, ignore errors
|
||||||
try {
|
try {
|
||||||
|
const { InviteEmail } = await import("@opencode/console-mail/InviteEmail.jsx")
|
||||||
await AWS.sendEmail({
|
await AWS.sendEmail({
|
||||||
to: email,
|
to: email,
|
||||||
subject: `You've been invited to join the ${workspaceID} workspace on OpenCode Zen`,
|
subject: `You've been invited to join the ${workspaceID} workspace on OpenCode Zen`,
|
||||||
@@ -156,6 +151,42 @@ export namespace User {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export const joinInvitedWorkspaces = fn(z.void(), async () => {
|
||||||
|
const account = Actor.assert("account")
|
||||||
|
const invitations = await Database.use(async (tx) => {
|
||||||
|
const invitations = await tx
|
||||||
|
.select({
|
||||||
|
id: UserTable.id,
|
||||||
|
workspaceID: UserTable.workspaceID,
|
||||||
|
})
|
||||||
|
.from(UserTable)
|
||||||
|
.where(eq(UserTable.email, account.properties.email))
|
||||||
|
|
||||||
|
await tx
|
||||||
|
.update(UserTable)
|
||||||
|
.set({
|
||||||
|
accountID: account.properties.accountID,
|
||||||
|
email: null,
|
||||||
|
})
|
||||||
|
.where(eq(UserTable.email, account.properties.email))
|
||||||
|
return invitations
|
||||||
|
})
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
invitations.map((invite) =>
|
||||||
|
Actor.provide(
|
||||||
|
"system",
|
||||||
|
{
|
||||||
|
workspaceID: invite.workspaceID,
|
||||||
|
},
|
||||||
|
() => Key.create({ name: "Default API Key" }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return invitations.length
|
||||||
|
})
|
||||||
|
|
||||||
export const updateRole = fn(
|
export const updateRole = fn(
|
||||||
z.object({
|
z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { Account } from "@opencode/console-core/account.js"
|
|||||||
import { Workspace } from "@opencode/console-core/workspace.js"
|
import { Workspace } from "@opencode/console-core/workspace.js"
|
||||||
import { Actor } from "@opencode/console-core/actor.js"
|
import { Actor } from "@opencode/console-core/actor.js"
|
||||||
import { Resource } from "@opencode/console-resource"
|
import { Resource } from "@opencode/console-resource"
|
||||||
import { Database } from "@opencode/console-core/drizzle/index.js"
|
import { User } from "@opencode/console-core/user.js"
|
||||||
|
|
||||||
type Env = {
|
type Env = {
|
||||||
AuthStorage: KVNamespace
|
AuthStorage: KVNamespace
|
||||||
@@ -123,8 +123,8 @@ export default {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
await Actor.provide("account", { accountID, email }, async () => {
|
await Actor.provide("account", { accountID, email }, async () => {
|
||||||
const workspaces = await Account.workspaces()
|
const workspaceCount = await User.joinInvitedWorkspaces()
|
||||||
if (workspaces.length === 0) {
|
if (workspaceCount === 0) {
|
||||||
await Workspace.create()
|
await Workspace.create()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
|
"jsx": "preserve",
|
||||||
|
"jsxImportSource": "react",
|
||||||
"types": ["@cloudflare/workers-types", "node"]
|
"types": ["@cloudflare/workers-types", "node"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user