2018-12-26 03:36:24 -06:00
|
|
|
import 'multer'
|
2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { HttpStatusCode, UserNotificationSetting } from '@peertube/peertube-models'
|
|
|
|
import { getFormattedObjects } from '@server/helpers/utils.js'
|
|
|
|
import { UserNotificationModel } from '@server/models/user/user-notification.js'
|
2018-12-26 03:36:24 -06:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort,
|
|
|
|
userNotificationsSortValidator
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '../../../middlewares/index.js'
|
2018-12-26 03:36:24 -06:00
|
|
|
import {
|
2019-01-02 09:37:43 -06:00
|
|
|
listUserNotificationsValidator,
|
2018-12-26 03:36:24 -06:00
|
|
|
markAsReadUserNotificationsValidator,
|
|
|
|
updateNotificationSettingsValidator
|
2024-02-12 03:47:52 -06:00
|
|
|
} from '../../../middlewares/validators/users/user-notifications.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting.js'
|
|
|
|
import { meRouter } from './me.js'
|
2018-12-26 03:36:24 -06:00
|
|
|
|
|
|
|
const myNotificationsRouter = express.Router()
|
|
|
|
|
|
|
|
meRouter.put('/me/notification-settings',
|
|
|
|
authenticate,
|
|
|
|
updateNotificationSettingsValidator,
|
|
|
|
asyncRetryTransactionMiddleware(updateNotificationSettings)
|
|
|
|
)
|
|
|
|
|
|
|
|
myNotificationsRouter.get('/me/notifications',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
userNotificationsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
2019-01-02 09:37:43 -06:00
|
|
|
listUserNotificationsValidator,
|
2018-12-26 03:36:24 -06:00
|
|
|
asyncMiddleware(listUserNotifications)
|
|
|
|
)
|
|
|
|
|
|
|
|
myNotificationsRouter.post('/me/notifications/read',
|
|
|
|
authenticate,
|
|
|
|
markAsReadUserNotificationsValidator,
|
|
|
|
asyncMiddleware(markAsReadUserNotifications)
|
|
|
|
)
|
|
|
|
|
2019-01-08 04:26:41 -06:00
|
|
|
myNotificationsRouter.post('/me/notifications/read-all',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(markAsReadAllUserNotifications)
|
|
|
|
)
|
|
|
|
|
2018-12-26 03:36:24 -06:00
|
|
|
export {
|
|
|
|
myNotificationsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function updateNotificationSettings (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
const body = req.body as UserNotificationSetting
|
2018-12-26 03:36:24 -06:00
|
|
|
|
2019-01-04 01:56:20 -06:00
|
|
|
const values: UserNotificationSetting = {
|
2018-12-26 03:36:24 -06:00
|
|
|
newVideoFromSubscription: body.newVideoFromSubscription,
|
2019-01-02 09:37:43 -06:00
|
|
|
newCommentOnMyVideo: body.newCommentOnMyVideo,
|
2020-07-07 07:34:16 -05:00
|
|
|
abuseAsModerator: body.abuseAsModerator,
|
2019-04-02 04:26:47 -05:00
|
|
|
videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
|
2019-01-02 09:37:43 -06:00
|
|
|
blacklistOnMyVideo: body.blacklistOnMyVideo,
|
|
|
|
myVideoPublished: body.myVideoPublished,
|
2019-01-04 01:56:20 -06:00
|
|
|
myVideoImportFinished: body.myVideoImportFinished,
|
|
|
|
newFollow: body.newFollow,
|
|
|
|
newUserRegistration: body.newUserRegistration,
|
2019-04-08 10:26:01 -05:00
|
|
|
commentMention: body.commentMention,
|
2019-08-30 09:50:12 -05:00
|
|
|
newInstanceFollower: body.newInstanceFollower,
|
2020-07-27 09:26:25 -05:00
|
|
|
autoInstanceFollowing: body.autoInstanceFollowing,
|
|
|
|
abuseNewMessage: body.abuseNewMessage,
|
2021-03-11 09:54:52 -06:00
|
|
|
abuseStateChange: body.abuseStateChange,
|
|
|
|
newPeerTubeVersion: body.newPeerTubeVersion,
|
2022-03-22 08:35:04 -05:00
|
|
|
newPluginVersion: body.newPluginVersion,
|
2022-03-22 10:58:49 -05:00
|
|
|
myVideoStudioEditionFinished: body.myVideoStudioEditionFinished
|
2019-01-04 01:56:20 -06:00
|
|
|
}
|
|
|
|
|
2024-02-12 03:47:52 -06:00
|
|
|
await UserNotificationSettingModel.updateUserSettings(values, user.id)
|
2018-12-26 03:36:24 -06:00
|
|
|
|
2024-02-12 03:47:52 -06:00
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
2018-12-26 03:36:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function listUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-26 03:36:24 -06:00
|
|
|
|
2019-01-02 09:37:43 -06:00
|
|
|
const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
|
2018-12-26 03:36:24 -06:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-26 03:36:24 -06:00
|
|
|
|
|
|
|
await UserNotificationModel.markAsRead(user.id, req.body.ids)
|
|
|
|
|
2024-02-12 03:47:52 -06:00
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
2018-12-26 03:36:24 -06:00
|
|
|
}
|
2019-01-08 04:26:41 -06:00
|
|
|
|
|
|
|
async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-01-08 04:26:41 -06:00
|
|
|
|
|
|
|
await UserNotificationModel.markAllAsRead(user.id)
|
|
|
|
|
2024-02-12 03:47:52 -06:00
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
2019-01-08 04:26:41 -06:00
|
|
|
}
|