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