diff options
Diffstat (limited to 'server/controllers/api/users')
-rw-r--r-- | server/controllers/api/users/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/users/my-notifications.ts | 84 |
2 files changed, 86 insertions, 0 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index bc24792a2..98be46ea2 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts | |||
@@ -39,6 +39,7 @@ import { meRouter } from './me' | |||
39 | import { deleteUserToken } from '../../../lib/oauth-model' | 39 | import { deleteUserToken } from '../../../lib/oauth-model' |
40 | import { myBlocklistRouter } from './my-blocklist' | 40 | import { myBlocklistRouter } from './my-blocklist' |
41 | import { myVideosHistoryRouter } from './my-history' | 41 | import { myVideosHistoryRouter } from './my-history' |
42 | import { myNotificationsRouter } from './my-notifications' | ||
42 | 43 | ||
43 | const auditLogger = auditLoggerFactory('users') | 44 | const auditLogger = auditLoggerFactory('users') |
44 | 45 | ||
@@ -55,6 +56,7 @@ const askSendEmailLimiter = new RateLimit({ | |||
55 | }) | 56 | }) |
56 | 57 | ||
57 | const usersRouter = express.Router() | 58 | const usersRouter = express.Router() |
59 | usersRouter.use('/', myNotificationsRouter) | ||
58 | usersRouter.use('/', myBlocklistRouter) | 60 | usersRouter.use('/', myBlocklistRouter) |
59 | usersRouter.use('/', myVideosHistoryRouter) | 61 | usersRouter.use('/', myVideosHistoryRouter) |
60 | usersRouter.use('/', meRouter) | 62 | usersRouter.use('/', meRouter) |
diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.ts new file mode 100644 index 000000000..cef1d237c --- /dev/null +++ b/server/controllers/api/users/my-notifications.ts | |||
@@ -0,0 +1,84 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'multer' | ||
3 | import { | ||
4 | asyncMiddleware, | ||
5 | asyncRetryTransactionMiddleware, | ||
6 | authenticate, | ||
7 | paginationValidator, | ||
8 | setDefaultPagination, | ||
9 | setDefaultSort, | ||
10 | userNotificationsSortValidator | ||
11 | } from '../../../middlewares' | ||
12 | import { UserModel } from '../../../models/account/user' | ||
13 | import { getFormattedObjects } from '../../../helpers/utils' | ||
14 | import { UserNotificationModel } from '../../../models/account/user-notification' | ||
15 | import { meRouter } from './me' | ||
16 | import { | ||
17 | markAsReadUserNotificationsValidator, | ||
18 | updateNotificationSettingsValidator | ||
19 | } from '../../../middlewares/validators/user-notifications' | ||
20 | import { UserNotificationSetting } from '../../../../shared/models/users' | ||
21 | import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting' | ||
22 | |||
23 | const myNotificationsRouter = express.Router() | ||
24 | |||
25 | meRouter.put('/me/notification-settings', | ||
26 | authenticate, | ||
27 | updateNotificationSettingsValidator, | ||
28 | asyncRetryTransactionMiddleware(updateNotificationSettings) | ||
29 | ) | ||
30 | |||
31 | myNotificationsRouter.get('/me/notifications', | ||
32 | authenticate, | ||
33 | paginationValidator, | ||
34 | userNotificationsSortValidator, | ||
35 | setDefaultSort, | ||
36 | setDefaultPagination, | ||
37 | asyncMiddleware(listUserNotifications) | ||
38 | ) | ||
39 | |||
40 | myNotificationsRouter.post('/me/notifications/read', | ||
41 | authenticate, | ||
42 | markAsReadUserNotificationsValidator, | ||
43 | asyncMiddleware(markAsReadUserNotifications) | ||
44 | ) | ||
45 | |||
46 | export { | ||
47 | myNotificationsRouter | ||
48 | } | ||
49 | |||
50 | // --------------------------------------------------------------------------- | ||
51 | |||
52 | async function updateNotificationSettings (req: express.Request, res: express.Response) { | ||
53 | const user: UserModel = res.locals.oauth.token.User | ||
54 | const body: UserNotificationSetting = req.body | ||
55 | |||
56 | const query = { | ||
57 | where: { | ||
58 | userId: user.id | ||
59 | } | ||
60 | } | ||
61 | |||
62 | await UserNotificationSettingModel.update({ | ||
63 | newVideoFromSubscription: body.newVideoFromSubscription, | ||
64 | newCommentOnMyVideo: body.newCommentOnMyVideo | ||
65 | }, query) | ||
66 | |||
67 | return res.status(204).end() | ||
68 | } | ||
69 | |||
70 | async function listUserNotifications (req: express.Request, res: express.Response) { | ||
71 | const user: UserModel = res.locals.oauth.token.User | ||
72 | |||
73 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort) | ||
74 | |||
75 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
76 | } | ||
77 | |||
78 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | ||
79 | const user: UserModel = res.locals.oauth.token.User | ||
80 | |||
81 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | ||
82 | |||
83 | return res.status(204).end() | ||
84 | } | ||