2019-07-18 07:28:37 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../initializers/config'
|
2019-04-15 03:49:46 -05:00
|
|
|
import { UserRight, VideoBlacklistType } from '../../shared/models'
|
2019-04-02 04:26:47 -05:00
|
|
|
import { VideoBlacklistModel } from '../models/video/video-blacklist'
|
|
|
|
import { UserModel } from '../models/account/user'
|
|
|
|
import { VideoModel } from '../models/video/video'
|
|
|
|
import { logger } from '../helpers/logger'
|
2019-04-15 03:49:46 -05:00
|
|
|
import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
|
2019-07-18 07:28:37 -05:00
|
|
|
import { Hooks } from './plugins/hooks'
|
2019-04-02 04:26:47 -05:00
|
|
|
|
2019-07-22 04:14:58 -05:00
|
|
|
async function autoBlacklistVideoIfNeeded (parameters: {
|
|
|
|
video: VideoModel,
|
|
|
|
user?: UserModel,
|
|
|
|
isRemote: boolean,
|
|
|
|
isNew: boolean,
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
|
|
|
const { video, user, isRemote, isNew, transaction } = parameters
|
2019-07-19 10:30:41 -05:00
|
|
|
const doAutoBlacklist = await Hooks.wrapPromiseFun(
|
|
|
|
autoBlacklistNeeded,
|
2019-07-22 04:14:58 -05:00
|
|
|
{ video, user, isRemote, isNew },
|
2019-07-18 07:28:37 -05:00
|
|
|
'filter:video.auto-blacklist.result'
|
|
|
|
)
|
2019-04-02 04:26:47 -05:00
|
|
|
|
2019-07-18 07:28:37 -05:00
|
|
|
if (!doAutoBlacklist) return false
|
2019-04-02 04:26:47 -05:00
|
|
|
|
|
|
|
const videoBlacklistToCreate = {
|
|
|
|
videoId: video.id,
|
|
|
|
unfederated: true,
|
|
|
|
reason: 'Auto-blacklisted. Moderator review required.',
|
|
|
|
type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
|
|
|
|
}
|
2019-07-22 04:14:58 -05:00
|
|
|
const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({
|
|
|
|
where: {
|
|
|
|
videoId: video.id
|
|
|
|
},
|
|
|
|
defaults: videoBlacklistToCreate,
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
video.VideoBlacklist = videoBlacklist
|
2019-04-15 03:49:46 -05:00
|
|
|
|
2019-04-02 04:26:47 -05:00
|
|
|
logger.info('Video %s auto-blacklisted.', video.uuid)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-22 04:14:58 -05:00
|
|
|
async function autoBlacklistNeeded (parameters: {
|
|
|
|
video: VideoModel,
|
|
|
|
isRemote: boolean,
|
|
|
|
isNew: boolean,
|
|
|
|
user?: UserModel
|
|
|
|
}) {
|
|
|
|
const { user, video, isRemote, isNew } = parameters
|
2019-07-18 07:28:37 -05:00
|
|
|
|
2019-07-22 04:14:58 -05:00
|
|
|
// Already blacklisted
|
|
|
|
if (video.VideoBlacklist) return false
|
2019-07-18 07:28:37 -05:00
|
|
|
if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
|
2019-07-23 03:44:48 -05:00
|
|
|
if (isRemote || isNew === false) return false
|
2019-07-18 07:28:37 -05:00
|
|
|
|
|
|
|
if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-02 04:26:47 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
autoBlacklistVideoIfNeeded
|
|
|
|
}
|