]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/my-notifications.ts
replace numbers with typed http status codes (#3409)
[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 21import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
2d53be02 22import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
cef534ed
C
23
24const myNotificationsRouter = express.Router()
25
26meRouter.put('/me/notification-settings',
27 authenticate,
28 updateNotificationSettingsValidator,
29 asyncRetryTransactionMiddleware(updateNotificationSettings)
30)
31
32myNotificationsRouter.get('/me/notifications',
33 authenticate,
34 paginationValidator,
35 userNotificationsSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
dc133480 38 listUserNotificationsValidator,
cef534ed
C
39 asyncMiddleware(listUserNotifications)
40)
41
42myNotificationsRouter.post('/me/notifications/read',
43 authenticate,
44 markAsReadUserNotificationsValidator,
45 asyncMiddleware(markAsReadUserNotifications)
46)
47
2f1548fd
C
48myNotificationsRouter.post('/me/notifications/read-all',
49 authenticate,
50 asyncMiddleware(markAsReadAllUserNotifications)
51)
52
cef534ed
C
53export {
54 myNotificationsRouter
55}
56
57// ---------------------------------------------------------------------------
58
59async function updateNotificationSettings (req: express.Request, res: express.Response) {
dae86118
C
60 const user = res.locals.oauth.token.User
61 const body = req.body as UserNotificationSetting
cef534ed
C
62
63 const query = {
64 where: {
65 userId: user.id
66 }
67 }
68
f7cc67b4 69 const values: UserNotificationSetting = {
cef534ed 70 newVideoFromSubscription: body.newVideoFromSubscription,
dc133480 71 newCommentOnMyVideo: body.newCommentOnMyVideo,
4f32032f 72 abuseAsModerator: body.abuseAsModerator,
7ccddd7b 73 videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
dc133480
C
74 blacklistOnMyVideo: body.blacklistOnMyVideo,
75 myVideoPublished: body.myVideoPublished,
f7cc67b4
C
76 myVideoImportFinished: body.myVideoImportFinished,
77 newFollow: body.newFollow,
78 newUserRegistration: body.newUserRegistration,
883993c8 79 commentMention: body.commentMention,
8424c402 80 newInstanceFollower: body.newInstanceFollower,
594d3e48
C
81 autoInstanceFollowing: body.autoInstanceFollowing,
82 abuseNewMessage: body.abuseNewMessage,
83 abuseStateChange: body.abuseStateChange
f7cc67b4
C
84 }
85
86 await UserNotificationSettingModel.update(values, query)
cef534ed 87
2d53be02 88 return res.status(HttpStatusCode.NO_CONTENT_204).end()
cef534ed
C
89}
90
91async function listUserNotifications (req: express.Request, res: express.Response) {
dae86118 92 const user = res.locals.oauth.token.User
cef534ed 93
dc133480 94 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
cef534ed
C
95
96 return res.json(getFormattedObjects(resultList.data, resultList.total))
97}
98
99async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
dae86118 100 const user = res.locals.oauth.token.User
cef534ed
C
101
102 await UserNotificationModel.markAsRead(user.id, req.body.ids)
103
2d53be02 104 return res.status(HttpStatusCode.NO_CONTENT_204).end()
cef534ed 105}
2f1548fd
C
106
107async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
dae86118 108 const user = res.locals.oauth.token.User
2f1548fd
C
109
110 await UserNotificationModel.markAllAsRead(user.id)
111
2d53be02 112 return res.status(HttpStatusCode.NO_CONTENT_204).end()
2f1548fd 113}