opencode/packages/console/core/src/user.ts

227 lines
6.5 KiB
TypeScript
Raw Normal View History

2025-08-29 04:44:55 +08:00
import { z } from "zod"
2025-10-09 06:59:41 +08:00
import { and, eq, getTableColumns, isNull, sql } from "drizzle-orm"
2025-08-29 04:44:55 +08:00
import { fn } from "./util/fn"
import { Database } from "./drizzle"
2025-10-03 01:58:38 +08:00
import { UserRole, UserTable } from "./schema/user.sql"
import { Actor } from "./actor"
import { Identifier } from "./identifier"
import { render } from "@jsx-email/render"
import { AWS } from "./aws"
2025-10-04 00:54:52 +08:00
import { Key } from "./key"
2025-10-04 04:32:53 +08:00
import { KeyTable } from "./schema/key.sql"
2025-10-11 09:21:55 +08:00
import { WorkspaceTable } from "./schema/workspace.sql"
2025-10-17 10:27:28 +08:00
import { AuthTable } from "./schema/auth.sql"
2025-08-29 04:44:55 +08:00
export namespace User {
2025-10-03 01:58:38 +08:00
const assertNotSelf = (id: string) => {
2025-10-09 06:59:41 +08:00
if (Actor.userID() !== id) return
throw new Error(`Expected not self actor, got self actor`)
2025-10-03 01:58:38 +08:00
}
export const list = fn(z.void(), () =>
Database.use((tx) =>
tx
2025-10-03 05:55:54 +08:00
.select({
...getTableColumns(UserTable),
2025-10-17 10:27:28 +08:00
authEmail: AuthTable.subject,
2025-10-03 05:55:54 +08:00
})
2025-08-29 04:44:55 +08:00
.from(UserTable)
2025-10-17 10:27:28 +08:00
.leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
2025-10-03 01:58:38 +08:00
.where(and(eq(UserTable.workspaceID, Actor.workspace()), isNull(UserTable.timeDeleted))),
),
)
export const fromID = fn(z.string(), (id) =>
Database.use((tx) =>
tx
.select()
.from(UserTable)
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id), isNull(UserTable.timeDeleted)))
.then((rows) => rows[0]),
),
)
2025-10-17 10:27:28 +08:00
export const getAuthEmail = fn(z.string(), (id) =>
2025-10-03 19:45:35 +08:00
Database.use((tx) =>
tx
.select({
2025-10-17 10:27:28 +08:00
email: AuthTable.subject,
2025-10-03 19:45:35 +08:00
})
.from(UserTable)
2025-10-17 10:27:28 +08:00
.leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
2025-10-03 19:45:35 +08:00
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id)))
.then((rows) => rows[0]?.email),
),
)
2025-10-03 01:58:38 +08:00
export const invite = fn(
z.object({
email: z.string(),
role: z.enum(UserRole),
2025-10-11 02:54:49 +08:00
monthlyLimit: z.number().nullable().optional(),
2025-10-03 01:58:38 +08:00
}),
2025-10-11 02:54:49 +08:00
async ({ email, role, monthlyLimit }) => {
2025-10-10 12:02:04 +08:00
Actor.assertAdmin()
2025-10-03 01:58:38 +08:00
const workspaceID = Actor.workspace()
2025-10-04 04:32:53 +08:00
// create user
2025-10-17 10:27:28 +08:00
const accountID = await Database.use((tx) =>
tx
.select({
accountID: AuthTable.accountID,
})
.from(AuthTable)
.where(and(eq(AuthTable.provider, "email"), eq(AuthTable.subject, email)))
.then((rows) => rows[0]?.accountID),
)
2025-10-04 04:32:53 +08:00
await Database.use((tx) =>
tx
.insert(UserTable)
.values({
id: Identifier.create("user"),
name: "",
2025-10-17 10:27:28 +08:00
...(accountID
2025-10-04 04:32:53 +08:00
? {
2025-10-17 10:27:28 +08:00
accountID,
2025-10-04 04:32:53 +08:00
}
: {
email,
}),
workspaceID,
role,
2025-10-11 02:54:49 +08:00
monthlyLimit,
2025-10-04 04:32:53 +08:00
})
.onDuplicateKeyUpdate({
set: {
2025-10-04 00:54:52 +08:00
role,
2025-10-11 02:54:49 +08:00
monthlyLimit,
2025-10-04 00:54:52 +08:00
timeDeleted: null,
2025-10-04 04:32:53 +08:00
},
}),
)
// create api key
2025-10-17 10:27:28 +08:00
if (accountID) {
2025-10-04 13:12:22 +08:00
await Database.use(async (tx) => {
const user = await tx
.select()
.from(UserTable)
2025-10-17 10:27:28 +08:00
.where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, accountID)))
2025-10-04 13:12:22 +08:00
.then((rows) => rows[0])
const key = await tx
.select()
.from(KeyTable)
.where(and(eq(KeyTable.workspaceID, workspaceID), eq(KeyTable.userID, user.id)))
.then((rows) => rows[0])
if (key) return
await Key.create({ userID: user.id, name: "Default API Key" })
})
}
2025-10-03 01:58:38 +08:00
// send email, ignore errors
try {
2025-10-11 09:21:55 +08:00
const emailInfo = await Database.use((tx) =>
tx
.select({
2025-10-17 10:27:28 +08:00
inviterEmail: AuthTable.subject,
2025-10-11 09:21:55 +08:00
workspaceName: WorkspaceTable.name,
})
.from(UserTable)
2025-10-17 10:27:28 +08:00
.innerJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
2025-10-11 09:21:55 +08:00
.innerJoin(WorkspaceTable, eq(WorkspaceTable.id, workspaceID))
.where(
and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.id, Actor.assert("user").properties.userID)),
)
.then((rows) => rows[0]),
)
2025-10-05 09:33:39 +08:00
const { InviteEmail } = await import("@opencode-ai/console-mail/InviteEmail.jsx")
2025-10-03 01:58:38 +08:00
await AWS.sendEmail({
to: email,
2025-10-15 10:52:35 +08:00
subject: `You've been invited to join the ${emailInfo.workspaceName} workspace on OpenCode`,
2025-10-03 01:58:38 +08:00
body: render(
// @ts-ignore
InviteEmail({
2025-10-17 10:27:28 +08:00
inviter: emailInfo.inviterEmail,
2025-10-03 01:58:38 +08:00
assetsUrl: `https://opencode.ai/email`,
2025-10-11 09:21:55 +08:00
workspaceID: workspaceID,
workspaceName: emailInfo.workspaceName,
2025-10-03 01:58:38 +08:00
}),
),
})
} catch (e) {
console.error(e)
}
},
)
2025-10-04 00:54:52 +08:00
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(
2025-10-04 13:12:22 +08:00
"system",
2025-10-04 00:54:52 +08:00
{
workspaceID: invite.workspaceID,
},
2025-10-04 13:12:22 +08:00
() => Key.create({ userID: invite.id, name: "Default API Key" }),
2025-10-04 00:54:52 +08:00
),
),
)
})
2025-10-07 21:17:05 +08:00
export const update = fn(
2025-10-03 01:58:38 +08:00
z.object({
id: z.string(),
role: z.enum(UserRole),
2025-10-07 21:17:05 +08:00
monthlyLimit: z.number().nullable(),
2025-08-29 04:44:55 +08:00
}),
2025-10-07 21:17:05 +08:00
async ({ id, role, monthlyLimit }) => {
2025-10-10 12:02:04 +08:00
Actor.assertAdmin()
2025-10-03 01:58:38 +08:00
if (role === "member") assertNotSelf(id)
return await Database.use((tx) =>
tx
.update(UserTable)
2025-10-07 21:17:05 +08:00
.set({ role, monthlyLimit })
2025-10-03 01:58:38 +08:00
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace()))),
)
},
2025-08-29 04:44:55 +08:00
)
2025-10-03 01:58:38 +08:00
export const remove = fn(z.string(), async (id) => {
2025-10-10 12:02:04 +08:00
Actor.assertAdmin()
2025-10-03 01:58:38 +08:00
assertNotSelf(id)
2025-10-04 04:32:53 +08:00
return await Database.use((tx) =>
tx
2025-10-03 01:58:38 +08:00
.update(UserTable)
.set({
timeDeleted: sql`now()`,
})
2025-10-04 04:32:53 +08:00
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace()))),
)
2025-10-03 01:58:38 +08:00
})
2025-08-29 04:44:55 +08:00
}