2019-07-18 08:28:37 -04:00
|
|
|
import { Transaction } from 'sequelize'
|
2021-01-04 04:50:47 -05:00
|
|
|
import { afterCommitIfTransaction } from '@server/helpers/database-utils'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
|
|
|
import {
|
|
|
|
MUser,
|
|
|
|
MVideoAccountLight,
|
|
|
|
MVideoBlacklist,
|
|
|
|
MVideoBlacklistVideo,
|
|
|
|
MVideoFullLight,
|
|
|
|
MVideoWithBlacklistLight
|
2020-06-18 04:45:25 -04:00
|
|
|
} from '@server/types/models'
|
2022-05-03 05:38:07 -04:00
|
|
|
import { LiveVideoError, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
|
2021-03-05 07:26:02 -05:00
|
|
|
import { logger, loggerTagsFactory } from '../helpers/logger'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../initializers/config'
|
2019-04-02 05:26:47 -04:00
|
|
|
import { VideoBlacklistModel } from '../models/video/video-blacklist'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { sendDeleteVideo } from './activitypub/send'
|
|
|
|
import { federateVideoIfNeeded } from './activitypub/videos'
|
2021-06-16 09:14:41 -04:00
|
|
|
import { LiveManager } from './live/live-manager'
|
2019-07-23 06:04:15 -04:00
|
|
|
import { Notifier } from './notifier'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { Hooks } from './plugins/hooks'
|
2019-04-02 05:26:47 -04:00
|
|
|
|
2021-03-05 07:26:02 -05:00
|
|
|
const lTags = loggerTagsFactory('blacklist')
|
|
|
|
|
2019-07-22 05:14:58 -04:00
|
|
|
async function autoBlacklistVideoIfNeeded (parameters: {
|
2020-01-31 10:56:52 -05:00
|
|
|
video: MVideoWithBlacklistLight
|
|
|
|
user?: MUser
|
|
|
|
isRemote: boolean
|
|
|
|
isNew: boolean
|
|
|
|
notify?: boolean
|
2019-07-22 05:14:58 -04:00
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
2019-07-23 06:04:15 -04:00
|
|
|
const { video, user, isRemote, isNew, notify = true, transaction } = parameters
|
2020-01-31 10:56:52 -05:00
|
|
|
const doAutoBlacklist = await Hooks.wrapFun(
|
2019-07-19 11:30:41 -04:00
|
|
|
autoBlacklistNeeded,
|
2019-07-22 05:14:58 -04:00
|
|
|
{ video, user, isRemote, isNew },
|
2019-07-18 08:28:37 -04:00
|
|
|
'filter:video.auto-blacklist.result'
|
|
|
|
)
|
2019-04-02 05:26:47 -04:00
|
|
|
|
2019-07-18 08:28:37 -04:00
|
|
|
if (!doAutoBlacklist) return false
|
2019-04-02 05:26:47 -04:00
|
|
|
|
|
|
|
const videoBlacklistToCreate = {
|
|
|
|
videoId: video.id,
|
|
|
|
unfederated: true,
|
|
|
|
reason: 'Auto-blacklisted. Moderator review required.',
|
2020-06-09 10:07:10 -04:00
|
|
|
type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
|
2019-04-02 05:26:47 -04:00
|
|
|
}
|
2019-08-30 10:50:12 -04:00
|
|
|
const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
|
2019-07-22 05:14:58 -04:00
|
|
|
where: {
|
|
|
|
videoId: video.id
|
|
|
|
},
|
|
|
|
defaults: videoBlacklistToCreate,
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
video.VideoBlacklist = videoBlacklist
|
2019-04-15 04:49:46 -04:00
|
|
|
|
2019-08-30 10:50:12 -04:00
|
|
|
videoBlacklist.Video = video
|
|
|
|
|
2021-01-04 04:50:47 -05:00
|
|
|
if (notify) {
|
|
|
|
afterCommitIfTransaction(transaction, () => {
|
|
|
|
Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
|
|
|
|
})
|
|
|
|
}
|
2019-07-23 06:04:15 -04:00
|
|
|
|
2021-03-05 07:26:02 -05:00
|
|
|
logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
|
2019-04-02 05:26:47 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:58:24 -04:00
|
|
|
async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
|
|
|
|
const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
unfederated: options.unfederate === true,
|
|
|
|
reason: options.reason,
|
2020-06-09 10:07:10 -04:00
|
|
|
type: VideoBlacklistType.MANUAL
|
2022-04-21 03:06:52 -04:00
|
|
|
})
|
2020-05-07 08:58:24 -04:00
|
|
|
blacklist.Video = videoInstance
|
|
|
|
|
|
|
|
if (options.unfederate === true) {
|
|
|
|
await sendDeleteVideo(videoInstance, undefined)
|
|
|
|
}
|
|
|
|
|
2020-09-25 04:04:21 -04:00
|
|
|
if (videoInstance.isLive) {
|
2023-04-21 08:55:10 -04:00
|
|
|
LiveManager.Instance.stopSessionOf(videoInstance.uuid, LiveVideoError.BLACKLISTED)
|
2020-09-25 04:04:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:58:24 -04:00
|
|
|
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)
|
|
|
|
|
2020-06-09 10:07:10 -04:00
|
|
|
if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
|
2020-05-07 08:58:24 -04: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 10:56:52 -05:00
|
|
|
function autoBlacklistNeeded (parameters: {
|
|
|
|
video: MVideoWithBlacklistLight
|
|
|
|
isRemote: boolean
|
|
|
|
isNew: boolean
|
2019-08-15 05:53:26 -04:00
|
|
|
user?: MUser
|
2019-07-22 05:14:58 -04:00
|
|
|
}) {
|
|
|
|
const { user, video, isRemote, isNew } = parameters
|
2019-07-18 08:28:37 -04:00
|
|
|
|
2019-07-22 05:14:58 -04:00
|
|
|
// Already blacklisted
|
|
|
|
if (video.VideoBlacklist) return false
|
2019-07-18 08:28:37 -04:00
|
|
|
if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
|
2019-07-23 04:44:48 -04:00
|
|
|
if (isRemote || isNew === false) return false
|
2019-07-18 08:28:37 -04:00
|
|
|
|
2020-06-09 10:07:10 -04:00
|
|
|
if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
|
2019-07-18 08:28:37 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|