]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/users/my-notifications.ts
cef1d237c0f3725c859ef3ea69c6a8ac15e40e7f
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / my-notifications.ts
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 }