aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users/my-notifications.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-12-26 10:36:24 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-01-09 11:15:15 +0100
commitcef534ed53e4518fe0acf581bfe880788d42fc36 (patch)
tree115b51ea5136849a2336d44915c7780649f25dc2 /server/controllers/api/users/my-notifications.ts
parent1de1d05f4c61fe059fa5e24e79c92582f0e7e4b3 (diff)
downloadPeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.gz
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.zst
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.zip
Add user notification base code
Diffstat (limited to 'server/controllers/api/users/my-notifications.ts')
-rw-r--r--server/controllers/api/users/my-notifications.ts84
1 files changed, 84 insertions, 0 deletions
diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.ts
new file mode 100644
index 000000000..cef1d237c
--- /dev/null
+++ b/server/controllers/api/users/my-notifications.ts
@@ -0,0 +1,84 @@
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'
12import { UserModel } from '../../../models/account/user'
13import { getFormattedObjects } from '../../../helpers/utils'
14import { UserNotificationModel } from '../../../models/account/user-notification'
15import { meRouter } from './me'
16import {
17 markAsReadUserNotificationsValidator,
18 updateNotificationSettingsValidator
19} from '../../../middlewares/validators/user-notifications'
20import { UserNotificationSetting } from '../../../../shared/models/users'
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,
37 asyncMiddleware(listUserNotifications)
38)
39
40myNotificationsRouter.post('/me/notifications/read',
41 authenticate,
42 markAsReadUserNotificationsValidator,
43 asyncMiddleware(markAsReadUserNotifications)
44)
45
46export {
47 myNotificationsRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async function updateNotificationSettings (req: express.Request, res: express.Response) {
53 const user: UserModel = res.locals.oauth.token.User
54 const body: UserNotificationSetting = req.body
55
56 const query = {
57 where: {
58 userId: user.id
59 }
60 }
61
62 await UserNotificationSettingModel.update({
63 newVideoFromSubscription: body.newVideoFromSubscription,
64 newCommentOnMyVideo: body.newCommentOnMyVideo
65 }, query)
66
67 return res.status(204).end()
68}
69
70async function listUserNotifications (req: express.Request, res: express.Response) {
71 const user: UserModel = res.locals.oauth.token.User
72
73 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort)
74
75 return res.json(getFormattedObjects(resultList.data, resultList.total))
76}
77
78async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
79 const user: UserModel = res.locals.oauth.token.User
80
81 await UserNotificationModel.markAsRead(user.id, req.body.ids)
82
83 return res.status(204).end()
84}