From 6691c52280363fc42f7865230ebb3741f02fff23 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 22 Jul 2019 11:14:58 +0200 Subject: Add filter hooks tests --- server/lib/activitypub/process/process-create.ts | 4 +-- server/lib/activitypub/videos.ts | 28 +++++++++++++++----- server/lib/video-blacklist.ts | 33 ++++++++++++++++++++---- 3 files changed, 51 insertions(+), 14 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index daf846513..b9f584aa5 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -48,9 +48,9 @@ export { async function processCreateVideo (activity: ActivityCreate) { const videoToCreateData = activity.object as VideoTorrentObject - const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) + const { video, created, autoBlacklisted } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) - if (created) Notifier.Instance.notifyOnNewVideo(video) + if (created && !autoBlacklisted) Notifier.Instance.notifyOnNewVideo(video) return video } diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index dade6b55f..67b433165 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -224,11 +224,11 @@ async function getOrCreateVideoAndAccountAndChannel (options: { if (!fetchedVideo) throw new Error('Cannot fetch remote video with url: ' + videoUrl) const channelActor = await getOrCreateVideoChannelFromVideoObject(fetchedVideo) - const video = await retryTransactionWrapper(createVideo, fetchedVideo, channelActor, syncParam.thumbnail) + const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(createVideo, fetchedVideo, channelActor, syncParam.thumbnail) - await syncVideoExternalAttributes(video, fetchedVideo, syncParam) + await syncVideoExternalAttributes(videoCreated, fetchedVideo, syncParam) - return { video, created: true } + return { video: videoCreated, created: true, autoBlacklisted } } async function updateVideoFromAP (options: { @@ -354,7 +354,13 @@ async function updateVideoFromAP (options: { } }) - const autoBlacklisted = await autoBlacklistVideoIfNeeded(video, undefined, undefined) + const autoBlacklisted = await autoBlacklistVideoIfNeeded({ + video, + user: undefined, + isRemote: true, + isNew: false, + transaction: undefined + }) if (autoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(video) else if (!wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideo(video) // Notify our users? @@ -467,7 +473,7 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor thumbnailModel = await promiseThumbnail } - const videoCreated: VideoModel = await sequelizeTypescript.transaction(async t => { + const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } const videoCreated = await video.save(sequelizeOptions) @@ -506,9 +512,17 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor }) await Promise.all(videoCaptionsPromises) + const autoBlacklisted = await autoBlacklistVideoIfNeeded({ + video, + user: undefined, + isRemote: true, + isNew: true, + transaction: t + }) + logger.info('Remote video with uuid %s inserted.', videoObject.uuid) - return videoCreated + return { autoBlacklisted, videoCreated } }) if (waitThumbnail === false) { @@ -519,7 +533,7 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor }) } - return videoCreated + return { autoBlacklisted, videoCreated } } async function videoActivityObjectToDBAttributes ( diff --git a/server/lib/video-blacklist.ts b/server/lib/video-blacklist.ts index 9bc996f5a..9749ce2f6 100644 --- a/server/lib/video-blacklist.ts +++ b/server/lib/video-blacklist.ts @@ -8,10 +8,17 @@ import { logger } from '../helpers/logger' import { UserAdminFlag } from '../../shared/models/users/user-flag.model' import { Hooks } from './plugins/hooks' -async function autoBlacklistVideoIfNeeded (video: VideoModel, user?: UserModel, transaction?: Transaction) { +async function autoBlacklistVideoIfNeeded (parameters: { + video: VideoModel, + user?: UserModel, + isRemote: boolean, + isNew: boolean, + transaction?: Transaction +}) { + const { video, user, isRemote, isNew, transaction } = parameters const doAutoBlacklist = await Hooks.wrapPromiseFun( autoBlacklistNeeded, - { video, user }, + { video, user, isRemote, isNew }, 'filter:video.auto-blacklist.result' ) @@ -23,17 +30,33 @@ async function autoBlacklistVideoIfNeeded (video: VideoModel, user?: UserModel, reason: 'Auto-blacklisted. Moderator review required.', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED } - await VideoBlacklistModel.create(videoBlacklistToCreate, { transaction }) + const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({ + where: { + videoId: video.id + }, + defaults: videoBlacklistToCreate, + transaction + }) + + video.VideoBlacklist = videoBlacklist logger.info('Video %s auto-blacklisted.', video.uuid) return true } -async function autoBlacklistNeeded (parameters: { video: VideoModel, user?: UserModel }) { - const { user } = parameters +async function autoBlacklistNeeded (parameters: { + video: VideoModel, + isRemote: boolean, + isNew: boolean, + user?: UserModel +}) { + const { user, video, isRemote, isNew } = parameters + // Already blacklisted + if (video.VideoBlacklist) return false if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false + if (isRemote || isNew) return false if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false -- cgit v1.2.3