1 import * as express from 'express'
5 asyncRetryTransactionMiddleware,
10 userNotificationsSortValidator
11 } from '../../../middlewares'
12 import { getFormattedObjects } from '../../../helpers/utils'
13 import { UserNotificationModel } from '../../../models/account/user-notification'
14 import { meRouter } from './me'
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'
23 const myNotificationsRouter = express.Router()
25 meRouter.put('/me/notification-settings',
27 updateNotificationSettingsValidator,
28 asyncRetryTransactionMiddleware(updateNotificationSettings)
31 myNotificationsRouter.get('/me/notifications',
34 userNotificationsSortValidator,
37 listUserNotificationsValidator,
38 asyncMiddleware(listUserNotifications)
41 myNotificationsRouter.post('/me/notifications/read',
43 markAsReadUserNotificationsValidator,
44 asyncMiddleware(markAsReadUserNotifications)
47 myNotificationsRouter.post('/me/notifications/read-all',
49 asyncMiddleware(markAsReadAllUserNotifications)
56 // ---------------------------------------------------------------------------
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
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
83 await UserNotificationSettingModel.update(values, query)
85 return res.status(204).end()
88 async function listUserNotifications (req: express.Request, res: express.Response) {
89 const user = res.locals.oauth.token.User
91 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
96 async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
97 const user = res.locals.oauth.token.User
99 await UserNotificationModel.markAsRead(user.id, req.body.ids)
101 return res.status(204).end()
104 async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
105 const user = res.locals.oauth.token.User
107 await UserNotificationModel.markAllAsRead(user.id)
109 return res.status(204).end()