]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/users/my-notifications.ts
017f5219edeb5a3b65bf6bbe855243d7d35aaefb
[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 { getFormattedObjects } from '../../../helpers/utils'
13 import { UserNotificationModel } from '../../../models/account/user-notification'
14 import { meRouter } from './me'
15 import {
16 listUserNotificationsValidator,
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 listUserNotificationsValidator,
38 asyncMiddleware(listUserNotifications)
39 )
40
41 myNotificationsRouter.post('/me/notifications/read',
42 authenticate,
43 markAsReadUserNotificationsValidator,
44 asyncMiddleware(markAsReadUserNotifications)
45 )
46
47 myNotificationsRouter.post('/me/notifications/read-all',
48 authenticate,
49 asyncMiddleware(markAsReadAllUserNotifications)
50 )
51
52 export {
53 myNotificationsRouter
54 }
55
56 // ---------------------------------------------------------------------------
57
58 async function updateNotificationSettings (req: express.Request, res: express.Response) {
59 const user = res.locals.oauth.token.User
60 const body = req.body as UserNotificationSetting
61
62 const query = {
63 where: {
64 userId: user.id
65 }
66 }
67
68 const values: UserNotificationSetting = {
69 newVideoFromSubscription: body.newVideoFromSubscription,
70 newCommentOnMyVideo: body.newCommentOnMyVideo,
71 videoAbuseAsModerator: body.videoAbuseAsModerator,
72 videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
73 blacklistOnMyVideo: body.blacklistOnMyVideo,
74 myVideoPublished: body.myVideoPublished,
75 myVideoImportFinished: body.myVideoImportFinished,
76 newFollow: body.newFollow,
77 newUserRegistration: body.newUserRegistration,
78 commentMention: body.commentMention,
79 newInstanceFollower: body.newInstanceFollower,
80 autoInstanceFollowing: body.autoInstanceFollowing
81 }
82
83 await UserNotificationSettingModel.update(values, query)
84
85 return res.status(204).end()
86 }
87
88 async function listUserNotifications (req: express.Request, res: express.Response) {
89 const user = res.locals.oauth.token.User
90
91 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
92
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
94 }
95
96 async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
97 const user = res.locals.oauth.token.User
98
99 await UserNotificationModel.markAsRead(user.id, req.body.ids)
100
101 return res.status(204).end()
102 }
103
104 async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
105 const user = res.locals.oauth.token.User
106
107 await UserNotificationModel.markAllAsRead(user.id)
108
109 return res.status(204).end()
110 }