@beechcms/api 0.4.0-preview.9 → 0.4.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/assets/dashboard/BeechLogo.svg +18 -18
- package/assets/dashboard/BeechLogoLIght.svg +48 -48
- package/assets/dashboard/assets/index-CH13idU1.js +554 -0
- package/assets/dashboard/assets/index-CewtCjom.css +1 -0
- package/assets/dashboard/beechLogoDark.svg +48 -48
- package/assets/dashboard/index.html +18 -18
- package/assets/dashboard/sol.svg +3 -3
- package/assets/dashboard/undraw_enter_nwx3.svg +36 -36
- package/migrations/0000_v040_base.sql +213 -213
- package/package.json +2 -2
- package/src/auth/bcrypt-hash-provider.ts +20 -0
- package/src/auth/constants.ts +10 -10
- package/src/auth/generate-refresh-token.test.ts +19 -0
- package/src/auth/hash-provider.test.ts +46 -0
- package/src/auth/in-memory-hash-provider.ts +13 -0
- package/src/auth/jose-token-service.ts +55 -0
- package/src/auth/login.test.ts +92 -0
- package/src/auth/login.ts +74 -91
- package/src/auth/refresh.ts +5 -127
- package/src/auth/static-token-service.ts +18 -0
- package/src/auth/token-service.test.ts +82 -0
- package/src/factory.ts +339 -303
- package/src/features/content/constants.ts +10 -0
- package/src/features/content/handlers/create.ts +157 -0
- package/src/features/content/handlers/delete.ts +80 -0
- package/src/features/content/handlers/facets.ts +45 -0
- package/src/features/content/handlers/get.ts +116 -0
- package/src/features/content/handlers/list.ts +88 -0
- package/src/features/content/handlers/update.ts +211 -0
- package/src/features/content/index.ts +20 -0
- package/src/features/draft/draft.handler.ts +283 -198
- package/src/features/draft/index.ts +1 -1
- package/src/features/email/email.provider.ts +38 -38
- package/src/features/email/email.service.ts +80 -80
- package/src/features/email/email.types.ts +98 -98
- package/src/features/email/index.ts +28 -28
- package/src/features/email/providers/resend.ts +63 -63
- package/src/features/email/templates/password-changed.ts +59 -59
- package/src/features/email/templates/password-reset.ts +64 -64
- package/src/features/email/templates/shell.ts +92 -93
- package/src/features/notifications/index.ts +1 -1
- package/src/features/notifications/notifications.handler.ts +101 -88
- package/src/features/password-reset/index.ts +15 -15
- package/src/features/password-reset/request.ts +82 -88
- package/src/features/password-reset/reset.ts +92 -110
- package/src/features/rotate-field/index.ts +1 -1
- package/src/features/rotate-field/rotate-field.handler.ts +128 -82
- package/src/features/rotate-field/rotate-field.schema.ts +13 -9
- package/src/features/schema/schema.handler.ts +16 -16
- package/src/features/settings/settings.handler.ts +301 -249
- package/src/features/setup/index.ts +91 -59
- package/src/features/stats/index.ts +1 -1
- package/src/features/stats/stats.handler.ts +430 -395
- package/src/index.ts +24 -11
- package/src/media-utils.ts +78 -78
- package/src/middleware/auth-providers.middleware.ts +32 -0
- package/src/middleware/observability.middleware.ts +52 -0
- package/src/middleware/rate-limit.middleware.ts +41 -0
- package/src/middleware/repository.middleware.ts +60 -0
- package/src/middleware/storage.middleware.ts +21 -0
- package/src/middleware.ts +47 -67
- package/src/public/access-policy.ts +23 -23
- package/src/public/api-key-middleware.ts +53 -53
- package/src/public/index.ts +12 -12
- package/src/public/problem-details.ts +48 -42
- package/src/public/public-add.ts +156 -183
- package/src/public/public-edit.ts +159 -183
- package/src/public/public-errors.ts +15 -15
- package/src/public/public-read.ts +216 -217
- package/src/public/public-routes.ts +84 -31
- package/src/public/query-builder.test.ts +220 -0
- package/src/public/query-builder.ts +152 -241
- package/src/public/rate-limit-middleware.ts +30 -42
- package/src/public/response-builder.ts +26 -26
- package/src/public/sanitize.ts +65 -65
- package/src/public/slug-utils.ts +14 -14
- package/src/rate-limit/cloudflare-rate-limiter.test.ts +26 -0
- package/src/rate-limit/cloudflare-rate-limiter.ts +11 -0
- package/src/rate-limit/in-memory-rate-limiter.test.ts +33 -0
- package/src/rate-limit/in-memory-rate-limiter.ts +13 -0
- package/src/rate-limit/no-op-rate-limiter.test.ts +18 -0
- package/src/rate-limit/no-op-rate-limiter.ts +7 -0
- package/src/search-utils.test.ts +207 -0
- package/src/search-utils.ts +209 -192
- package/src/search.ts +61 -72
- package/src/shared/apply-policies.test.ts +77 -0
- package/src/shared/apply-policies.ts +63 -63
- package/src/shared/background-notification-service.test.ts +58 -0
- package/src/shared/background-notification-service.ts +48 -0
- package/src/shared/base.repository.d1.ts +28 -0
- package/src/shared/content-utils.test.ts +161 -0
- package/src/shared/content-utils.ts +82 -108
- package/src/shared/content.repository.d1.test.ts +312 -0
- package/src/shared/content.repository.d1.ts +382 -0
- package/src/shared/d1-activity-log.repository.test.ts +136 -0
- package/src/shared/d1-activity-log.repository.ts +101 -0
- package/src/shared/d1-activity-logger.test.ts +82 -0
- package/src/shared/d1-activity-logger.ts +63 -0
- package/src/shared/d1-analytics.repository.test.ts +74 -0
- package/src/shared/d1-analytics.repository.ts +81 -0
- package/src/shared/d1-content-scan.repository.ts +29 -0
- package/src/shared/d1-notification.repository.test.ts +124 -0
- package/src/shared/d1-notification.repository.ts +114 -0
- package/src/shared/d1-password-reset-token.repository.test.ts +77 -0
- package/src/shared/d1-password-reset-token.repository.ts +52 -0
- package/src/shared/d1-search.repository.test.ts +83 -0
- package/src/shared/d1-search.repository.ts +84 -0
- package/src/shared/d1-session.repository.test.ts +121 -0
- package/src/shared/d1-session.repository.ts +98 -0
- package/src/shared/d1-user.repository.test.ts +147 -0
- package/src/shared/d1-user.repository.ts +109 -0
- package/src/shared/d1-widget.repository.test.ts +217 -0
- package/src/shared/d1-widget.repository.ts +337 -0
- package/src/shared/fixed-clock.ts +21 -0
- package/src/shared/fts-sync.ts +4 -4
- package/src/shared/idempotency.repository.d1.test.ts +79 -0
- package/src/shared/idempotency.repository.d1.ts +56 -0
- package/src/shared/in-memory-activity-logger.ts +15 -0
- package/src/shared/in-memory-notification-service.ts +15 -0
- package/src/shared/media.repository.d1.test.ts +103 -0
- package/src/shared/media.repository.d1.ts +64 -0
- package/src/shared/query-utils.ts +137 -137
- package/src/shared/request-utils.ts +22 -0
- package/src/shared/sequential-id-generator.ts +22 -0
- package/src/shared/storage/factory.ts +40 -0
- package/src/shared/storage/r2-binding-bucket.ts +81 -0
- package/src/shared/storage/s3-bucket.ts +163 -0
- package/src/shared/storage-utils.ts +36 -36
- package/src/shared/system-stats.repository.d1.test.ts +54 -0
- package/src/shared/system-stats.repository.d1.ts +44 -0
- package/src/types.ts +63 -36
- package/src/upload.ts +186 -335
- package/src/widget.ts +208 -349
- package/assets/dashboard/assets/index-CC-jbp6g.js +0 -554
- package/assets/dashboard/assets/index-CQODXprH.css +0 -1
- package/src/content.ts +0 -502
- package/src/features/draft/draft.test.ts +0 -315
- package/src/features/rotate-field/rotate-field.test.ts +0 -297
- package/src/shared/activity-logger.ts +0 -79
- package/src/shared/notification-service.ts +0 -56
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import { Hono } from 'hono'
|
|
3
|
-
import type { Env, Variables } from '../../types'
|
|
4
|
-
|
|
5
|
-
const schemaApp = new Hono<{ Bindings: Env; Variables: Variables }>()
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Ritorna l'intero schema del CMS (la lista dei Seed configurati).
|
|
9
|
-
* Usato dalla Dashboard per generare dinamicamente il menu e le form.
|
|
10
|
-
*/
|
|
11
|
-
schemaApp.get('/', async (
|
|
12
|
-
const registry =
|
|
13
|
-
return
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
export { schemaApp }
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import { Hono } from 'hono'
|
|
3
|
+
import type { Env, Variables } from '../../types'
|
|
4
|
+
|
|
5
|
+
const schemaApp = new Hono<{ Bindings: Env; Variables: Variables }>()
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Ritorna l'intero schema del CMS (la lista dei Seed configurati).
|
|
9
|
+
* Usato dalla Dashboard per generare dinamicamente il menu e le form.
|
|
10
|
+
*/
|
|
11
|
+
schemaApp.get('/', async (context) => {
|
|
12
|
+
const registry = context.get('seedRegistry')
|
|
13
|
+
return context.json(registry.all())
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export { schemaApp }
|
|
@@ -1,249 +1,301 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import { Hono } from 'hono'
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
).
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import { Hono } from 'hono'
|
|
3
|
+
import type { Env, Variables } from '../../types'
|
|
4
|
+
|
|
5
|
+
const settingsApp = new Hono<{ Bindings: Env; Variables: Variables }>()
|
|
6
|
+
|
|
7
|
+
const EMAIL_VALIDATION_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
8
|
+
const MIN_PASSWORD_LENGTH = 8
|
|
9
|
+
const MAX_PASSWORD_LENGTH = 128
|
|
10
|
+
const SESSION_LIST_LIMIT = 20
|
|
11
|
+
const ACTIVITY_LOG_LIMIT = 30
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* GET /api/settings
|
|
15
|
+
* Retrieves the general site configuration.
|
|
16
|
+
*/
|
|
17
|
+
settingsApp.get('/', async (context) => {
|
|
18
|
+
return context.json({
|
|
19
|
+
siteTitle: 'Beech CMS',
|
|
20
|
+
siteLogo: '/beechLogoDark.svg',
|
|
21
|
+
defaultLanguage: 'it',
|
|
22
|
+
dateFormat: context.env.DATE_FORMAT || 'DD-MM-YYYY',
|
|
23
|
+
features: {
|
|
24
|
+
drafts: true,
|
|
25
|
+
media: true,
|
|
26
|
+
search: true,
|
|
27
|
+
activityLog: true
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* GET /api/settings/me
|
|
34
|
+
* Retrieves the currently authenticated user's profile and preferences.
|
|
35
|
+
*/
|
|
36
|
+
settingsApp.get('/me', async (context) => {
|
|
37
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
38
|
+
|
|
39
|
+
const currentUser = await context.get('userRepository').findById(userId)
|
|
40
|
+
if (!currentUser) {
|
|
41
|
+
return context.json({ error: 'User not found' }, 404)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let notificationPreferences: Record<string, boolean>
|
|
45
|
+
try {
|
|
46
|
+
notificationPreferences = JSON.parse(currentUser.notificationPreferences || '{}')
|
|
47
|
+
} catch {
|
|
48
|
+
notificationPreferences = {}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return context.json({
|
|
52
|
+
id: currentUser.id,
|
|
53
|
+
email: currentUser.email,
|
|
54
|
+
name: currentUser.name,
|
|
55
|
+
avatarUrl: currentUser.avatarUrl,
|
|
56
|
+
notificationPrefs: {
|
|
57
|
+
contentCreate: notificationPreferences.contentCreate ?? true,
|
|
58
|
+
contentUpdate: notificationPreferences.contentUpdate ?? true,
|
|
59
|
+
contentDelete: notificationPreferences.contentDelete ?? true,
|
|
60
|
+
mediaUpload: notificationPreferences.mediaUpload ?? false,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* PUT /api/settings/profile
|
|
67
|
+
* Updates the user's name and email address.
|
|
68
|
+
*/
|
|
69
|
+
settingsApp.put('/profile', async (context) => {
|
|
70
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
71
|
+
|
|
72
|
+
let payload: Record<string, unknown>
|
|
73
|
+
try {
|
|
74
|
+
payload = await context.req.json()
|
|
75
|
+
} catch {
|
|
76
|
+
return context.json({ error: 'Invalid JSON body' }, 400)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const nameInput = typeof payload.name === 'string' ? payload.name.trim() : null
|
|
80
|
+
const emailInput = typeof payload.email === 'string' ? payload.email.trim().toLowerCase() : null
|
|
81
|
+
|
|
82
|
+
if (emailInput !== null && !EMAIL_VALIDATION_REGEX.test(emailInput)) {
|
|
83
|
+
return context.json({ type: 'bad-request', title: 'Bad Request', status: 400, detail: 'Invalid email format' }, 400)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (nameInput !== null && nameInput.length > 100) {
|
|
87
|
+
return context.json({ type: 'bad-request', title: 'Bad Request', status: 400, detail: 'Name is too long (maximum 100 characters)' }, 400)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const hasNoFields = nameInput === null && emailInput === null
|
|
91
|
+
if (hasNoFields) {
|
|
92
|
+
return context.json({ error: 'No fields to update' }, 400)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (emailInput !== null) {
|
|
96
|
+
const emailTaken = await context.get('userRepository').emailBelongsToAnotherUser(emailInput, userId)
|
|
97
|
+
if (emailTaken) {
|
|
98
|
+
return context.json({ type: 'conflict', title: 'Conflict', status: 409, detail: 'Email address is already in use' }, 409)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const fieldsToUpdate: { name?: string; email?: string } = {}
|
|
103
|
+
if (nameInput !== null) fieldsToUpdate.name = nameInput
|
|
104
|
+
if (emailInput !== null) fieldsToUpdate.email = emailInput
|
|
105
|
+
|
|
106
|
+
await context.get('userRepository').updateProfile(userId, fieldsToUpdate)
|
|
107
|
+
return context.json({ success: true })
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* PUT /api/settings/password
|
|
112
|
+
* Updates the user's password after verifying the current one.
|
|
113
|
+
*/
|
|
114
|
+
settingsApp.put('/password', async (context) => {
|
|
115
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
116
|
+
|
|
117
|
+
let payload: Record<string, unknown>
|
|
118
|
+
try {
|
|
119
|
+
payload = await context.req.json()
|
|
120
|
+
} catch {
|
|
121
|
+
return context.json({ error: 'Invalid JSON body' }, 400)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const currentPassword = typeof payload.currentPassword === 'string' ? payload.currentPassword : ''
|
|
125
|
+
const newPassword = typeof payload.newPassword === 'string' ? payload.newPassword : ''
|
|
126
|
+
|
|
127
|
+
if (!currentPassword || !newPassword) {
|
|
128
|
+
return context.json({ error: 'Both currentPassword and newPassword are required' }, 400)
|
|
129
|
+
}
|
|
130
|
+
if (newPassword.length < MIN_PASSWORD_LENGTH) {
|
|
131
|
+
return context.json({ error: `Password must be at least ${MIN_PASSWORD_LENGTH} characters long` }, 400)
|
|
132
|
+
}
|
|
133
|
+
if (newPassword.length > MAX_PASSWORD_LENGTH) {
|
|
134
|
+
return context.json({ error: 'Password is too long' }, 400)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const userRecord = await context.get('userRepository').findById(userId)
|
|
138
|
+
if (!userRecord) {
|
|
139
|
+
return context.json({ error: 'User not found' }, 404)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const hashProvider = context.get('hashProvider')
|
|
143
|
+
const isPasswordCorrect = await hashProvider.verify(currentPassword, userRecord.passwordHash)
|
|
144
|
+
if (!isPasswordCorrect) {
|
|
145
|
+
return context.json({ type: 'invalid-credentials', title: 'Unauthorized', status: 401, detail: 'Current password is incorrect' }, 401)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const hashedNewPassword = await hashProvider.hash(newPassword)
|
|
149
|
+
await context.get('userRepository').updatePasswordHash(userId, hashedNewPassword)
|
|
150
|
+
return context.json({ success: true })
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* PUT /api/settings/avatar
|
|
155
|
+
* Updates the user's avatar URL.
|
|
156
|
+
*/
|
|
157
|
+
settingsApp.put('/avatar', async (context) => {
|
|
158
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
159
|
+
|
|
160
|
+
let payload: Record<string, unknown>
|
|
161
|
+
try {
|
|
162
|
+
payload = await context.req.json()
|
|
163
|
+
} catch {
|
|
164
|
+
return context.json({ error: 'Invalid JSON body' }, 400)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const avatarUrl = typeof payload.avatarUrl === 'string' ? payload.avatarUrl.trim() : null
|
|
168
|
+
await context.get('userRepository').updateAvatarUrl(userId, avatarUrl)
|
|
169
|
+
return context.json({ success: true })
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* GET /api/settings/sessions
|
|
174
|
+
* Retrieves a list of active refresh tokens for the user.
|
|
175
|
+
*/
|
|
176
|
+
settingsApp.get('/sessions', async (context) => {
|
|
177
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
178
|
+
const nowTimestamp = Math.floor(Date.now() / 1000)
|
|
179
|
+
const sessions = await context.get('sessionRepository').listActiveForUser(userId, nowTimestamp, SESSION_LIST_LIMIT)
|
|
180
|
+
return context.json(sessions)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* DELETE /api/settings/sessions/:id
|
|
185
|
+
* Revokes a specific refresh token (session).
|
|
186
|
+
*/
|
|
187
|
+
settingsApp.delete('/sessions/:id', async (context) => {
|
|
188
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
189
|
+
const sessionId = context.req.param('id')
|
|
190
|
+
const nowTimestamp = Math.floor(Date.now() / 1000)
|
|
191
|
+
|
|
192
|
+
const wasRevoked = await context.get('sessionRepository').revokeById(sessionId, userId, nowTimestamp)
|
|
193
|
+
if (!wasRevoked) {
|
|
194
|
+
return context.json({ error: 'Session not found or already revoked' }, 404)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return context.json({ success: true })
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* GET /api/settings/activity
|
|
202
|
+
* Retrieves the latest activity logs for the user.
|
|
203
|
+
*/
|
|
204
|
+
settingsApp.get('/activity', async (context) => {
|
|
205
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
206
|
+
|
|
207
|
+
const entries = await context.get('activityLogRepository').list({
|
|
208
|
+
userId,
|
|
209
|
+
limit: ACTIVITY_LOG_LIMIT,
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
// Preserve legacy snake_case shape consumed by the dashboard activity tab.
|
|
213
|
+
const responseEntries = entries.map((entry) => ({
|
|
214
|
+
id: entry.id,
|
|
215
|
+
action: entry.action,
|
|
216
|
+
entity_type: entry.entityType,
|
|
217
|
+
entity_slug: entry.entitySlug,
|
|
218
|
+
details: entry.details ? JSON.stringify(entry.details) : null,
|
|
219
|
+
created_at: entry.createdAt,
|
|
220
|
+
}))
|
|
221
|
+
|
|
222
|
+
return context.json(responseEntries)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* GET /api/settings/storage
|
|
227
|
+
* Calculates storage usage and identifies orphaned media files.
|
|
228
|
+
*/
|
|
229
|
+
settingsApp.get('/storage', async (context) => {
|
|
230
|
+
const mediaRepo = context.get('mediaRepository')
|
|
231
|
+
const statsRepo = context.get('systemStatsRepository')
|
|
232
|
+
|
|
233
|
+
const totalStorageUsedBytes = await statsRepo.getStorageUsage()
|
|
234
|
+
const totalFileCount = await mediaRepo.count()
|
|
235
|
+
|
|
236
|
+
const registeredSeeds = context.get('seedRegistry').all()
|
|
237
|
+
const referencedMediaKeys = await context.get('contentScanRepository').getReferencedMediaKeys(registeredSeeds)
|
|
238
|
+
|
|
239
|
+
const { items: allMediaRows } = await mediaRepo.list({ limit: 50, offset: 0 })
|
|
240
|
+
const orphanedMediaFiles = allMediaRows.filter(mediaFile => !referencedMediaKeys.has(mediaFile.key))
|
|
241
|
+
|
|
242
|
+
return context.json({
|
|
243
|
+
totalBytes: totalStorageUsedBytes,
|
|
244
|
+
fileCount: totalFileCount,
|
|
245
|
+
orphans: orphanedMediaFiles,
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* GET /api/settings/notifications
|
|
251
|
+
* Retrieves the user's notification preferences.
|
|
252
|
+
*/
|
|
253
|
+
settingsApp.get('/notifications', async (context) => {
|
|
254
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
255
|
+
|
|
256
|
+
const userRecord = await context.get('userRepository').findById(userId)
|
|
257
|
+
if (!userRecord) {
|
|
258
|
+
return context.json({ error: 'User not found' }, 404)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let userPreferences: Record<string, boolean>
|
|
262
|
+
try {
|
|
263
|
+
userPreferences = JSON.parse(userRecord.notificationPreferences || '{}')
|
|
264
|
+
} catch {
|
|
265
|
+
userPreferences = {}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return context.json({
|
|
269
|
+
contentCreate: userPreferences.contentCreate ?? true,
|
|
270
|
+
contentUpdate: userPreferences.contentUpdate ?? true,
|
|
271
|
+
contentDelete: userPreferences.contentDelete ?? true,
|
|
272
|
+
mediaUpload: userPreferences.mediaUpload ?? false,
|
|
273
|
+
})
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* PUT /api/settings/notifications
|
|
278
|
+
* Updates the user's notification preferences.
|
|
279
|
+
*/
|
|
280
|
+
settingsApp.put('/notifications', async (context) => {
|
|
281
|
+
const { sub: userId } = context.get('jwtPayload')
|
|
282
|
+
|
|
283
|
+
let payload: Record<string, unknown>
|
|
284
|
+
try {
|
|
285
|
+
payload = await context.req.json()
|
|
286
|
+
} catch {
|
|
287
|
+
return context.json({ error: 'Invalid JSON body' }, 400)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const newPreferences = {
|
|
291
|
+
contentCreate: payload.contentCreate === true,
|
|
292
|
+
contentUpdate: payload.contentUpdate === true,
|
|
293
|
+
contentDelete: payload.contentDelete === true,
|
|
294
|
+
mediaUpload: payload.mediaUpload === true,
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
await context.get('userRepository').updateNotificationPreferences(userId, JSON.stringify(newPreferences))
|
|
298
|
+
return context.json({ success: true })
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
export { settingsApp }
|