@pairdraw/cloud 0.18.0 → 0.19.0-beta.1
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/dist/client.d.ts +5 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +0 -7
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +8 -5
- package/dist/contract2.js.map +1 -1
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +202 -110
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +2299 -661
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3298 -964
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +415 -190
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +243 -79
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/api.ts +0 -15
- package/src/contract/documents.ts +8 -0
- package/src/contract/index.ts +1 -3
- package/src/contract/sharing.ts +0 -9
- package/src/runtime/cloudflare/relay.ts +5 -1
- package/src/runtime/cloudflare/worker.ts +4 -1
- package/src/runtime/node/bootstrap.ts +13 -3
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/server.ts +12 -2
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/site/server.ts +19 -2
- package/src/server/api/router.ts +16 -8
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +9 -0
- package/src/server/auth/factory.ts +3 -0
- package/src/server/collaboration/relay/hub.ts +42 -3
- package/src/server/collaboration/routes.ts +21 -14
- package/src/server/config/schema.ts +3 -0
- package/src/server/db/expressions.ts +13 -1
- package/src/server/db/migrate.ts +3 -1
- package/src/server/db/migrations/028_invitation_sends.ts +31 -0
- package/src/server/db/tables.ts +9 -0
- package/src/server/documents/access.ts +4 -2
- package/src/server/documents/routes.ts +81 -57
- package/src/server/documents/versions/routes.ts +27 -16
- package/src/server/index.ts +2 -0
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/profiles/routes.ts +16 -12
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +98 -11
- package/src/server/sharing/capabilities/service.ts +27 -2
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +13 -7
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +2 -0
- package/src/server/sharing/routes.ts +79 -64
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +76 -0
- package/src/server/workspaces/members.ts +2 -0
- package/src/server/workspaces/routes.ts +56 -35
package/src/server/db/tables.ts
CHANGED
|
@@ -349,6 +349,14 @@ export interface AuthSchemaVersionTable {
|
|
|
349
349
|
updatedAt: TimestampColumn
|
|
350
350
|
}
|
|
351
351
|
|
|
352
|
+
/** One invitation sent, for the daily invitation budget; see `src/server/invitations/budget.ts`. */
|
|
353
|
+
export interface InvitationSendTable {
|
|
354
|
+
id: string
|
|
355
|
+
senderId: string
|
|
356
|
+
emailNormalized: string
|
|
357
|
+
sentAt: TimestampColumn
|
|
358
|
+
}
|
|
359
|
+
|
|
352
360
|
export interface CloudDatabase {
|
|
353
361
|
account: Account
|
|
354
362
|
session: Session
|
|
@@ -382,6 +390,7 @@ export interface CloudDatabase {
|
|
|
382
390
|
transactionalEmail: TransactionalEmailTable
|
|
383
391
|
uploadStorageReservation: UploadStorageReservationTable
|
|
384
392
|
upload: UploadTable
|
|
393
|
+
invitationSend: InvitationSendTable
|
|
385
394
|
}
|
|
386
395
|
|
|
387
396
|
export type NewTransactionalEmail = Insertable<TransactionalEmailTable>
|
|
@@ -46,7 +46,9 @@ export async function resolveDocumentAccess(
|
|
|
46
46
|
|
|
47
47
|
const sources: DocumentAccessSource[] = []
|
|
48
48
|
const candidates: DocumentPermission[] = []
|
|
49
|
-
|
|
49
|
+
// Making a document gives access only while its maker is in the workspace: someone removed
|
|
50
|
+
// from it keeps no way back in.
|
|
51
|
+
const owner = document.createdBy === userId && document.workspaceRole !== null
|
|
50
52
|
if (owner) {
|
|
51
53
|
sources.push('owner')
|
|
52
54
|
candidates.push('edit')
|
|
@@ -96,7 +98,7 @@ export async function canDeleteDocument(
|
|
|
96
98
|
.executeTakeFirst()
|
|
97
99
|
if (!document) return false
|
|
98
100
|
return (
|
|
99
|
-
document.createdBy === userId ||
|
|
101
|
+
(document.createdBy === userId && document.workspaceRole !== null) ||
|
|
100
102
|
document.workspaceRole === 'admin' ||
|
|
101
103
|
document.workspaceRole === 'editor'
|
|
102
104
|
)
|
|
@@ -16,8 +16,13 @@ import {
|
|
|
16
16
|
} from '#cloud/server/documents/errors'
|
|
17
17
|
import type { DocumentService } from '#cloud/server/documents/service'
|
|
18
18
|
import { StorageQuotaExceededError } from '#cloud/server/quota'
|
|
19
|
-
import { CLOUD_RATE_LIMITS,
|
|
20
|
-
import {
|
|
19
|
+
import { CLOUD_RATE_LIMITS, createResourceRateLimiter } from '#cloud/server/rate-limit'
|
|
20
|
+
import {
|
|
21
|
+
documentParams,
|
|
22
|
+
uploadParams,
|
|
23
|
+
validatedJSON,
|
|
24
|
+
workspaceParams
|
|
25
|
+
} from '#cloud/server/validation'
|
|
21
26
|
import { Hono, type Context } from 'hono'
|
|
22
27
|
import type { Kysely } from 'kysely'
|
|
23
28
|
|
|
@@ -69,32 +74,32 @@ export function createDocumentRoutes(
|
|
|
69
74
|
if (rateLimit) {
|
|
70
75
|
router.use(
|
|
71
76
|
'/workspaces/:workspaceId/documents',
|
|
72
|
-
|
|
77
|
+
createResourceRateLimiter(
|
|
73
78
|
rateLimit.database,
|
|
74
79
|
rateLimit.secret,
|
|
75
80
|
CLOUD_RATE_LIMITS.documentCreation,
|
|
76
|
-
(context) => context.req.
|
|
77
|
-
(context) => context.req.
|
|
81
|
+
(context) => context.req.param('workspaceId') ?? 'unknown',
|
|
82
|
+
(context) => context.req.method !== 'POST'
|
|
78
83
|
)
|
|
79
84
|
)
|
|
80
85
|
router.use(
|
|
81
86
|
'/documents/:documentId/images',
|
|
82
|
-
|
|
87
|
+
createResourceRateLimiter(
|
|
83
88
|
rateLimit.database,
|
|
84
89
|
rateLimit.secret,
|
|
85
90
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
86
|
-
(context) => context.req.
|
|
87
|
-
(context) => context.req.
|
|
91
|
+
(context) => context.req.param('documentId') ?? 'unknown',
|
|
92
|
+
(context) => context.req.method !== 'POST'
|
|
88
93
|
)
|
|
89
94
|
)
|
|
90
95
|
router.use(
|
|
91
96
|
'/documents/:documentId/uploads',
|
|
92
|
-
|
|
97
|
+
createResourceRateLimiter(
|
|
93
98
|
rateLimit.database,
|
|
94
99
|
rateLimit.secret,
|
|
95
100
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
96
|
-
(context) => context.req.
|
|
97
|
-
(context) => context.req.
|
|
101
|
+
(context) => context.req.param('documentId') ?? 'unknown',
|
|
102
|
+
(context) => context.req.method !== 'POST'
|
|
98
103
|
)
|
|
99
104
|
)
|
|
100
105
|
}
|
|
@@ -102,7 +107,7 @@ export function createDocumentRoutes(
|
|
|
102
107
|
.get('/shared-documents', async (context) =>
|
|
103
108
|
context.json({ documents: await service.listShared(context.get('actor').userId) })
|
|
104
109
|
)
|
|
105
|
-
.get('/workspaces/:workspaceId/documents', async (context) => {
|
|
110
|
+
.get('/workspaces/:workspaceId/documents', workspaceParams, async (context) => {
|
|
106
111
|
const documents = await service.list(
|
|
107
112
|
context.get('actor').userId,
|
|
108
113
|
context.req.param('workspaceId')
|
|
@@ -110,7 +115,7 @@ export function createDocumentRoutes(
|
|
|
110
115
|
if (!documents) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
111
116
|
return context.json({ documents })
|
|
112
117
|
})
|
|
113
|
-
.get('/workspaces/:workspaceId/usage', async (context) => {
|
|
118
|
+
.get('/workspaces/:workspaceId/usage', workspaceParams, async (context) => {
|
|
114
119
|
try {
|
|
115
120
|
return context.json({
|
|
116
121
|
usage: await service.usage(context.get('actor').userId, context.req.param('workspaceId'))
|
|
@@ -123,6 +128,7 @@ export function createDocumentRoutes(
|
|
|
123
128
|
})
|
|
124
129
|
.post(
|
|
125
130
|
'/workspaces/:workspaceId/documents',
|
|
131
|
+
workspaceParams,
|
|
126
132
|
validatedJSON(parseCreateDocument),
|
|
127
133
|
async (context) => {
|
|
128
134
|
try {
|
|
@@ -139,7 +145,7 @@ export function createDocumentRoutes(
|
|
|
139
145
|
}
|
|
140
146
|
}
|
|
141
147
|
)
|
|
142
|
-
.delete('/documents/:documentId', async (context) => {
|
|
148
|
+
.delete('/documents/:documentId', documentParams, async (context) => {
|
|
143
149
|
try {
|
|
144
150
|
await service.remove(context.get('actor').userId, context.req.param('documentId'))
|
|
145
151
|
return context.body(null, 204)
|
|
@@ -149,7 +155,7 @@ export function createDocumentRoutes(
|
|
|
149
155
|
throw error
|
|
150
156
|
}
|
|
151
157
|
})
|
|
152
|
-
.get('/documents/:documentId/access', async (context) => {
|
|
158
|
+
.get('/documents/:documentId/access', documentParams, async (context) => {
|
|
153
159
|
try {
|
|
154
160
|
return context.json({
|
|
155
161
|
access: await service.access(context.get('actor').userId, context.req.param('documentId'))
|
|
@@ -160,7 +166,7 @@ export function createDocumentRoutes(
|
|
|
160
166
|
throw error
|
|
161
167
|
}
|
|
162
168
|
})
|
|
163
|
-
.get('/documents/:documentId', async (context) => {
|
|
169
|
+
.get('/documents/:documentId', documentParams, async (context) => {
|
|
164
170
|
try {
|
|
165
171
|
return context.json({
|
|
166
172
|
document: await service.download(
|
|
@@ -174,20 +180,25 @@ export function createDocumentRoutes(
|
|
|
174
180
|
throw error
|
|
175
181
|
}
|
|
176
182
|
})
|
|
177
|
-
.post(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
183
|
+
.post(
|
|
184
|
+
'/documents/:documentId/uploads',
|
|
185
|
+
documentParams,
|
|
186
|
+
validatedJSON(parseCreateUpload),
|
|
187
|
+
(context) =>
|
|
188
|
+
documentResponse(context, async () =>
|
|
189
|
+
context.json(
|
|
190
|
+
await service.createUpload(
|
|
191
|
+
context.get('actor').userId,
|
|
192
|
+
context.req.param('documentId'),
|
|
193
|
+
context.req.valid('json')
|
|
194
|
+
),
|
|
195
|
+
201
|
|
196
|
+
)
|
|
186
197
|
)
|
|
187
|
-
)
|
|
188
198
|
)
|
|
189
199
|
.post(
|
|
190
200
|
'/documents/:documentId/images/commit',
|
|
201
|
+
documentParams,
|
|
191
202
|
validatedJSON(parseCommitImageUploads),
|
|
192
203
|
(context) =>
|
|
193
204
|
documentResponse(context, async () => {
|
|
@@ -199,41 +210,54 @@ export function createDocumentRoutes(
|
|
|
199
210
|
return context.body(null, 204)
|
|
200
211
|
})
|
|
201
212
|
)
|
|
202
|
-
.post(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
.post(
|
|
214
|
+
'/documents/:documentId/images/links',
|
|
215
|
+
documentParams,
|
|
216
|
+
validatedJSON(parseRequestImageLinks),
|
|
217
|
+
(context) =>
|
|
218
|
+
documentResponse(context, async () =>
|
|
219
|
+
context.json(
|
|
220
|
+
await service.imageLinks(
|
|
221
|
+
context.get('actor').userId,
|
|
222
|
+
context.req.param('documentId'),
|
|
223
|
+
context.req.valid('json').checksums
|
|
224
|
+
)
|
|
209
225
|
)
|
|
210
226
|
)
|
|
211
|
-
)
|
|
212
227
|
)
|
|
213
|
-
.post(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
228
|
+
.post(
|
|
229
|
+
'/documents/:documentId/images',
|
|
230
|
+
documentParams,
|
|
231
|
+
validatedJSON(parseRequestImageUploads),
|
|
232
|
+
(context) =>
|
|
233
|
+
documentResponse(context, async () =>
|
|
234
|
+
context.json(
|
|
235
|
+
await service.requestImageUploads(
|
|
236
|
+
context.get('actor').userId,
|
|
237
|
+
context.req.param('documentId'),
|
|
238
|
+
context.req.valid('json')
|
|
239
|
+
),
|
|
240
|
+
201
|
|
241
|
+
)
|
|
222
242
|
)
|
|
223
|
-
)
|
|
224
243
|
)
|
|
225
|
-
.post(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
244
|
+
.post(
|
|
245
|
+
'/uploads/:uploadId/commit',
|
|
246
|
+
uploadParams,
|
|
247
|
+
validatedJSON(parseCommitUpload),
|
|
248
|
+
async (context) => {
|
|
249
|
+
try {
|
|
250
|
+
const document = await service.commitUpload(
|
|
251
|
+
context.get('actor').userId,
|
|
252
|
+
context.req.param('uploadId'),
|
|
253
|
+
context.req.valid('json')
|
|
254
|
+
)
|
|
255
|
+
return context.json({ document })
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const response = documentDomainError(context, error)
|
|
258
|
+
if (response) return response
|
|
259
|
+
throw error
|
|
260
|
+
}
|
|
237
261
|
}
|
|
238
|
-
|
|
262
|
+
)
|
|
239
263
|
}
|
|
@@ -4,7 +4,12 @@ import {
|
|
|
4
4
|
parseUpdateDocumentVersion
|
|
5
5
|
} from '#cloud/contract'
|
|
6
6
|
import { documentDomainError, type DocumentRouteEnvironment } from '#cloud/server/documents/routes'
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
documentParams,
|
|
9
|
+
documentVersionParams,
|
|
10
|
+
validatedJSON,
|
|
11
|
+
validatedQuery
|
|
12
|
+
} from '#cloud/server/validation'
|
|
8
13
|
import { Hono, type Context } from 'hono'
|
|
9
14
|
import * as v from 'valibot'
|
|
10
15
|
|
|
@@ -29,7 +34,7 @@ async function answer(context: Context, run: () => Promise<Response>): Promise<R
|
|
|
29
34
|
/** A document's version history: listing, naming, opening, restoring, and duplicating versions. */
|
|
30
35
|
export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
31
36
|
return new Hono<DocumentRouteEnvironment>()
|
|
32
|
-
.get('/documents/:documentId/versions', validatedQuery(parsePage), (context) =>
|
|
37
|
+
.get('/documents/:documentId/versions', documentParams, validatedQuery(parsePage), (context) =>
|
|
33
38
|
answer(context, async () =>
|
|
34
39
|
context.json(
|
|
35
40
|
await service.list(
|
|
@@ -40,22 +45,27 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
40
45
|
)
|
|
41
46
|
)
|
|
42
47
|
)
|
|
43
|
-
.post(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
.post(
|
|
49
|
+
'/documents/:documentId/versions',
|
|
50
|
+
documentParams,
|
|
51
|
+
validatedJSON(parseSaveDocumentVersion),
|
|
52
|
+
(context) =>
|
|
53
|
+
answer(context, async () =>
|
|
54
|
+
context.json(
|
|
55
|
+
{
|
|
56
|
+
version: await service.save(
|
|
57
|
+
context.get('actor').userId,
|
|
58
|
+
context.req.param('documentId'),
|
|
59
|
+
context.req.valid('json')
|
|
60
|
+
)
|
|
61
|
+
},
|
|
62
|
+
201
|
|
63
|
+
)
|
|
54
64
|
)
|
|
55
|
-
)
|
|
56
65
|
)
|
|
57
66
|
.patch(
|
|
58
67
|
'/documents/:documentId/versions/:versionId',
|
|
68
|
+
documentVersionParams,
|
|
59
69
|
validatedJSON(parseUpdateDocumentVersion),
|
|
60
70
|
(context) =>
|
|
61
71
|
answer(context, async () =>
|
|
@@ -69,7 +79,7 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
69
79
|
})
|
|
70
80
|
)
|
|
71
81
|
)
|
|
72
|
-
.get('/documents/:documentId/versions/:versionId', (context) =>
|
|
82
|
+
.get('/documents/:documentId/versions/:versionId', documentVersionParams, (context) =>
|
|
73
83
|
answer(context, async () =>
|
|
74
84
|
context.json(
|
|
75
85
|
await service.download(
|
|
@@ -80,7 +90,7 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
80
90
|
)
|
|
81
91
|
)
|
|
82
92
|
)
|
|
83
|
-
.post('/documents/:documentId/versions/:versionId/restore', (context) =>
|
|
93
|
+
.post('/documents/:documentId/versions/:versionId/restore', documentVersionParams, (context) =>
|
|
84
94
|
answer(context, async () =>
|
|
85
95
|
context.json(
|
|
86
96
|
await service.restore(
|
|
@@ -93,6 +103,7 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
93
103
|
)
|
|
94
104
|
.post(
|
|
95
105
|
'/documents/:documentId/versions/:versionId/duplicate',
|
|
106
|
+
documentVersionParams,
|
|
96
107
|
validatedJSON(parseDuplicateDocumentVersion),
|
|
97
108
|
(context) =>
|
|
98
109
|
answer(context, async () =>
|
package/src/server/index.ts
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
3
|
+
import type { Transaction } from 'kysely'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How many invitations, to documents and workspaces alike, one person may send in a day, and how
|
|
7
|
+
* many one address may receive from everyone. Each sends an email naming the inviter and the file,
|
|
8
|
+
* both of which the sender chooses, so without a daily budget invitations are a way to send spam.
|
|
9
|
+
*/
|
|
10
|
+
export const INVITATION_BUDGET = {
|
|
11
|
+
windowMs: 24 * 60 * 60 * 1000,
|
|
12
|
+
perSender: 100,
|
|
13
|
+
perRecipient: 5
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
/** An invitation past the sender's or the recipient's daily budget. */
|
|
17
|
+
export class InvitationLimitError extends Error {
|
|
18
|
+
override readonly name = 'InvitationLimitError'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function sentSince(
|
|
22
|
+
transaction: Transaction<CloudDatabase>,
|
|
23
|
+
column: 'senderId' | 'emailNormalized',
|
|
24
|
+
value: string,
|
|
25
|
+
since: Date
|
|
26
|
+
): Promise<number> {
|
|
27
|
+
const row = await transaction
|
|
28
|
+
.selectFrom('invitationSend')
|
|
29
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
30
|
+
.where(column, '=', value)
|
|
31
|
+
.where('sentAt', '>', since)
|
|
32
|
+
.executeTakeFirstOrThrow()
|
|
33
|
+
return Number(row.count)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Counts an invitation from `senderId` to `email` against both daily budgets, in the transaction
|
|
38
|
+
* that creates it, or refuses it once either budget is used. Sends are logged apart from the
|
|
39
|
+
* invitations, so deleting a workspace or document gives nothing back, and the check holds locks
|
|
40
|
+
* on the sender and the recipient, so two invitations at once cannot both take the last one.
|
|
41
|
+
*/
|
|
42
|
+
export async function spendInvitationBudget(
|
|
43
|
+
transaction: Transaction<CloudDatabase>,
|
|
44
|
+
invitation: { senderId: string; email: string },
|
|
45
|
+
now = new Date()
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
const email = invitation.email.trim().toLowerCase()
|
|
48
|
+
// Always sender first, then recipient, so two transactions never wait on each other in turn.
|
|
49
|
+
await lockForTransaction(transaction, `invitation-sender:${invitation.senderId}`)
|
|
50
|
+
await lockForTransaction(transaction, `invitation-recipient:${email}`)
|
|
51
|
+
const since = new Date(now.getTime() - INVITATION_BUDGET.windowMs)
|
|
52
|
+
const bySender = await sentSince(transaction, 'senderId', invitation.senderId, since)
|
|
53
|
+
const byRecipient = await sentSince(transaction, 'emailNormalized', email, since)
|
|
54
|
+
if (bySender >= INVITATION_BUDGET.perSender || byRecipient >= INVITATION_BUDGET.perRecipient) {
|
|
55
|
+
throw new InvitationLimitError()
|
|
56
|
+
}
|
|
57
|
+
await transaction
|
|
58
|
+
.insertInto('invitationSend')
|
|
59
|
+
.values({
|
|
60
|
+
id: crypto.randomUUID(),
|
|
61
|
+
senderId: invitation.senderId,
|
|
62
|
+
emailNormalized: email,
|
|
63
|
+
sentAt: now
|
|
64
|
+
})
|
|
65
|
+
.execute()
|
|
66
|
+
}
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
import type { CloudActor } from '#cloud/server/auth'
|
|
7
7
|
import type { DocumentService } from '#cloud/server/documents'
|
|
8
8
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
9
|
-
import { validatedJSON } from '#cloud/server/validation'
|
|
9
|
+
import { documentParams, validatedJSON, workspaceParams } from '#cloud/server/validation'
|
|
10
10
|
import { Hono, type Context } from 'hono'
|
|
11
11
|
|
|
12
12
|
import {
|
|
@@ -64,16 +64,20 @@ export function createPublicProfileSettingsRoutes(service?: PublicProfileService
|
|
|
64
64
|
})
|
|
65
65
|
)
|
|
66
66
|
)
|
|
67
|
-
.put(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
.put(
|
|
68
|
+
'/workspaces/:workspaceId/public',
|
|
69
|
+
workspaceParams,
|
|
70
|
+
validatedJSON(parsePublishWorkspace),
|
|
71
|
+
(context) =>
|
|
72
|
+
profileRoute(context, async () =>
|
|
73
|
+
context.json({
|
|
74
|
+
profile: await required(service).publishWorkspace(
|
|
75
|
+
context.get('actor').userId,
|
|
76
|
+
context.req.param('workspaceId'),
|
|
77
|
+
context.req.valid('json').public
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
)
|
|
77
81
|
)
|
|
78
82
|
}
|
|
79
83
|
|
|
@@ -93,7 +97,7 @@ export function createPublicProfileRoutes(
|
|
|
93
97
|
return context.json({ profile })
|
|
94
98
|
})
|
|
95
99
|
)
|
|
96
|
-
.post('/documents/:documentId/document', (context) =>
|
|
100
|
+
.post('/documents/:documentId/document', documentParams, (context) =>
|
|
97
101
|
profileRoute(context, async () => {
|
|
98
102
|
const documentId = context.req.param('documentId')
|
|
99
103
|
await required(service).requirePublicDocument(documentId)
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { INVITATION_BUDGET } from '#cloud/server/invitations'
|
|
2
3
|
import type { Kysely } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export function createRateLimitCleanupService(database: Kysely<CloudDatabase>) {
|
|
5
6
|
return {
|
|
6
7
|
async cleanupExpired(before: Date): Promise<number> {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const [limits, invitations] = await Promise.all([
|
|
9
|
+
database
|
|
10
|
+
.deleteFrom('apiRateLimit')
|
|
11
|
+
.where('windowStartedAt', '<', before)
|
|
12
|
+
.executeTakeFirst(),
|
|
13
|
+
// Never a send the invitation budget still counts, whatever cutoff the sweep passes.
|
|
14
|
+
database
|
|
15
|
+
.deleteFrom('invitationSend')
|
|
16
|
+
.where(
|
|
17
|
+
'sentAt',
|
|
18
|
+
'<',
|
|
19
|
+
new Date(Math.min(before.getTime(), Date.now() - INVITATION_BUDGET.windowMs))
|
|
20
|
+
)
|
|
21
|
+
.executeTakeFirst()
|
|
22
|
+
])
|
|
23
|
+
return Number(limits.numDeletedRows) + Number(invitations.numDeletedRows)
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
@@ -5,8 +5,11 @@ export {
|
|
|
5
5
|
createActorRateLimiter,
|
|
6
6
|
createCloudRateLimiter,
|
|
7
7
|
createRouteRateLimiter,
|
|
8
|
+
createResourceRateLimiter,
|
|
8
9
|
createTrustedIPRateLimiter,
|
|
9
10
|
routeHasOwnBudget,
|
|
10
|
-
|
|
11
|
+
routeParamResource,
|
|
12
|
+
type RateLimitPolicy,
|
|
13
|
+
type ResourceRateLimitPolicy
|
|
11
14
|
} from './middleware'
|
|
12
15
|
export { PostgresRateLimitStore } from './postgres'
|