PeerTube/server/lib/video-blacklist.ts

133 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-07-18 14:28:37 +02:00
import { Transaction } from 'sequelize'
2020-05-07 14:58:24 +02:00
import { sequelizeTypescript } from '@server/initializers/database'
import {
MUser,
MVideoAccountLight,
MVideoBlacklist,
MVideoBlacklistVideo,
MVideoFullLight,
MVideoWithBlacklistLight
} from '@server/typings/models'
import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
2020-05-07 14:58:24 +02:00
import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
import { logger } from '../helpers/logger'
2019-04-11 11:33:44 +02:00
import { CONFIG } from '../initializers/config'
import { VideoBlacklistModel } from '../models/video/video-blacklist'
2020-05-07 14:58:24 +02:00
import { sendDeleteVideo } from './activitypub/send'
import { federateVideoIfNeeded } from './activitypub/videos'
2019-07-23 12:04:15 +02:00
import { Notifier } from './notifier'
2020-05-07 14:58:24 +02:00
import { Hooks } from './plugins/hooks'
2019-07-22 11:14:58 +02:00
async function autoBlacklistVideoIfNeeded (parameters: {
2020-01-31 16:56:52 +01:00
video: MVideoWithBlacklistLight
user?: MUser
isRemote: boolean
isNew: boolean
notify?: boolean
2019-07-22 11:14:58 +02:00
transaction?: Transaction
}) {
2019-07-23 12:04:15 +02:00
const { video, user, isRemote, isNew, notify = true, transaction } = parameters
2020-01-31 16:56:52 +01:00
const doAutoBlacklist = await Hooks.wrapFun(
2019-07-19 17:30:41 +02:00
autoBlacklistNeeded,
2019-07-22 11:14:58 +02:00
{ video, user, isRemote, isNew },
2019-07-18 14:28:37 +02:00
'filter:video.auto-blacklist.result'
)
2019-07-18 14:28:37 +02:00
if (!doAutoBlacklist) return false
const videoBlacklistToCreate = {
videoId: video.id,
unfederated: true,
reason: 'Auto-blacklisted. Moderator review required.',
type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
}
const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
2019-07-22 11:14:58 +02:00
where: {
videoId: video.id
},
defaults: videoBlacklistToCreate,
transaction
})
video.VideoBlacklist = videoBlacklist
2019-04-15 10:49:46 +02:00
videoBlacklist.Video = video
if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
2019-07-23 12:04:15 +02:00
logger.info('Video %s auto-blacklisted.', video.uuid)
return true
}
2020-05-07 14:58:24 +02:00
async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
videoId: videoInstance.id,
unfederated: options.unfederate === true,
reason: options.reason,
type: VideoBlacklistType.MANUAL
2020-05-07 14:58:24 +02:00
}
)
blacklist.Video = videoInstance
if (options.unfederate === true) {
await sendDeleteVideo(videoInstance, undefined)
}
Notifier.Instance.notifyOnVideoBlacklist(blacklist)
}
async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
const unfederated = videoBlacklist.unfederated
const videoBlacklistType = videoBlacklist.type
await videoBlacklist.destroy({ transaction: t })
video.VideoBlacklist = undefined
// Re federate the video
if (unfederated === true) {
await federateVideoIfNeeded(video, true, t)
}
return videoBlacklistType
})
Notifier.Instance.notifyOnVideoUnblacklist(video)
if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
2020-05-07 14:58:24 +02:00
Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
// Delete on object so new video notifications will send
delete video.VideoBlacklist
Notifier.Instance.notifyOnNewVideoIfNeeded(video)
}
}
// ---------------------------------------------------------------------------
export {
autoBlacklistVideoIfNeeded,
blacklistVideo,
unblacklistVideo
}
// ---------------------------------------------------------------------------
2020-01-31 16:56:52 +01:00
function autoBlacklistNeeded (parameters: {
video: MVideoWithBlacklistLight
isRemote: boolean
isNew: boolean
2019-08-15 11:53:26 +02:00
user?: MUser
2019-07-22 11:14:58 +02:00
}) {
const { user, video, isRemote, isNew } = parameters
2019-07-18 14:28:37 +02:00
2019-07-22 11:14:58 +02:00
// Already blacklisted
if (video.VideoBlacklist) return false
2019-07-18 14:28:37 +02:00
if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
2019-07-23 10:44:48 +02:00
if (isRemote || isNew === false) return false
2019-07-18 14:28:37 +02:00
if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
2019-07-18 14:28:37 +02:00
return true
}