2018-12-26 04:36:24 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import 'multer'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort,
|
|
|
|
userNotificationsSortValidator
|
|
|
|
} from '../../../middlewares'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
|
|
|
import { UserNotificationModel } from '../../../models/account/user-notification'
|
|
|
|
import { meRouter } from './me'
|
|
|
|
import {
|
2019-01-02 10:37:43 -05:00
|
|
|
listUserNotificationsValidator,
|
2018-12-26 04:36:24 -05:00
|
|
|
markAsReadUserNotificationsValidator,
|
|
|
|
updateNotificationSettingsValidator
|
|
|
|
} from '../../../middlewares/validators/user-notifications'
|
2019-01-04 02:56:20 -05:00
|
|
|
import { UserNotificationSetting } from '../../../../shared/models/users'
|
2018-12-26 04:36:24 -05:00
|
|
|
import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
|
2020-12-07 08:32:36 -05:00
|
|
|
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
2018-12-26 04:36:24 -05: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 10:37:43 -05:00
|
|
|
listUserNotificationsValidator,
|
2018-12-26 04:36:24 -05:00
|
|
|
asyncMiddleware(listUserNotifications)
|
|
|
|
)
|
|
|
|
|
|
|
|
myNotificationsRouter.post('/me/notifications/read',
|
|
|
|
authenticate,
|
|
|
|
markAsReadUserNotificationsValidator,
|
|
|
|
asyncMiddleware(markAsReadUserNotifications)
|
|
|
|
)
|
|
|
|
|
2019-01-08 05:26:41 -05:00
|
|
|
myNotificationsRouter.post('/me/notifications/read-all',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(markAsReadAllUserNotifications)
|
|
|
|
)
|
|
|
|
|
2018-12-26 04:36:24 -05:00
|
|
|
export {
|
|
|
|
myNotificationsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function updateNotificationSettings (req: express.Request, res: express.Response) {
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
const body = req.body as UserNotificationSetting
|
2018-12-26 04:36:24 -05:00
|
|
|
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 02:56:20 -05:00
|
|
|
const values: UserNotificationSetting = {
|
2018-12-26 04:36:24 -05:00
|
|
|
newVideoFromSubscription: body.newVideoFromSubscription,
|
2019-01-02 10:37:43 -05:00
|
|
|
newCommentOnMyVideo: body.newCommentOnMyVideo,
|
2020-07-07 08:34:16 -04:00
|
|
|
abuseAsModerator: body.abuseAsModerator,
|
2019-04-02 05:26:47 -04:00
|
|
|
videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
|
2019-01-02 10:37:43 -05:00
|
|
|
blacklistOnMyVideo: body.blacklistOnMyVideo,
|
|
|
|
myVideoPublished: body.myVideoPublished,
|
2019-01-04 02:56:20 -05:00
|
|
|
myVideoImportFinished: body.myVideoImportFinished,
|
|
|
|
newFollow: body.newFollow,
|
|
|
|
newUserRegistration: body.newUserRegistration,
|
2019-04-08 11:26:01 -04:00
|
|
|
commentMention: body.commentMention,
|
2019-08-30 10:50:12 -04:00
|
|
|
newInstanceFollower: body.newInstanceFollower,
|
2020-07-27 10:26:25 -04:00
|
|
|
autoInstanceFollowing: body.autoInstanceFollowing,
|
|
|
|
abuseNewMessage: body.abuseNewMessage,
|
|
|
|
abuseStateChange: body.abuseStateChange
|
2019-01-04 02:56:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
await UserNotificationSettingModel.update(values, query)
|
2018-12-26 04:36:24 -05:00
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-12-26 04:36:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function listUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-26 04:36:24 -05:00
|
|
|
|
2019-01-02 10:37:43 -05:00
|
|
|
const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
|
2018-12-26 04:36:24 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-26 04:36:24 -05:00
|
|
|
|
|
|
|
await UserNotificationModel.markAsRead(user.id, req.body.ids)
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-12-26 04:36:24 -05:00
|
|
|
}
|
2019-01-08 05:26:41 -05:00
|
|
|
|
|
|
|
async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-01-08 05:26:41 -05:00
|
|
|
|
|
|
|
await UserNotificationModel.markAllAsRead(user.id)
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-01-08 05:26:41 -05:00
|
|
|
}
|