PeerTube/server/core/lib/video-blacklist.ts

145 lines
4.5 KiB
TypeScript
Raw Normal View History

2019-07-18 07:28:37 -05:00
import { Transaction } from 'sequelize'
import { LiveVideoError, UserAdminFlag, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '@peertube/peertube-models'
import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
2020-05-07 07:58:24 -05:00
import {
MUser,
MVideoAccountLight,
MVideoBlacklist,
MVideoBlacklistVideo,
MVideoFullLight,
MVideoWithBlacklistLight
} from '@server/types/models/index.js'
import { logger, loggerTagsFactory } from '../helpers/logger.js'
import { CONFIG } from '../initializers/config.js'
import { VideoBlacklistModel } from '../models/video/video-blacklist.js'
import { sendDeleteVideo } from './activitypub/send/index.js'
import { federateVideoIfNeeded } from './activitypub/videos/index.js'
import { LiveManager } from './live/live-manager.js'
import { Notifier } from './notifier/index.js'
import { Hooks } from './plugins/hooks.js'
const lTags = loggerTagsFactory('blacklist')
2019-07-22 04:14:58 -05:00
async function autoBlacklistVideoIfNeeded (parameters: {
2020-01-31 09:56:52 -06:00
video: MVideoWithBlacklistLight
user?: MUser
isRemote: boolean
isNew: boolean
2023-07-19 09:02:49 -05:00
isNewFile: boolean
2020-01-31 09:56:52 -06:00
notify?: boolean
2019-07-22 04:14:58 -05:00
transaction?: Transaction
}) {
2023-07-19 09:02:49 -05:00
const { video, user, isRemote, isNew, isNewFile, notify = true, transaction } = parameters
2020-01-31 09:56:52 -06:00
const doAutoBlacklist = await Hooks.wrapFun(
2019-07-19 10:30:41 -05:00
autoBlacklistNeeded,
2023-07-19 09:02:49 -05:00
{ video, user, isRemote, isNew, isNewFile },
2019-07-18 07:28:37 -05:00
'filter:video.auto-blacklist.result'
)
2019-07-18 07:28:37 -05: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 04:14:58 -05:00
where: {
videoId: video.id
},
defaults: videoBlacklistToCreate,
transaction
})
video.VideoBlacklist = videoBlacklist
2019-04-15 03:49:46 -05:00
videoBlacklist.Video = video
2021-01-04 03:50:47 -06:00
if (notify) {
afterCommitIfTransaction(transaction, () => {
Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
})
}
2019-07-23 05:04:15 -05:00
logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
return true
}
2020-05-07 07:58:24 -05: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 07:58:24 -05:00
blacklist.Video = videoInstance
if (options.unfederate === true) {
await sendDeleteVideo(videoInstance, undefined)
}
if (videoInstance.isLive) {
LiveManager.Instance.stopSessionOf({ videoUUID: videoInstance.uuid, error: LiveVideoError.BLACKLISTED })
}
2020-05-07 07:58:24 -05: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)
if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
2020-05-07 07:58:24 -05: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 09:56:52 -06:00
function autoBlacklistNeeded (parameters: {
video: MVideoWithBlacklistLight
isRemote: boolean
isNew: boolean
2023-07-19 09:02:49 -05:00
isNewFile: boolean
2019-08-15 04:53:26 -05:00
user?: MUser
2019-07-22 04:14:58 -05:00
}) {
2023-07-19 09:02:49 -05:00
const { user, video, isRemote, isNew, isNewFile } = 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
2023-07-19 09:02:49 -05:00
if (isRemote || (isNew === false && isNewFile === false)) return false
2019-07-18 07:28:37 -05:00
if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
2019-07-18 07:28:37 -05:00
return true
}