]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/my-notifications.ts
Add auto follow back support for instances
[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 78 commentMention: body.commentMention,
8424c402
C
79 newInstanceFollower: body.newInstanceFollower,
80 autoInstanceFollowing: body.autoInstanceFollowing
f7cc67b4
C
81 }
82
83 await UserNotificationSettingModel.update(values, query)
cef534ed
C
84
85 return res.status(204).end()
86}
87
88async function listUserNotifications (req: express.Request, res: express.Response) {
dae86118 89 const user = res.locals.oauth.token.User
cef534ed 90
dc133480 91 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
cef534ed
C
92
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
94}
95
96async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
dae86118 97 const user = res.locals.oauth.token.User
cef534ed
C
98
99 await UserNotificationModel.markAsRead(user.id, req.body.ids)
100
101 return res.status(204).end()
102}
2f1548fd
C
103
104async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
dae86118 105 const user = res.locals.oauth.token.User
2f1548fd
C
106
107 await UserNotificationModel.markAllAsRead(user.id)
108
109 return res.status(204).end()
110}