2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2019-08-15 04:53:26 -05:00
|
|
|
import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../../../shared'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
2018-08-13 09:57:13 -05:00
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
blacklistSortValidator,
|
|
|
|
ensureUserHasRight,
|
|
|
|
paginationValidator,
|
|
|
|
setBlacklistSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosBlacklistAddValidator,
|
2019-08-15 04:53:26 -05:00
|
|
|
videosBlacklistFiltersValidator,
|
2018-08-13 09:57:13 -05:00
|
|
|
videosBlacklistRemoveValidator,
|
2019-08-15 04:53:26 -05:00
|
|
|
videosBlacklistUpdateValidator
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../../middlewares'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
|
2018-08-13 09:57:13 -05:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2018-12-26 03:36:24 -06:00
|
|
|
import { Notifier } from '../../../lib/notifier'
|
2019-02-26 03:55:40 -06:00
|
|
|
import { sendDeleteVideo } from '../../../lib/activitypub/send'
|
2019-01-10 08:39:51 -06:00
|
|
|
import { federateVideoIfNeeded } from '../../../lib/activitypub'
|
2019-08-15 04:53:26 -05:00
|
|
|
import { MVideoBlacklistVideo } from '@server/typings/models'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const blacklistRouter = express.Router()
|
|
|
|
|
2017-10-10 03:02:18 -05:00
|
|
|
blacklistRouter.post('/:videoId/blacklist',
|
2017-05-15 15:22:03 -05:00
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(videosBlacklistAddValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(addVideoToBlacklist)
|
2017-05-05 09:53:35 -05:00
|
|
|
)
|
|
|
|
|
2017-10-10 03:02:18 -05:00
|
|
|
blacklistRouter.get('/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-10-10 03:02:18 -05:00
|
|
|
paginationValidator,
|
|
|
|
blacklistSortValidator,
|
|
|
|
setBlacklistSort,
|
2018-01-18 03:53:54 -06:00
|
|
|
setDefaultPagination,
|
2019-04-02 04:26:47 -05:00
|
|
|
videosBlacklistFiltersValidator,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(listBlacklist)
|
2017-10-10 03:02:18 -05:00
|
|
|
)
|
|
|
|
|
2018-08-13 09:57:13 -05:00
|
|
|
blacklistRouter.put('/:videoId/blacklist',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
|
|
|
asyncMiddleware(videosBlacklistUpdateValidator),
|
|
|
|
asyncMiddleware(updateVideoBlacklistController)
|
|
|
|
)
|
|
|
|
|
2017-10-10 03:02:18 -05:00
|
|
|
blacklistRouter.delete('/:videoId/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(videosBlacklistRemoveValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(removeVideoFromBlacklistController)
|
2017-10-10 03:02:18 -05:00
|
|
|
)
|
|
|
|
|
2017-05-05 09:53:35 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
blacklistRouter
|
|
|
|
}
|
2017-05-05 09:53:35 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-08-13 09:57:13 -05:00
|
|
|
async function addVideoToBlacklist (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoInstance = res.locals.videoAll
|
2018-08-13 09:57:13 -05:00
|
|
|
const body: VideoBlacklistCreate = req.body
|
2017-05-05 09:53:35 -05:00
|
|
|
|
|
|
|
const toCreate = {
|
2018-08-13 09:57:13 -05:00
|
|
|
videoId: videoInstance.id,
|
2019-01-10 08:39:51 -06:00
|
|
|
unfederated: body.unfederate === true,
|
2019-04-02 04:26:47 -05:00
|
|
|
reason: body.reason,
|
|
|
|
type: VideoBlacklistType.MANUAL
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create(toCreate)
|
2018-12-26 03:36:24 -06:00
|
|
|
blacklist.Video = videoInstance
|
|
|
|
|
2019-01-10 08:39:51 -06:00
|
|
|
if (body.unfederate === true) {
|
|
|
|
await sendDeleteVideo(videoInstance, undefined)
|
|
|
|
}
|
|
|
|
|
2018-12-26 03:36:24 -06:00
|
|
|
Notifier.Instance.notifyOnVideoBlacklist(blacklist)
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
logger.info('Video %s blacklisted.', videoInstance.uuid)
|
2018-12-26 03:36:24 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
2017-10-10 03:02:18 -05:00
|
|
|
|
2018-08-13 09:57:13 -05:00
|
|
|
async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoBlacklist = res.locals.videoBlacklist
|
2018-08-13 09:57:13 -05:00
|
|
|
|
|
|
|
if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(t => {
|
|
|
|
return videoBlacklist.save({ transaction: t })
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2019-07-23 02:48:48 -05:00
|
|
|
async function listBlacklist (req: express.Request, res: express.Response) {
|
2019-04-02 04:26:47 -05:00
|
|
|
const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.type)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2019-07-23 02:48:48 -05:00
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2017-10-10 03:02:18 -05:00
|
|
|
}
|
|
|
|
|
2019-07-23 02:48:48 -05:00
|
|
|
async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoBlacklist = res.locals.videoBlacklist
|
2019-08-15 04:53:26 -05:00
|
|
|
const video = res.locals.videoAll
|
2017-10-10 03:02:18 -05:00
|
|
|
|
2019-04-02 04:26:47 -05:00
|
|
|
const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
|
2019-01-10 08:39:51 -06:00
|
|
|
const unfederated = videoBlacklist.unfederated
|
2019-04-02 04:26:47 -05:00
|
|
|
const videoBlacklistType = videoBlacklist.type
|
|
|
|
|
2019-01-10 08:39:51 -06:00
|
|
|
await videoBlacklist.destroy({ transaction: t })
|
2019-07-23 05:04:15 -05:00
|
|
|
video.VideoBlacklist = undefined
|
2019-01-10 08:39:51 -06:00
|
|
|
|
|
|
|
// Re federate the video
|
|
|
|
if (unfederated === true) {
|
|
|
|
await federateVideoIfNeeded(video, true, t)
|
|
|
|
}
|
2019-04-02 04:26:47 -05:00
|
|
|
|
|
|
|
return videoBlacklistType
|
2018-08-13 09:57:13 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2018-12-26 03:36:24 -06:00
|
|
|
Notifier.Instance.notifyOnVideoUnblacklist(video)
|
|
|
|
|
2019-04-02 04:26:47 -05:00
|
|
|
if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
|
|
|
|
Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
|
|
|
|
|
|
|
|
// Delete on object so new video notifications will send
|
|
|
|
delete video.VideoBlacklist
|
2019-07-23 05:04:15 -05:00
|
|
|
Notifier.Instance.notifyOnNewVideoIfNeeded(video)
|
2019-04-02 04:26:47 -05:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
logger.info('Video %s removed from blacklist.', video.uuid)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2018-08-13 09:57:13 -05:00
|
|
|
return res.type('json').status(204).end()
|
2017-10-10 03:02:18 -05:00
|
|
|
}
|