aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users/my-notifications.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/users/my-notifications.ts')
-rw-r--r--server/controllers/api/users/my-notifications.ts84
1 files changed, 84 insertions, 0 deletions
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 @@
1import * as express from 'express'
2import 'multer'
3import {
4 asyncMiddleware,
5 asyncRetryTransactionMiddleware,
6 authenticate,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort,
10 userNotificationsSortValidator
11} from '../../../middlewares'
12import { UserModel } from '../../../models/account/user'
13import { getFormattedObjects } from '../../../helpers/utils'
14import { UserNotificationModel } from '../../../models/account/user-notification'
15import { meRouter } from './me'
16import {
17 markAsReadUserNotificationsValidator,
18 updateNotificationSettingsValidator
19} from '../../../middlewares/validators/user-notifications'
20import { UserNotificationSetting } from '../../../../shared/models/users'
21import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
22
23const myNotificationsRouter = express.Router()
24
25meRouter.put('/me/notification-settings',
26 authenticate,
27 updateNotificationSettingsValidator,
28 asyncRetryTransactionMiddleware(updateNotificationSettings)
29)
30
31myNotificationsRouter.get('/me/notifications',
32 authenticate,
33 paginationValidator,
34 userNotificationsSortValidator,
35 setDefaultSort,
36 setDefaultPagination,
37 asyncMiddleware(listUserNotifications)
38)
39
40myNotificationsRouter.post('/me/notifications/read',
41 authenticate,
42 markAsReadUserNotificationsValidator,
43 asyncMiddleware(markAsReadUserNotifications)
44)
45
46export {
47 myNotificationsRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async 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
70async 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
78async 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}