aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users/my-notifications.ts
blob: 2909770daed97391f59511b28f21e85338e934c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'multer'
import * as express from 'express'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
import { UserNotificationSetting } from '../../../../shared/models/users'
import { getFormattedObjects } from '../../../helpers/utils'
import {
  asyncMiddleware,
  asyncRetryTransactionMiddleware,
  authenticate,
  paginationValidator,
  setDefaultPagination,
  setDefaultSort,
  userNotificationsSortValidator
} from '../../../middlewares'
import {
  listUserNotificationsValidator,
  markAsReadUserNotificationsValidator,
  updateNotificationSettingsValidator
} from '../../../middlewares/validators/user-notifications'
import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting'
import { meRouter } from './me'

const myNotificationsRouter = express.Router()

meRouter.put('/me/notification-settings',
  authenticate,
  updateNotificationSettingsValidator,
  asyncRetryTransactionMiddleware(updateNotificationSettings)
)

myNotificationsRouter.get('/me/notifications',
  authenticate,
  paginationValidator,
  userNotificationsSortValidator,
  setDefaultSort,
  setDefaultPagination,
  listUserNotificationsValidator,
  asyncMiddleware(listUserNotifications)
)

myNotificationsRouter.post('/me/notifications/read',
  authenticate,
  markAsReadUserNotificationsValidator,
  asyncMiddleware(markAsReadUserNotifications)
)

myNotificationsRouter.post('/me/notifications/read-all',
  authenticate,
  asyncMiddleware(markAsReadAllUserNotifications)
)

export {
  myNotificationsRouter
}

// ---------------------------------------------------------------------------

async function updateNotificationSettings (req: express.Request, res: express.Response) {
  const user = res.locals.oauth.token.User
  const body = req.body as UserNotificationSetting

  const query = {
    where: {
      userId: user.id
    }
  }

  const values: UserNotificationSetting = {
    newVideoFromSubscription: body.newVideoFromSubscription,
    newCommentOnMyVideo: body.newCommentOnMyVideo,
    abuseAsModerator: body.abuseAsModerator,
    videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
    blacklistOnMyVideo: body.blacklistOnMyVideo,
    myVideoPublished: body.myVideoPublished,
    myVideoImportFinished: body.myVideoImportFinished,
    newFollow: body.newFollow,
    newUserRegistration: body.newUserRegistration,
    commentMention: body.commentMention,
    newInstanceFollower: body.newInstanceFollower,
    autoInstanceFollowing: body.autoInstanceFollowing,
    abuseNewMessage: body.abuseNewMessage,
    abuseStateChange: body.abuseStateChange,
    newPeerTubeVersion: body.newPeerTubeVersion,
    newPluginVersion: body.newPluginVersion
  }

  await UserNotificationSettingModel.update(values, query)

  return res.status(HttpStatusCode.NO_CONTENT_204).end()
}

async function listUserNotifications (req: express.Request, res: express.Response) {
  const user = res.locals.oauth.token.User

  const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}

async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
  const user = res.locals.oauth.token.User

  await UserNotificationModel.markAsRead(user.id, req.body.ids)

  return res.status(HttpStatusCode.NO_CONTENT_204).end()
}

async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
  const user = res.locals.oauth.token.User

  await UserNotificationModel.markAllAsRead(user.id)

  return res.status(HttpStatusCode.NO_CONTENT_204).end()
}