@myrialabs/clopen 0.4.47 → 0.4.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/backend/artifacts/project/sources.ts +1 -1
- package/backend/auth/auth-service.ts +52 -358
- package/backend/auth/onboarding.test.ts +2 -3
- package/backend/auth/permissions.test.ts +10 -20
- package/backend/auth/permissions.ts +4 -290
- package/backend/auth/rate-limiter.test.ts +8 -9
- package/backend/auth/rate-limiter.ts +0 -2
- package/backend/auth/tokens.ts +1 -8
- package/backend/bootstrap.ts +7 -0
- package/backend/chat/attachments.test.ts +88 -0
- package/backend/chat/attachments.ts +159 -0
- package/backend/chat/engine-handoff.test.ts +0 -2
- package/backend/chat/stream-manager.ts +62 -62
- package/backend/chat/turn-answer.test.ts +64 -0
- package/backend/chat/turn-answer.ts +125 -0
- package/backend/chat-apps/awaiting.test.ts +39 -0
- package/backend/chat-apps/awaiting.ts +49 -0
- package/backend/chat-apps/conversation.ts +746 -0
- package/backend/chat-apps/inbound.ts +324 -0
- package/backend/chat-apps/index.ts +309 -0
- package/backend/chat-apps/link-events.ts +20 -0
- package/backend/chat-apps/media.test.ts +62 -0
- package/backend/chat-apps/media.ts +208 -0
- package/backend/chat-apps/messages.test.ts +229 -0
- package/backend/chat-apps/messages.ts +365 -0
- package/backend/chat-apps/outbound.ts +243 -0
- package/backend/chat-apps/pairing.test.ts +45 -0
- package/backend/chat-apps/pairing.ts +74 -0
- package/backend/chat-apps/platforms/index.ts +20 -0
- package/backend/chat-apps/platforms/telegram.test.ts +35 -0
- package/backend/chat-apps/platforms/telegram.ts +191 -0
- package/backend/chat-apps/platforms/types.ts +114 -0
- package/backend/chat-apps/questions.ts +133 -0
- package/backend/chat-apps/runtime.ts +572 -0
- package/backend/chat-apps/storage.test.ts +201 -0
- package/backend/chat-apps/turns.test.ts +45 -0
- package/backend/chat-apps/turns.ts +208 -0
- package/backend/containers/pty.ts +3 -11
- package/backend/control/actions/accounts.ts +89 -0
- package/backend/control/actions/files.ts +116 -0
- package/backend/control/actions/models.ts +188 -0
- package/backend/control/actions/projects.ts +272 -0
- package/backend/control/actions/sessions.ts +280 -0
- package/backend/control/actions/skills.ts +111 -0
- package/backend/control/control.test.ts +169 -0
- package/backend/control/define.ts +20 -0
- package/backend/control/folders.test.ts +67 -0
- package/backend/control/index.ts +213 -0
- package/backend/control/invoke.ts +44 -0
- package/backend/control/links.ts +35 -0
- package/backend/control/paging.ts +26 -0
- package/backend/control/types.ts +117 -0
- package/backend/control/workspace.ts +127 -0
- package/backend/database/migrations/084_create_channel_links.ts +71 -0
- package/backend/database/migrations/085_add_home_projects.ts +36 -0
- package/backend/database/migrations/086_single_owner.test.ts +170 -0
- package/backend/database/migrations/086_single_owner.ts +418 -0
- package/backend/database/migrations/index.ts +21 -0
- package/backend/database/queries/auth-queries.ts +7 -85
- package/backend/database/queries/channel-queries.ts +270 -0
- package/backend/database/queries/db-client-connection-queries.ts +18 -56
- package/backend/database/queries/file-share-queries.ts +5 -23
- package/backend/database/queries/git-identity-queries.ts +31 -42
- package/backend/database/queries/graph-queries.ts +1 -1
- package/backend/database/queries/index.ts +13 -1
- package/backend/database/queries/message-queries.test.ts +146 -0
- package/backend/database/queries/message-queries.ts +42 -1
- package/backend/database/queries/permission-set-queries.ts +1 -1
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
- package/backend/database/queries/project-queries.ts +50 -63
- package/backend/database/queries/session-queries.ts +28 -56
- package/backend/database/queries/ssh-connection-queries.ts +18 -41
- package/backend/db-client/integrations/projector.ts +0 -5
- package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
- package/backend/engine/adapters/claude/message-converter.ts +25 -2
- package/backend/engine/adapters/cline/message-converter.ts +0 -1
- package/backend/engine/adapters/cline/stream.ts +11 -15
- package/backend/engine/adapters/codex/message-converter.ts +0 -2
- package/backend/engine/adapters/copilot/message-converter.ts +1 -2
- package/backend/engine/adapters/cursor/message-converter.ts +0 -1
- package/backend/engine/adapters/opencode/message-converter.ts +2 -4
- package/backend/engine/adapters/opencode/stream.ts +11 -0
- package/backend/engine/adapters/parent-scope.test.ts +38 -0
- package/backend/engine/adapters/parent-scope.ts +44 -0
- package/backend/engine/adapters/pi/message-converter.ts +0 -1
- package/backend/engine/adapters/pi/stream.ts +4 -5
- package/backend/engine/adapters/qwen/message-converter.ts +0 -2
- package/backend/engine/bootstrap-default-engine.ts +2 -2
- package/backend/engine/docs/adapter-contract.md +8 -1
- package/backend/engine/install-runner.ts +8 -17
- package/backend/files/file-shares.ts +20 -44
- package/backend/files/file-size-limit.ts +3 -3
- package/backend/files/image-processing.ts +1 -1
- package/backend/files/os-clipboard.ts +2 -2
- package/backend/git/git-executor.ts +25 -5
- package/backend/git/git-service.ts +420 -19
- package/backend/git/identity/credential-helper.test.ts +2 -7
- package/backend/git/identity/import-local.test.ts +11 -16
- package/backend/git/identity/import-local.ts +12 -12
- package/backend/git/identity/index.ts +10 -24
- package/backend/git/identity/resolve.ts +13 -14
- package/backend/git/identity/scope-config.ts +33 -28
- package/backend/git/identity/service.ts +44 -47
- package/backend/git/op-lock.test.ts +111 -0
- package/backend/git/op-lock.ts +180 -0
- package/backend/git/remote-sync.test.ts +249 -0
- package/backend/host/metrics.ts +1 -1
- package/backend/http/bearer-auth.ts +1 -2
- package/backend/http/files-download.test.ts +8 -9
- package/backend/http/files-download.ts +2 -2
- package/backend/http/files-shared.test.ts +62 -89
- package/backend/http/files-shared.ts +2 -2
- package/backend/http/files-upload.test.ts +3 -7
- package/backend/http/files-upload.ts +3 -3
- package/backend/http/notes-images.test.ts +15 -38
- package/backend/http/notes-images.ts +6 -8
- package/backend/http/ssh-sftp.ts +5 -10
- package/backend/index.ts +21 -7
- package/backend/integrations/accounts.ts +61 -3
- package/backend/integrations/index.ts +2 -2
- package/backend/integrations/registry/define.ts +15 -0
- package/backend/integrations/registry/index.ts +2 -0
- package/backend/integrations/registry/providers/telegram.ts +54 -0
- package/backend/integrations/registry/types.ts +21 -1
- package/backend/integrations/webhooks/gateway.ts +7 -6
- package/backend/integrations/webhooks/verify.test.ts +30 -0
- package/backend/integrations/webhooks/verify.ts +4 -0
- package/backend/mcp/external/project.ts +1 -1
- package/backend/mcp/internal/remote-server.test.ts +1 -3
- package/backend/mcp/internal/remote-server.ts +4 -4
- package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
- package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
- package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
- package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
- package/backend/memory/redact.ts +1 -1
- package/backend/notes/access.test.ts +28 -29
- package/backend/notes/access.ts +6 -11
- package/backend/permissions/service.ts +1 -1
- package/backend/preview/browser/browser-clipboard.test.ts +46 -0
- package/backend/preview/browser/browser-clipboard.ts +67 -0
- package/backend/preview/browser/browser-dialog-handler.ts +95 -6
- package/backend/preview/browser/browser-host-bridge.ts +9 -0
- package/backend/preview/browser/browser-interaction-handler.ts +316 -86
- package/backend/preview/browser/browser-mcp-control.ts +34 -0
- package/backend/preview/browser/browser-pool.ts +67 -6
- package/backend/preview/browser/browser-preview-service.ts +361 -25
- package/backend/preview/browser/browser-tab-manager.ts +66 -23
- package/backend/preview/browser/browser-video-capture.ts +92 -5
- package/backend/preview/browser/element-locator.ts +190 -17
- package/backend/preview/browser/keyboard-chords.test.ts +68 -0
- package/backend/preview/browser/keyboard-chords.ts +132 -0
- package/backend/preview/browser/paste.ts +81 -0
- package/backend/preview/browser/process-usage.ts +142 -0
- package/backend/preview/browser/profile-lock.ts +209 -0
- package/backend/preview/browser/scripts/host-bridge.ts +59 -0
- package/backend/preview/browser/scripts/video-stream.ts +58 -0
- package/backend/preview/browser/types.ts +40 -2
- package/backend/preview/browser/viewport.test.ts +45 -0
- package/backend/preview/browser/viewport.ts +96 -0
- package/backend/profiles/service.ts +2 -2
- package/backend/project/status-manager.ts +43 -172
- package/backend/projects/home.ts +61 -0
- package/backend/projects/project-cleanup.integration.test.ts +2 -2
- package/backend/projects/project-cleanup.ts +0 -8
- package/backend/skills/service.ts +2 -3
- package/backend/snapshot/helpers.ts +0 -1
- package/backend/snapshot/restore.ts +252 -0
- package/backend/ssh/ptykit.ts +1 -2
- package/backend/terminal/ptykit.ts +5 -8
- package/backend/tunnel/public-origin.ts +31 -20
- package/backend/utils/user-helpers.ts +0 -55
- package/backend/utils/ws.ts +61 -154
- package/backend/worktrees/branching/engine.ts +3 -16
- package/backend/worktrees/branching/index.ts +1 -1
- package/backend/worktrees/branching/projector.ts +1 -1
- package/backend/worktrees/branching/service.ts +13 -25
- package/backend/ws/access.ts +12 -21
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/auth/device.ts +16 -71
- package/backend/ws/auth/index.ts +4 -22
- package/backend/ws/auth/login.test.ts +14 -60
- package/backend/ws/auth/login.ts +24 -124
- package/backend/ws/auth/status.ts +0 -1
- package/backend/ws/chat/composer.ts +118 -118
- package/backend/ws/chat/stream.ts +64 -47
- package/backend/ws/chat-apps/index.ts +117 -0
- package/backend/ws/containers/actions.ts +11 -25
- package/backend/ws/containers/index.ts +3 -4
- package/backend/ws/containers/list.ts +10 -36
- package/backend/ws/containers/logs.ts +2 -2
- package/backend/ws/containers/tunnel.ts +1 -2
- package/backend/ws/db-client/access.ts +2 -16
- package/backend/ws/db-client/accounts.ts +5 -15
- package/backend/ws/db-client/connections.ts +17 -25
- package/backend/ws/db-client/context.ts +1 -1
- package/backend/ws/db-client/env.ts +7 -10
- package/backend/ws/db-client/index.ts +2 -2
- package/backend/ws/db-client/io.ts +5 -5
- package/backend/ws/db-client/query-history-retention.test.ts +8 -8
- package/backend/ws/db-client/query.ts +18 -17
- package/backend/ws/db-client/schema.ts +13 -13
- package/backend/ws/db-client/structure.ts +35 -35
- package/backend/ws/db-client/supabase.ts +4 -21
- package/backend/ws/deployments/context.ts +1 -1
- package/backend/ws/deployments/crud.ts +0 -5
- package/backend/ws/engine/access.ts +7 -20
- package/backend/ws/engine/claude/accounts.ts +4 -4
- package/backend/ws/engine/cline/accounts.ts +5 -5
- package/backend/ws/engine/codex/accounts.ts +3 -3
- package/backend/ws/engine/pi/accounts.ts +5 -5
- package/backend/ws/files/clipboard.ts +8 -27
- package/backend/ws/files/image-edit.ts +6 -6
- package/backend/ws/files/index.ts +1 -1
- package/backend/ws/files/path-access.test.ts +32 -131
- package/backend/ws/files/path-access.ts +8 -87
- package/backend/ws/files/read.ts +15 -19
- package/backend/ws/files/reveal.ts +3 -3
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/share.ts +9 -13
- package/backend/ws/files/state.ts +9 -12
- package/backend/ws/files/watch.ts +3 -3
- package/backend/ws/files/write.ts +30 -31
- package/backend/ws/git/branch.ts +80 -59
- package/backend/ws/git/commit.ts +63 -44
- package/backend/ws/git/conflict.ts +16 -18
- package/backend/ws/git/identity-context.ts +8 -17
- package/backend/ws/git/identity.ts +17 -27
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/op-state.ts +139 -0
- package/backend/ws/git/remote.ts +241 -141
- package/backend/ws/git/repo-context.ts +67 -0
- package/backend/ws/git/staging.ts +18 -28
- package/backend/ws/git/status.ts +5 -2
- package/backend/ws/index.ts +5 -1
- package/backend/ws/instructions/crud.ts +1 -1
- package/backend/ws/integrations/crud.ts +1 -3
- package/backend/ws/mcp/crud.ts +3 -5
- package/backend/ws/mcp/tools.ts +1 -1
- package/backend/ws/memory/crud.ts +14 -46
- package/backend/ws/memory/index.ts +2 -2
- package/backend/ws/messages/crud.ts +29 -8
- package/backend/ws/notes/index.ts +33 -39
- package/backend/ws/permissions/crud.ts +3 -4
- package/backend/ws/ports/index.ts +1 -2
- package/backend/ws/ports/kill.ts +5 -10
- package/backend/ws/ports/list.ts +4 -10
- package/backend/ws/preview/access.ts +1 -1
- package/backend/ws/preview/browser/interact.ts +52 -79
- package/backend/ws/preview/browser/native-ui.ts +24 -0
- package/backend/ws/preview/browser/stats.ts +98 -1
- package/backend/ws/preview/browser/tab-info.ts +15 -0
- package/backend/ws/preview/browser/tab.ts +19 -4
- package/backend/ws/preview/index.ts +27 -0
- package/backend/ws/profiles/crud.ts +6 -8
- package/backend/ws/project-artifacts/index.ts +30 -31
- package/backend/ws/projects/crud.ts +33 -35
- package/backend/ws/projects/index.ts +3 -8
- package/backend/ws/projects/info.ts +3 -3
- package/backend/ws/projects/overview.ts +19 -30
- package/backend/ws/projects/status.ts +17 -57
- package/backend/ws/sessions/crud.ts +34 -45
- package/backend/ws/sessions/index.ts +1 -1
- package/backend/ws/settings/crud.ts +1 -1
- package/backend/ws/skills/crud.ts +3 -4
- package/backend/ws/skills/index.ts +1 -1
- package/backend/ws/snapshot/changes.ts +7 -7
- package/backend/ws/snapshot/restore.ts +11 -295
- package/backend/ws/snapshot/timeline.ts +2 -4
- package/backend/ws/ssh/access.ts +6 -21
- package/backend/ws/ssh/connections.ts +21 -31
- package/backend/ws/ssh/forwards.ts +12 -12
- package/backend/ws/ssh/sftp.ts +34 -34
- package/backend/ws/ssh/tunnel.ts +1 -2
- package/backend/ws/stack/install.ts +7 -17
- package/backend/ws/subagents/crud.ts +1 -1
- package/backend/ws/tunnel/share.ts +0 -2
- package/backend/ws/user/crud.ts +1 -59
- package/backend/ws/user/index.ts +1 -1
- package/backend/ws/work/context.ts +1 -1
- package/backend/ws/work/crud.ts +0 -5
- package/backend/ws/workspace/state.ts +10 -13
- package/backend/ws/worktrees/branching.ts +7 -36
- package/backend/ws/worktrees/crud.ts +2 -2
- package/bin/clopen.ts +10 -11
- package/dist/assets/{index-CwuKQelV.js → index-CONlwAXZ.js} +1 -1
- package/dist/assets/{index-BTcR1byF.js → index-DJzwt-hq.js} +1 -1
- package/dist/assets/main-BUiUL3eZ.css +1 -0
- package/dist/assets/main-nBOb9YOA.js +1240 -0
- package/dist/index.html +2 -2
- package/package.json +2 -1
- package/shared/constants/attachments.ts +14 -0
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/preview.test.ts +259 -0
- package/shared/constants/preview.ts +1018 -57
- package/shared/constants/tool-icons.ts +5 -0
- package/shared/types/containers/container.ts +1 -1
- package/shared/types/database/schema.ts +9 -6
- package/shared/types/git-identity.ts +1 -1
- package/shared/types/git.ts +116 -0
- package/shared/types/integrations.ts +7 -0
- package/shared/types/stores/settings.ts +1 -1
- package/shared/types/unified/index.ts +1 -1
- package/shared/types/unified/message.ts +0 -7
- package/shared/types/unified/stream.ts +2 -4
- package/shared/types/unified/tool.ts +15 -1
- package/shared/utils/ask-user-question.test.ts +319 -0
- package/shared/utils/ask-user-question.ts +154 -0
- package/shared/utils/home-project.ts +25 -0
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/ws-server.ts +32 -7
- package/backend/auth/session-invalidation.test.ts +0 -112
- package/backend/auth/session-invalidation.ts +0 -77
- package/backend/project/status-manager.test.ts +0 -32
- package/backend/ws/auth/invites.ts +0 -94
- package/backend/ws/auth/users.ts +0 -112
- package/backend/ws/projects/presence.ts +0 -46
- package/backend/ws/stack/access.ts +0 -31
- package/dist/assets/main-BiDNMWb3.css +0 -1
- package/dist/assets/main-BlhKWz2B.js +0 -1241
|
@@ -5,7 +5,6 @@ export interface DBUser {
|
|
|
5
5
|
name: string;
|
|
6
6
|
color: string;
|
|
7
7
|
avatar: string;
|
|
8
|
-
role: 'admin' | 'member';
|
|
9
8
|
personal_access_token_hash: string | null;
|
|
10
9
|
created_at: string;
|
|
11
10
|
updated_at: string;
|
|
@@ -20,24 +19,10 @@ export interface DBAuthSession {
|
|
|
20
19
|
last_active_at: string;
|
|
21
20
|
user_agent: string | null;
|
|
22
21
|
ip_address: string | null;
|
|
23
|
-
/** How the session was created: 'setup' | '
|
|
22
|
+
/** How the session was created: 'setup' | 'device-link' | 'login' | 'pat' | 'no-auth'. */
|
|
24
23
|
source: string | null;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
export interface DBInviteToken {
|
|
28
|
-
id: string;
|
|
29
|
-
token_hash: string;
|
|
30
|
-
role: 'admin' | 'member';
|
|
31
|
-
label: string | null;
|
|
32
|
-
created_by: string;
|
|
33
|
-
max_uses: number;
|
|
34
|
-
use_count: number;
|
|
35
|
-
expires_at: string | null;
|
|
36
|
-
created_at: string;
|
|
37
|
-
/** JSON array of project ids to grant the new member on join (null = none). */
|
|
38
|
-
project_ids: string | null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
26
|
export interface DBDeviceCode {
|
|
42
27
|
id: string;
|
|
43
28
|
code_hash: string;
|
|
@@ -55,14 +40,13 @@ export const authQueries = {
|
|
|
55
40
|
const db = getDatabase();
|
|
56
41
|
const now = new Date().toISOString();
|
|
57
42
|
db.prepare(`
|
|
58
|
-
INSERT INTO users (id, name, color, avatar,
|
|
59
|
-
VALUES (?, ?, ?, ?, ?, ?,
|
|
43
|
+
INSERT INTO users (id, name, color, avatar, personal_access_token_hash, created_at, updated_at)
|
|
44
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
60
45
|
`).run(
|
|
61
46
|
user.id,
|
|
62
47
|
user.name,
|
|
63
48
|
user.color,
|
|
64
49
|
user.avatar,
|
|
65
|
-
user.role,
|
|
66
50
|
user.personal_access_token_hash,
|
|
67
51
|
user.created_at,
|
|
68
52
|
user.updated_at ?? now
|
|
@@ -80,9 +64,10 @@ export const authQueries = {
|
|
|
80
64
|
return db.prepare('SELECT * FROM users WHERE personal_access_token_hash = ?').get(hash) as DBUser | null;
|
|
81
65
|
},
|
|
82
66
|
|
|
83
|
-
|
|
67
|
+
/** The owner — the install's only account — or null before setup. */
|
|
68
|
+
getOwner(): DBUser | null {
|
|
84
69
|
const db = getDatabase();
|
|
85
|
-
return db.prepare('SELECT * FROM users ORDER BY created_at ASC').
|
|
70
|
+
return db.prepare('SELECT * FROM users ORDER BY created_at ASC LIMIT 1').get() as DBUser | null;
|
|
86
71
|
},
|
|
87
72
|
|
|
88
73
|
countUsers(): number {
|
|
@@ -91,12 +76,6 @@ export const authQueries = {
|
|
|
91
76
|
return result.count;
|
|
92
77
|
},
|
|
93
78
|
|
|
94
|
-
countAdmins(): number {
|
|
95
|
-
const db = getDatabase();
|
|
96
|
-
const result = db.prepare("SELECT COUNT(*) as count FROM users WHERE role = 'admin'").get() as { count: number };
|
|
97
|
-
return result.count;
|
|
98
|
-
},
|
|
99
|
-
|
|
100
79
|
updateUser(id: string, fields: Partial<Pick<DBUser, 'name' | 'color' | 'avatar' | 'personal_access_token_hash'>>): void {
|
|
101
80
|
const db = getDatabase();
|
|
102
81
|
const now = new Date().toISOString();
|
|
@@ -112,11 +91,6 @@ export const authQueries = {
|
|
|
112
91
|
db.prepare(`UPDATE users SET ${sets.join(', ')} WHERE id = ?`).run(...values);
|
|
113
92
|
},
|
|
114
93
|
|
|
115
|
-
deleteUser(id: string): void {
|
|
116
|
-
const db = getDatabase();
|
|
117
|
-
db.prepare('DELETE FROM users WHERE id = ?').run(id);
|
|
118
|
-
},
|
|
119
|
-
|
|
120
94
|
// ===================== Auth Sessions =====================
|
|
121
95
|
|
|
122
96
|
createSession(session: DBAuthSession): DBAuthSession {
|
|
@@ -143,12 +117,7 @@ export const authQueries = {
|
|
|
143
117
|
return db.prepare('SELECT * FROM auth_sessions WHERE token_hash = ?').get(hash) as DBAuthSession | null;
|
|
144
118
|
},
|
|
145
119
|
|
|
146
|
-
|
|
147
|
-
const db = getDatabase();
|
|
148
|
-
return db.prepare('SELECT * FROM auth_sessions WHERE user_id = ? ORDER BY last_active_at DESC').all(userId) as DBAuthSession[];
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
/** All sessions across every user (admin "Connected devices" view). */
|
|
120
|
+
/** Every signed-in device. */
|
|
152
121
|
getAllSessions(): DBAuthSession[] {
|
|
153
122
|
const db = getDatabase();
|
|
154
123
|
return db.prepare('SELECT * FROM auth_sessions ORDER BY last_active_at DESC').all() as DBAuthSession[];
|
|
@@ -175,11 +144,6 @@ export const authQueries = {
|
|
|
175
144
|
db.prepare('DELETE FROM auth_sessions WHERE token_hash = ?').run(hash);
|
|
176
145
|
},
|
|
177
146
|
|
|
178
|
-
deleteSessionsByUserId(userId: string): void {
|
|
179
|
-
const db = getDatabase();
|
|
180
|
-
db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(userId);
|
|
181
|
-
},
|
|
182
|
-
|
|
183
147
|
deleteExpiredSessions(): number {
|
|
184
148
|
const db = getDatabase();
|
|
185
149
|
const now = new Date().toISOString();
|
|
@@ -187,48 +151,6 @@ export const authQueries = {
|
|
|
187
151
|
return result.changes;
|
|
188
152
|
},
|
|
189
153
|
|
|
190
|
-
// ===================== Invite Tokens =====================
|
|
191
|
-
|
|
192
|
-
createInvite(invite: DBInviteToken): DBInviteToken {
|
|
193
|
-
const db = getDatabase();
|
|
194
|
-
db.prepare(`
|
|
195
|
-
INSERT INTO invite_tokens (id, token_hash, role, label, created_by, max_uses, use_count, expires_at, created_at, project_ids)
|
|
196
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
197
|
-
`).run(
|
|
198
|
-
invite.id,
|
|
199
|
-
invite.token_hash,
|
|
200
|
-
invite.role,
|
|
201
|
-
invite.label,
|
|
202
|
-
invite.created_by,
|
|
203
|
-
invite.max_uses,
|
|
204
|
-
invite.use_count,
|
|
205
|
-
invite.expires_at,
|
|
206
|
-
invite.created_at,
|
|
207
|
-
invite.project_ids
|
|
208
|
-
);
|
|
209
|
-
return db.prepare('SELECT * FROM invite_tokens WHERE id = ?').get(invite.id) as DBInviteToken;
|
|
210
|
-
},
|
|
211
|
-
|
|
212
|
-
getInviteByTokenHash(hash: string): DBInviteToken | null {
|
|
213
|
-
const db = getDatabase();
|
|
214
|
-
return db.prepare('SELECT * FROM invite_tokens WHERE token_hash = ?').get(hash) as DBInviteToken | null;
|
|
215
|
-
},
|
|
216
|
-
|
|
217
|
-
incrementUseCount(id: string): void {
|
|
218
|
-
const db = getDatabase();
|
|
219
|
-
db.prepare('UPDATE invite_tokens SET use_count = use_count + 1 WHERE id = ?').run(id);
|
|
220
|
-
},
|
|
221
|
-
|
|
222
|
-
getAllInvites(): DBInviteToken[] {
|
|
223
|
-
const db = getDatabase();
|
|
224
|
-
return db.prepare('SELECT * FROM invite_tokens ORDER BY created_at DESC').all() as DBInviteToken[];
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
revokeInvite(id: string): void {
|
|
228
|
-
const db = getDatabase();
|
|
229
|
-
db.prepare('DELETE FROM invite_tokens WHERE id = ?').run(id);
|
|
230
|
-
},
|
|
231
|
-
|
|
232
154
|
deleteAllSessions(): number {
|
|
233
155
|
const db = getDatabase();
|
|
234
156
|
const result = db.prepare('DELETE FROM auth_sessions').run() as { changes: number };
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat-app storage — which chat is paired through which bot,
|
|
3
|
+
* what each chat is currently talking to, and what every message in the chat
|
|
4
|
+
* app means in Clopen. See migration 084 for why each table exists.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { randomBytes } from 'crypto';
|
|
8
|
+
import { getDatabase } from '../index';
|
|
9
|
+
|
|
10
|
+
export interface ChannelLinkRow {
|
|
11
|
+
id: string;
|
|
12
|
+
account_id: string;
|
|
13
|
+
user_id: string;
|
|
14
|
+
conversation_id: string;
|
|
15
|
+
platform_user_id: string;
|
|
16
|
+
display_name: string | null;
|
|
17
|
+
/** 0 turns the chat off entirely: it can neither be told nor tell Clopen anything. */
|
|
18
|
+
is_enabled: number;
|
|
19
|
+
active_project_id: string | null;
|
|
20
|
+
active_session_id: string | null;
|
|
21
|
+
created_at: string;
|
|
22
|
+
last_used_at: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a message in the chat app stands for.
|
|
27
|
+
*
|
|
28
|
+
* question an AskUserQuestion still (or no longer) waiting on a tap
|
|
29
|
+
* finished a "run finished / failed" notification for a turn
|
|
30
|
+
* answer the assistant's reply to a turn, sent to the chat
|
|
31
|
+
* user the user's own prompt, as they typed it in the chat app
|
|
32
|
+
* menu a list of projects / sessions / models offered as buttons
|
|
33
|
+
* confirm "this will restore files — go ahead?"
|
|
34
|
+
*/
|
|
35
|
+
export type ChannelMessageKind = 'question' | 'answer' | 'user' | 'menu' | 'confirm';
|
|
36
|
+
export type ChannelMessageState = 'open' | 'answered' | 'closed';
|
|
37
|
+
|
|
38
|
+
export interface ChannelMessageRow {
|
|
39
|
+
id: string;
|
|
40
|
+
link_id: string;
|
|
41
|
+
kind: ChannelMessageKind;
|
|
42
|
+
project_id: string | null;
|
|
43
|
+
chat_session_id: string | null;
|
|
44
|
+
tool_use_id: string | null;
|
|
45
|
+
question_index: number | null;
|
|
46
|
+
/** The Clopen message this row stands for: a user message, or the end of a turn. */
|
|
47
|
+
clopen_message_id: string | null;
|
|
48
|
+
platform_message_id: string;
|
|
49
|
+
state: ChannelMessageState;
|
|
50
|
+
payload: string;
|
|
51
|
+
created_at: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ChannelMessageInput {
|
|
55
|
+
/** Pre-minted when the id has to ride inside the message's own buttons. */
|
|
56
|
+
id?: string;
|
|
57
|
+
linkId: string;
|
|
58
|
+
kind: ChannelMessageKind;
|
|
59
|
+
projectId?: string | null;
|
|
60
|
+
chatSessionId?: string | null;
|
|
61
|
+
toolUseId?: string | null;
|
|
62
|
+
questionIndex?: number | null;
|
|
63
|
+
clopenMessageId?: string | null;
|
|
64
|
+
platformMessageId: string;
|
|
65
|
+
state?: ChannelMessageState;
|
|
66
|
+
payload?: unknown;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Row ids travel inside a button. Ten base62 characters is ~59 bits — far
|
|
71
|
+
* past guessable, and short enough to leave room in a 64-byte button for the
|
|
72
|
+
* action that rides along with it.
|
|
73
|
+
*/
|
|
74
|
+
const ID_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
75
|
+
|
|
76
|
+
export function newChannelMessageId(): string {
|
|
77
|
+
const bytes = randomBytes(10);
|
|
78
|
+
let id = '';
|
|
79
|
+
for (const byte of bytes) id += ID_ALPHABET[byte % ID_ALPHABET.length];
|
|
80
|
+
return id;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const channelLinkQueries = {
|
|
84
|
+
/** Switched on: the only state in which a chat can do anything. */
|
|
85
|
+
isActive(row: ChannelLinkRow): boolean {
|
|
86
|
+
return row.is_enabled === 1;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
getById(id: string): ChannelLinkRow | null {
|
|
90
|
+
return (getDatabase().prepare('SELECT * FROM channel_links WHERE id = ?').get(id) as ChannelLinkRow | null) ?? null;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
getByConversation(accountId: string, conversationId: string): ChannelLinkRow | null {
|
|
94
|
+
return (getDatabase()
|
|
95
|
+
.prepare('SELECT * FROM channel_links WHERE account_id = ? AND conversation_id = ?')
|
|
96
|
+
.get(accountId, conversationId) as ChannelLinkRow | null) ?? null;
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
listByAccount(accountId: string): ChannelLinkRow[] {
|
|
100
|
+
return getDatabase()
|
|
101
|
+
.prepare('SELECT * FROM channel_links WHERE account_id = ? ORDER BY created_at')
|
|
102
|
+
.all(accountId) as ChannelLinkRow[];
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Pair a chat. Re-pairing the same chat moves it rather than failing:
|
|
107
|
+
* whoever just proved they hold the chat AND a fresh code from a Clopen
|
|
108
|
+
* session holds it now, and nothing the previous holder was talking to
|
|
109
|
+
* carries over.
|
|
110
|
+
*/
|
|
111
|
+
upsert(input: {
|
|
112
|
+
accountId: string;
|
|
113
|
+
userId: string;
|
|
114
|
+
conversationId: string;
|
|
115
|
+
platformUserId: string;
|
|
116
|
+
displayName: string | null;
|
|
117
|
+
}): ChannelLinkRow {
|
|
118
|
+
const db = getDatabase();
|
|
119
|
+
const now = new Date().toISOString();
|
|
120
|
+
const existing = this.getByConversation(input.accountId, input.conversationId);
|
|
121
|
+
if (existing) {
|
|
122
|
+
const changedHands = existing.platform_user_id !== input.platformUserId;
|
|
123
|
+
db.prepare(
|
|
124
|
+
`UPDATE channel_links
|
|
125
|
+
SET user_id = ?, platform_user_id = ?, display_name = ?, is_enabled = 1, last_used_at = ?
|
|
126
|
+
WHERE id = ?`
|
|
127
|
+
).run(input.userId, input.platformUserId, input.displayName, now, existing.id);
|
|
128
|
+
if (changedHands) {
|
|
129
|
+
// A chat that changed hands must not keep answering — or continuing — the old holder's sessions.
|
|
130
|
+
db.prepare("UPDATE channel_messages SET state = 'closed' WHERE link_id = ? AND state = 'open'").run(existing.id);
|
|
131
|
+
db.prepare('UPDATE channel_links SET active_project_id = NULL, active_session_id = NULL WHERE id = ?').run(existing.id);
|
|
132
|
+
}
|
|
133
|
+
return this.getById(existing.id)!;
|
|
134
|
+
}
|
|
135
|
+
const id = crypto.randomUUID();
|
|
136
|
+
db.prepare(
|
|
137
|
+
`INSERT INTO channel_links
|
|
138
|
+
(id, account_id, user_id, conversation_id, platform_user_id, display_name, created_at, last_used_at)
|
|
139
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
|
140
|
+
).run(id, input.accountId, input.userId, input.conversationId, input.platformUserId, input.displayName, now, now);
|
|
141
|
+
return this.getById(id)!;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
setEnabled(id: string, enabled: boolean): void {
|
|
145
|
+
getDatabase().prepare('UPDATE channel_links SET is_enabled = ? WHERE id = ?').run(enabled ? 1 : 0, id);
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
/** What a plain message from this chat continues. A null session means "start one in this project". */
|
|
149
|
+
setActive(id: string, projectId: string | null, sessionId: string | null): void {
|
|
150
|
+
getDatabase()
|
|
151
|
+
.prepare('UPDATE channel_links SET active_project_id = ?, active_session_id = ? WHERE id = ?')
|
|
152
|
+
.run(projectId, sessionId, id);
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
/** Forget a session that no longer exists, everywhere it was the active one. */
|
|
156
|
+
clearActiveSession(sessionId: string): void {
|
|
157
|
+
getDatabase().prepare('UPDATE channel_links SET active_session_id = NULL WHERE active_session_id = ?').run(sessionId);
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
touch(id: string): void {
|
|
161
|
+
getDatabase().prepare('UPDATE channel_links SET last_used_at = ? WHERE id = ?').run(new Date().toISOString(), id);
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
remove(id: string): void {
|
|
165
|
+
getDatabase().prepare('DELETE FROM channel_links WHERE id = ?').run(id);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const channelMessageQueries = {
|
|
170
|
+
create(input: ChannelMessageInput): ChannelMessageRow {
|
|
171
|
+
const id = input.id ?? newChannelMessageId();
|
|
172
|
+
getDatabase()
|
|
173
|
+
.prepare(
|
|
174
|
+
`INSERT INTO channel_messages
|
|
175
|
+
(id, link_id, kind, project_id, chat_session_id, tool_use_id, question_index, clopen_message_id, platform_message_id, state, payload, created_at)
|
|
176
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
177
|
+
)
|
|
178
|
+
.run(
|
|
179
|
+
id,
|
|
180
|
+
input.linkId,
|
|
181
|
+
input.kind,
|
|
182
|
+
input.projectId ?? null,
|
|
183
|
+
input.chatSessionId ?? null,
|
|
184
|
+
input.toolUseId ?? null,
|
|
185
|
+
input.questionIndex ?? null,
|
|
186
|
+
input.clopenMessageId ?? null,
|
|
187
|
+
input.platformMessageId,
|
|
188
|
+
input.state ?? 'open',
|
|
189
|
+
JSON.stringify(input.payload ?? {}),
|
|
190
|
+
new Date().toISOString()
|
|
191
|
+
);
|
|
192
|
+
return this.getById(id)!;
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
getById(id: string): ChannelMessageRow | null {
|
|
196
|
+
return (getDatabase().prepare('SELECT * FROM channel_messages WHERE id = ?').get(id) as ChannelMessageRow | null) ?? null;
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
/** The row a swipe-reply or an edit points at. Newest first (rowid breaks a same-millisecond tie). */
|
|
200
|
+
getByMessage(linkId: string, platformMessageId: string): ChannelMessageRow | null {
|
|
201
|
+
return (getDatabase()
|
|
202
|
+
.prepare('SELECT * FROM channel_messages WHERE link_id = ? AND platform_message_id = ? ORDER BY created_at DESC, rowid DESC LIMIT 1')
|
|
203
|
+
.get(linkId, platformMessageId) as ChannelMessageRow | null) ?? null;
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
openQuestionsForLink(linkId: string): ChannelMessageRow[] {
|
|
207
|
+
return getDatabase()
|
|
208
|
+
.prepare("SELECT * FROM channel_messages WHERE link_id = ? AND kind = 'question' AND state = 'open' ORDER BY created_at")
|
|
209
|
+
.all(linkId) as ChannelMessageRow[];
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
forToolUse(toolUseId: string): ChannelMessageRow[] {
|
|
213
|
+
return getDatabase()
|
|
214
|
+
.prepare("SELECT * FROM channel_messages WHERE tool_use_id = ? AND kind = 'question' ORDER BY question_index")
|
|
215
|
+
.all(toolUseId) as ChannelMessageRow[];
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
/** Open confirmations of a chat, oldest first — a newer one may replace them. */
|
|
219
|
+
openConfirmsForLink(linkId: string): ChannelMessageRow[] {
|
|
220
|
+
return getDatabase()
|
|
221
|
+
.prepare("SELECT * FROM channel_messages WHERE link_id = ? AND kind = 'confirm' AND state = 'open' ORDER BY created_at")
|
|
222
|
+
.all(linkId) as ChannelMessageRow[];
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
openQuestionsForSession(chatSessionId: string): ChannelMessageRow[] {
|
|
226
|
+
return getDatabase()
|
|
227
|
+
.prepare("SELECT * FROM channel_messages WHERE chat_session_id = ? AND kind = 'question' AND state = 'open'")
|
|
228
|
+
.all(chatSessionId) as ChannelMessageRow[];
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/** The user's own prompt row for a Clopen user message, if it came from this chat. */
|
|
232
|
+
getUserMessage(linkId: string, clopenMessageId: string): ChannelMessageRow | null {
|
|
233
|
+
return (getDatabase()
|
|
234
|
+
.prepare("SELECT * FROM channel_messages WHERE link_id = ? AND kind = 'user' AND clopen_message_id = ? LIMIT 1")
|
|
235
|
+
.get(linkId, clopenMessageId) as ChannelMessageRow | null) ?? null;
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
setPayload(id: string, payload: unknown): void {
|
|
239
|
+
getDatabase().prepare('UPDATE channel_messages SET payload = ? WHERE id = ?').run(JSON.stringify(payload), id);
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
/** A row written before its message was sent (its id rides on the buttons) learns the message id here. */
|
|
243
|
+
setPlatformMessage(id: string, platformMessageId: string): void {
|
|
244
|
+
getDatabase().prepare('UPDATE channel_messages SET platform_message_id = ? WHERE id = ?').run(platformMessageId, id);
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
setClopenMessage(id: string, clopenMessageId: string): void {
|
|
248
|
+
getDatabase().prepare('UPDATE channel_messages SET clopen_message_id = ? WHERE id = ?').run(clopenMessageId, id);
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Move a row out of `open`. Conditional on it still being open, so two
|
|
253
|
+
* paths racing to settle one question (a tap and an answer in the web UI)
|
|
254
|
+
* cannot both act on it — the loser sees `false` and stands down.
|
|
255
|
+
*/
|
|
256
|
+
settle(id: string, state: Exclude<ChannelMessageState, 'open'>): boolean {
|
|
257
|
+
const result = getDatabase()
|
|
258
|
+
.prepare("UPDATE channel_messages SET state = ? WHERE id = ? AND state = 'open'")
|
|
259
|
+
.run(state, id) as { changes: number };
|
|
260
|
+
return result.changes > 0;
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
deleteForSession(chatSessionId: string): void {
|
|
264
|
+
getDatabase().prepare('DELETE FROM channel_messages WHERE chat_session_id = ?').run(chatSessionId);
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
pruneBefore(isoTimestamp: string): void {
|
|
268
|
+
getDatabase().prepare('DELETE FROM channel_messages WHERE created_at < ?').run(isoTimestamp);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
@@ -90,14 +90,6 @@ function redactConnectionSecrets(connection: DbClientConnection): DbClientConnec
|
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
function hasConnectionAccess(
|
|
94
|
-
row: DBDbClientConnectionRow,
|
|
95
|
-
userId: string,
|
|
96
|
-
isAdmin: boolean
|
|
97
|
-
): boolean {
|
|
98
|
-
if (isAdmin) return true;
|
|
99
|
-
return row.owner_user_id === userId;
|
|
100
|
-
}
|
|
101
93
|
|
|
102
94
|
function preserveExistingSecret(
|
|
103
95
|
patchValue: string | undefined,
|
|
@@ -120,10 +112,9 @@ interface InsertParams {
|
|
|
120
112
|
ssh: DbClientSshConfig;
|
|
121
113
|
options: Record<string, unknown>;
|
|
122
114
|
color: string | null;
|
|
123
|
-
ownerUserId: string | null;
|
|
124
115
|
}
|
|
125
116
|
|
|
126
|
-
function normalizeInput(input: DbClientConnectionInput
|
|
117
|
+
function normalizeInput(input: DbClientConnectionInput): InsertParams {
|
|
127
118
|
const ssh: DbClientSshConfig = {
|
|
128
119
|
...DEFAULT_SSH,
|
|
129
120
|
...(input.ssh ?? {})
|
|
@@ -140,8 +131,7 @@ function normalizeInput(input: DbClientConnectionInput, ownerUserId: string | nu
|
|
|
140
131
|
sslCa: input.sslCa ?? null,
|
|
141
132
|
ssh,
|
|
142
133
|
options: input.options ?? {},
|
|
143
|
-
color: input.color ?? null
|
|
144
|
-
ownerUserId
|
|
134
|
+
color: input.color ?? null
|
|
145
135
|
};
|
|
146
136
|
}
|
|
147
137
|
|
|
@@ -152,11 +142,11 @@ function getRowById(id: string): DBDbClientConnectionRow | null {
|
|
|
152
142
|
`).get(id) as DBDbClientConnectionRow | null);
|
|
153
143
|
}
|
|
154
144
|
|
|
155
|
-
function insertConnection(input: DbClientConnectionInput
|
|
145
|
+
function insertConnection(input: DbClientConnectionInput): DbClientConnection {
|
|
156
146
|
const db = getDatabase();
|
|
157
147
|
const id = crypto.randomUUID();
|
|
158
148
|
const now = new Date().toISOString();
|
|
159
|
-
const params = normalizeInput(input
|
|
149
|
+
const params = normalizeInput(input);
|
|
160
150
|
|
|
161
151
|
db.prepare(`
|
|
162
152
|
INSERT INTO db_client_connections (
|
|
@@ -166,7 +156,6 @@ function insertConnection(input: DbClientConnectionInput, ownerUserId: string |
|
|
|
166
156
|
ssh_enabled, ssh_host, ssh_port, ssh_username, ssh_auth_method,
|
|
167
157
|
ssh_password, ssh_private_key, ssh_passphrase, ssh_connection_id,
|
|
168
158
|
options_json, color,
|
|
169
|
-
owner_user_id,
|
|
170
159
|
created_at, updated_at, last_used_at
|
|
171
160
|
) VALUES (
|
|
172
161
|
?, ?, ?,
|
|
@@ -175,7 +164,6 @@ function insertConnection(input: DbClientConnectionInput, ownerUserId: string |
|
|
|
175
164
|
?, ?, ?, ?, ?,
|
|
176
165
|
?, ?, ?, ?,
|
|
177
166
|
?, ?,
|
|
178
|
-
?,
|
|
179
167
|
?, ?, ?
|
|
180
168
|
)
|
|
181
169
|
`).run(
|
|
@@ -193,7 +181,6 @@ function insertConnection(input: DbClientConnectionInput, ownerUserId: string |
|
|
|
193
181
|
params.ssh.connectionId || null,
|
|
194
182
|
JSON.stringify(params.options),
|
|
195
183
|
params.color,
|
|
196
|
-
params.ownerUserId,
|
|
197
184
|
now, now, null
|
|
198
185
|
);
|
|
199
186
|
|
|
@@ -205,33 +192,20 @@ function insertConnection(input: DbClientConnectionInput, ownerUserId: string |
|
|
|
205
192
|
}
|
|
206
193
|
|
|
207
194
|
export const dbClientConnectionQueries = {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
? db.prepare(`
|
|
212
|
-
SELECT * FROM db_client_connections
|
|
213
|
-
ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
|
|
214
|
-
`).all()
|
|
215
|
-
: db.prepare(`
|
|
216
|
-
SELECT * FROM db_client_connections
|
|
217
|
-
WHERE owner_user_id = ?
|
|
218
|
-
ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
|
|
219
|
-
`).all(userId)) as DBDbClientConnectionRow[];
|
|
220
|
-
|
|
221
|
-
return rows.map((row) => redactConnectionSecrets(rowToConnection(openConnectionRow(row))));
|
|
195
|
+
/** Every connection with its secrets removed — what a client is sent. */
|
|
196
|
+
listRedacted(): DbClientConnection[] {
|
|
197
|
+
return this.list().map(redactConnectionSecrets);
|
|
222
198
|
},
|
|
223
199
|
|
|
224
|
-
|
|
200
|
+
getRedacted(id: string): DbClientConnection | null {
|
|
225
201
|
const row = getRowById(id);
|
|
226
|
-
|
|
227
|
-
return null;
|
|
228
|
-
}
|
|
229
|
-
return redactConnectionSecrets(rowToConnection(row));
|
|
202
|
+
return row ? redactConnectionSecrets(rowToConnection(row)) : null;
|
|
230
203
|
},
|
|
231
204
|
|
|
232
|
-
|
|
205
|
+
/** The connection, or an error when it does not exist. */
|
|
206
|
+
require(id: string): DbClientConnection {
|
|
233
207
|
const row = getRowById(id);
|
|
234
|
-
if (!row
|
|
208
|
+
if (!row) {
|
|
235
209
|
throw new Error('db-client connection not found');
|
|
236
210
|
}
|
|
237
211
|
return rowToConnection(row);
|
|
@@ -252,12 +226,11 @@ export const dbClientConnectionQueries = {
|
|
|
252
226
|
},
|
|
253
227
|
|
|
254
228
|
create(input: DbClientConnectionInput): DbClientConnection {
|
|
255
|
-
return insertConnection(input
|
|
229
|
+
return insertConnection(input);
|
|
256
230
|
},
|
|
257
231
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return redactConnectionSecrets(connection);
|
|
232
|
+
createRedacted(input: DbClientConnectionInput): DbClientConnection {
|
|
233
|
+
return redactConnectionSecrets(insertConnection(input));
|
|
261
234
|
},
|
|
262
235
|
|
|
263
236
|
update(id: string, patch: Partial<DbClientConnectionInput>): DbClientConnection {
|
|
@@ -330,15 +303,9 @@ export const dbClientConnectionQueries = {
|
|
|
330
303
|
return rowToConnection(row);
|
|
331
304
|
},
|
|
332
305
|
|
|
333
|
-
|
|
334
|
-
id
|
|
335
|
-
patch
|
|
336
|
-
userId: string,
|
|
337
|
-
isAdmin: boolean
|
|
338
|
-
): DbClientConnection {
|
|
339
|
-
this.ensureAccess(id, userId, isAdmin);
|
|
340
|
-
const updated = this.update(id, patch);
|
|
341
|
-
return redactConnectionSecrets(updated);
|
|
306
|
+
updateRedacted(id: string, patch: Partial<DbClientConnectionInput>): DbClientConnection {
|
|
307
|
+
this.require(id);
|
|
308
|
+
return redactConnectionSecrets(this.update(id, patch));
|
|
342
309
|
},
|
|
343
310
|
|
|
344
311
|
/**
|
|
@@ -365,11 +332,6 @@ export const dbClientConnectionQueries = {
|
|
|
365
332
|
db.prepare('DELETE FROM db_client_connections WHERE id = ?').run(id);
|
|
366
333
|
},
|
|
367
334
|
|
|
368
|
-
deleteForUser(id: string, userId: string, isAdmin: boolean): void {
|
|
369
|
-
this.ensureAccess(id, userId, isAdmin);
|
|
370
|
-
this.delete(id);
|
|
371
|
-
},
|
|
372
|
-
|
|
373
335
|
markUsed(id: string): void {
|
|
374
336
|
const db = getDatabase();
|
|
375
337
|
const now = new Date().toISOString();
|
|
@@ -34,11 +34,6 @@ export interface FileShareRow {
|
|
|
34
34
|
created_at: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
/** A row joined with its creator's display name, for the management list. */
|
|
38
|
-
export interface FileShareListRow extends FileShareRow {
|
|
39
|
-
created_by_name: string | null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
37
|
export interface CreateFileShareParams {
|
|
43
38
|
tokenHash: string;
|
|
44
39
|
filePath: string;
|
|
@@ -48,14 +43,8 @@ export interface CreateFileShareParams {
|
|
|
48
43
|
createdAt: string;
|
|
49
44
|
}
|
|
50
45
|
|
|
51
|
-
const LIST_SELECT = `
|
|
52
|
-
SELECT s.*, u.name AS created_by_name
|
|
53
|
-
FROM file_shares s
|
|
54
|
-
LEFT JOIN users u ON u.id = s.created_by
|
|
55
|
-
`;
|
|
56
|
-
|
|
57
46
|
/** Rows that have not passed their deadline — including the ones without one. */
|
|
58
|
-
const LIVE = '(
|
|
47
|
+
const LIVE = '(expires_at IS NULL OR expires_at > ?)';
|
|
59
48
|
|
|
60
49
|
export const fileShareQueries = {
|
|
61
50
|
create(params: CreateFileShareParams): FileShareRow {
|
|
@@ -89,18 +78,11 @@ export const fileShareQueries = {
|
|
|
89
78
|
return db.prepare('SELECT * FROM file_shares WHERE id = ?').get(id) as FileShareRow | null;
|
|
90
79
|
},
|
|
91
80
|
|
|
92
|
-
/** Every live link, newest first —
|
|
93
|
-
|
|
94
|
-
const db = getDatabase();
|
|
95
|
-
return db.prepare(`${LIST_SELECT} WHERE ${LIVE} ORDER BY s.created_at DESC`)
|
|
96
|
-
.all(now) as FileShareListRow[];
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
/** One user's live links, newest first. */
|
|
100
|
-
listForUser(userId: string, now: string): FileShareListRow[] {
|
|
81
|
+
/** Every live link, newest first — what is currently open. */
|
|
82
|
+
listLive(now: string): FileShareRow[] {
|
|
101
83
|
const db = getDatabase();
|
|
102
|
-
return db.prepare(
|
|
103
|
-
.all(
|
|
84
|
+
return db.prepare(`SELECT * FROM file_shares WHERE ${LIVE} ORDER BY created_at DESC`)
|
|
85
|
+
.all(now) as FileShareRow[];
|
|
104
86
|
},
|
|
105
87
|
|
|
106
88
|
/**
|