X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fvideo-blacklist.ts;h=bd60c6201c93d1cde2ff762b717103f5ef2dc714;hb=35b30b643cf9870b0934f34253ffb23cf6a264b0;hp=1dd45b76d9d1fe742c52d4beef611aa991f357e6;hpb=8d5e65349deebd499c0be10fe02d535a77d58ddb;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/video-blacklist.ts b/server/lib/video-blacklist.ts index 1dd45b76d..bd60c6201 100644 --- a/server/lib/video-blacklist.ts +++ b/server/lib/video-blacklist.ts @@ -1,23 +1,33 @@ import { Transaction } from 'sequelize' +import { sequelizeTypescript } from '@server/initializers/database' +import { + MUser, + MVideoAccountLight, + MVideoBlacklist, + MVideoBlacklistVideo, + MVideoFullLight, + MVideoWithBlacklistLight +} from '@server/typings/models' +import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models' +import { UserAdminFlag } from '../../shared/models/users/user-flag.model' +import { logger } from '../helpers/logger' import { CONFIG } from '../initializers/config' -import { UserRight, VideoBlacklistType } from '../../shared/models' import { VideoBlacklistModel } from '../models/video/video-blacklist' -import { logger } from '../helpers/logger' -import { UserAdminFlag } from '../../shared/models/users/user-flag.model' -import { Hooks } from './plugins/hooks' +import { sendDeleteVideo } from './activitypub/send' +import { federateVideoIfNeeded } from './activitypub/videos' import { Notifier } from './notifier' -import { MUser, MVideoBlacklistVideo, MVideoWithBlacklistLight } from '@server/typings/models' +import { Hooks } from './plugins/hooks' async function autoBlacklistVideoIfNeeded (parameters: { - video: MVideoWithBlacklistLight, - user?: MUser, - isRemote: boolean, - isNew: boolean, - notify?: boolean, + video: MVideoWithBlacklistLight + user?: MUser + isRemote: boolean + isNew: boolean + notify?: boolean transaction?: Transaction }) { const { video, user, isRemote, isNew, notify = true, transaction } = parameters - const doAutoBlacklist = await Hooks.wrapPromiseFun( + const doAutoBlacklist = await Hooks.wrapFun( autoBlacklistNeeded, { video, user, isRemote, isNew }, 'filter:video.auto-blacklist.result' @@ -49,10 +59,64 @@ async function autoBlacklistVideoIfNeeded (parameters: { return true } -async function autoBlacklistNeeded (parameters: { - video: MVideoWithBlacklistLight, - isRemote: boolean, - isNew: boolean, +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 + } + ) + 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) { + Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video) + + // Delete on object so new video notifications will send + delete video.VideoBlacklist + Notifier.Instance.notifyOnNewVideoIfNeeded(video) + } +} + +// --------------------------------------------------------------------------- + +export { + autoBlacklistVideoIfNeeded, + blacklistVideo, + unblacklistVideo +} + +// --------------------------------------------------------------------------- + +function autoBlacklistNeeded (parameters: { + video: MVideoWithBlacklistLight + isRemote: boolean + isNew: boolean user?: MUser }) { const { user, video, isRemote, isNew } = parameters @@ -66,9 +130,3 @@ async function autoBlacklistNeeded (parameters: { return true } - -// --------------------------------------------------------------------------- - -export { - autoBlacklistVideoIfNeeded -}