Correctly notify on auto blacklist

pull/1987/head
Chocobozzz 2019-07-23 12:04:15 +02:00 committed by Chocobozzz
parent d8e9a42c4b
commit 5b77537ce5
15 changed files with 61 additions and 55 deletions

View File

@ -32,6 +32,7 @@ async function run () {
for (const video of localVideos) { for (const video of localVideos) {
currentVideoId = video.id currentVideoId = video.id
for (const file of video.VideoFiles) { for (const file of video.VideoFiles) {
currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
@ -44,22 +45,29 @@ async function run () {
const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
const isMaxBitrateExceeded = videoBitrate > maxBitrate const isMaxBitrateExceeded = videoBitrate > maxBitrate
if (isMaxBitrateExceeded) { if (isMaxBitrateExceeded) {
console.log('Optimizing video file %s with bitrate %s kbps (max: %s kbps)', console.log(
basename(currentFile), videoBitrate / 1000, maxBitrate / 1000) 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
)
const backupFile = `${currentFile}_backup` const backupFile = `${currentFile}_backup`
await copy(currentFile, backupFile) await copy(currentFile, backupFile)
await optimizeVideofile(video, file) await optimizeVideofile(video, file)
const originalDuration = await getDurationFromVideoFile(backupFile) const originalDuration = await getDurationFromVideoFile(backupFile)
const newDuration = await getDurationFromVideoFile(currentFile) const newDuration = await getDurationFromVideoFile(currentFile)
if (originalDuration === newDuration) { if (originalDuration === newDuration) {
console.log('Finished optimizing %s', basename(currentFile)) console.log('Finished optimizing %s', basename(currentFile))
await remove(backupFile) await remove(backupFile)
} else { return
console.log('Failed to optimize %s, restoring original', basename(currentFile))
move(backupFile, currentFile, { overwrite: true })
await video.createTorrentAndSetInfoHash(file)
await file.save()
} }
console.log('Failed to optimize %s, restoring original', basename(currentFile))
await move(backupFile, currentFile, { overwrite: true })
await video.createTorrentAndSetInfoHash(file)
await file.save()
} }
} }
} }

View File

@ -115,6 +115,7 @@ async function removeVideoFromBlacklistController (req: express.Request, res: ex
const videoBlacklistType = videoBlacklist.type const videoBlacklistType = videoBlacklist.type
await videoBlacklist.destroy({ transaction: t }) await videoBlacklist.destroy({ transaction: t })
video.VideoBlacklist = undefined
// Re federate the video // Re federate the video
if (unfederated === true) { if (unfederated === true) {
@ -131,7 +132,7 @@ async function removeVideoFromBlacklistController (req: express.Request, res: ex
// Delete on object so new video notifications will send // Delete on object so new video notifications will send
delete video.VideoBlacklist delete video.VideoBlacklist
Notifier.Instance.notifyOnNewVideo(video) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
} }
logger.info('Video %s removed from blacklist.', res.locals.video.uuid) logger.info('Video %s removed from blacklist.', res.locals.video.uuid)

View File

@ -245,7 +245,14 @@ function insertIntoDB (parameters: {
if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
await autoBlacklistVideoIfNeeded({ video, user, isRemote: false, isNew: true, transaction: t }) await autoBlacklistVideoIfNeeded({
video,
user,
notify: false,
isRemote: false,
isNew: true,
transaction: t
})
// Set tags to the video // Set tags to the video
if (tags) { if (tags) {

View File

@ -235,7 +235,7 @@ async function addVideo (req: express.Request, res: express.Response) {
// Create the torrent file // Create the torrent file
await video.createTorrentAndSetInfoHash(videoFile) await video.createTorrentAndSetInfoHash(videoFile)
const { videoCreated, videoWasAutoBlacklisted } = await sequelizeTypescript.transaction(async t => { const { videoCreated } = await sequelizeTypescript.transaction(async t => {
const sequelizeOptions = { transaction: t } const sequelizeOptions = { transaction: t }
const videoCreated = await video.save(sequelizeOptions) const videoCreated = await video.save(sequelizeOptions)
@ -268,23 +268,22 @@ async function addVideo (req: express.Request, res: express.Response) {
}, { transaction: t }) }, { transaction: t })
} }
const videoWasAutoBlacklisted = await autoBlacklistVideoIfNeeded({ await autoBlacklistVideoIfNeeded({
video, video,
user: res.locals.oauth.token.User, user: res.locals.oauth.token.User,
isRemote: false, isRemote: false,
isNew: true, isNew: true,
transaction: t transaction: t
}) })
if (!videoWasAutoBlacklisted) await federateVideoIfNeeded(video, true, t) await federateVideoIfNeeded(video, true, t)
auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON()))
logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
return { videoCreated, videoWasAutoBlacklisted } return { videoCreated }
}) })
if (videoWasAutoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated) Notifier.Instance.notifyOnNewVideoIfNeeded(videoCreated)
else Notifier.Instance.notifyOnNewVideo(videoCreated)
if (video.state === VideoState.TO_TRANSCODE) { if (video.state === VideoState.TO_TRANSCODE) {
// Put uuid because we don't have id auto incremented for now // Put uuid because we don't have id auto incremented for now
@ -413,11 +412,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
}) })
const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE
await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
// Don't send update if the video was unfederated
if (!videoInstanceUpdated.VideoBlacklist || videoInstanceUpdated.VideoBlacklist.unfederated === false) {
await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
}
auditLogger.update( auditLogger.update(
getAuditIdFromRes(res), getAuditIdFromRes(res),
@ -430,7 +425,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
}) })
if (wasUnlistedVideo || wasPrivateVideo) { if (wasUnlistedVideo || wasPrivateVideo) {
Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
} }
Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated }) Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated })

View File

@ -1,9 +1,7 @@
import * as express from 'express'
import 'express-validator' import 'express-validator'
import 'multer' import 'multer'
import * as validator from 'validator' import * as validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
import { VideoChannelModel } from '../../models/video/video-channel'
import { exists } from './misc' import { exists } from './misc'
const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
@ -25,18 +23,5 @@ function isVideoChannelSupportValid (value: string) {
export { export {
isVideoChannelDescriptionValid, isVideoChannelDescriptionValid,
isVideoChannelNameValid, isVideoChannelNameValid,
isVideoChannelSupportValid, isVideoChannelSupportValid
}
function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
if (!videoChannel) {
res.status(404)
.json({ error: 'Video channel not found' })
.end()
return false
}
res.locals.videoChannel = videoChannel
return true
} }

View File

@ -63,5 +63,5 @@ async function processVideoShare (actorAnnouncer: ActorModel, activity: Activity
return undefined return undefined
}) })
if (videoCreated) Notifier.Instance.notifyOnNewVideo(video) if (videoCreated) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
} }

View File

@ -48,9 +48,9 @@ export {
async function processCreateVideo (activity: ActivityCreate) { async function processCreateVideo (activity: ActivityCreate) {
const videoToCreateData = activity.object as VideoTorrentObject const videoToCreateData = activity.object as VideoTorrentObject
const { video, created, autoBlacklisted } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
if (created && !autoBlacklisted) Notifier.Instance.notifyOnNewVideo(video) if (created) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
return video return video
} }

View File

@ -58,8 +58,12 @@ import { Hooks } from '../plugins/hooks'
import { autoBlacklistVideoIfNeeded } from '../video-blacklist' import { autoBlacklistVideoIfNeeded } from '../video-blacklist'
async function federateVideoIfNeeded (video: VideoModel, isNewVideo: boolean, transaction?: sequelize.Transaction) { async function federateVideoIfNeeded (video: VideoModel, isNewVideo: boolean, transaction?: sequelize.Transaction) {
// If the video is not private and is published, we federate it if (
if (video.privacy !== VideoPrivacy.PRIVATE && video.state === VideoState.PUBLISHED) { // Check this is not a blacklisted video, or unfederated blacklisted video
(video.isBlacklisted() === false || (isNewVideo === false && video.VideoBlacklist.unfederated === false)) &&
// Check the video is public/unlisted and published
video.privacy !== VideoPrivacy.PRIVATE && video.state === VideoState.PUBLISHED
) {
// Fetch more attributes that we will need to serialize in AP object // Fetch more attributes that we will need to serialize in AP object
if (isArray(video.VideoCaptions) === false) { if (isArray(video.VideoCaptions) === false) {
video.VideoCaptions = await video.$get('VideoCaptions', { video.VideoCaptions = await video.$get('VideoCaptions', {
@ -354,7 +358,7 @@ async function updateVideoFromAP (options: {
} }
}) })
const autoBlacklisted = await autoBlacklistVideoIfNeeded({ await autoBlacklistVideoIfNeeded({
video, video,
user: undefined, user: undefined,
isRemote: true, isRemote: true,
@ -362,8 +366,7 @@ async function updateVideoFromAP (options: {
transaction: undefined transaction: undefined
}) })
if (autoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(video) if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(video) // Notify our users?
else if (!wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideo(video) // Notify our users?
logger.info('Remote video with uuid %s updated', videoObject.uuid) logger.info('Remote video with uuid %s updated', videoObject.uuid)
} catch (err) { } catch (err) {

View File

@ -199,10 +199,10 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true) Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true)
if (videoImportUpdated.Video.VideoBlacklist) { if (videoImportUpdated.Video.isBlacklisted()) {
Notifier.Instance.notifyOnVideoAutoBlacklist(videoImportUpdated.Video) Notifier.Instance.notifyOnVideoAutoBlacklist(videoImportUpdated.Video)
} else { } else {
Notifier.Instance.notifyOnNewVideo(videoImportUpdated.Video) Notifier.Instance.notifyOnNewVideoIfNeeded(videoImportUpdated.Video)
} }
// Create transcoding jobs? // Create transcoding jobs?

View File

@ -112,7 +112,7 @@ async function publishNewResolutionIfNeeded (video: VideoModel, payload?: NewRes
}) })
if (videoPublished) { if (videoPublished) {
Notifier.Instance.notifyOnNewVideo(videoDatabase) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
} }
@ -172,7 +172,7 @@ async function onVideoFileOptimizerSuccess (videoArg: VideoModel, payload: Optim
return { videoDatabase, videoPublished } return { videoDatabase, videoPublished }
}) })
if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase) if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution }) const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution })

View File

@ -22,9 +22,9 @@ class Notifier {
private constructor () {} private constructor () {}
notifyOnNewVideo (video: VideoModel): void { notifyOnNewVideoIfNeeded (video: VideoModel): void {
// Only notify on public and published videos which are not blacklisted // Only notify on public and published videos which are not blacklisted
if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.VideoBlacklist) return if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
this.notifySubscribersOfNewVideo(video) this.notifySubscribersOfNewVideo(video)
.catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))

View File

@ -57,7 +57,7 @@ export class UpdateVideosScheduler extends AbstractScheduler {
}) })
for (const v of publishedVideos) { for (const v of publishedVideos) {
Notifier.Instance.notifyOnNewVideo(v) Notifier.Instance.notifyOnNewVideoIfNeeded(v)
Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v) Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
} }
} }

View File

@ -7,15 +7,17 @@ import { VideoModel } from '../models/video/video'
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
import { UserAdminFlag } from '../../shared/models/users/user-flag.model' import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
import { Hooks } from './plugins/hooks' import { Hooks } from './plugins/hooks'
import { Notifier } from './notifier'
async function autoBlacklistVideoIfNeeded (parameters: { async function autoBlacklistVideoIfNeeded (parameters: {
video: VideoModel, video: VideoModel,
user?: UserModel, user?: UserModel,
isRemote: boolean, isRemote: boolean,
isNew: boolean, isNew: boolean,
notify?: boolean,
transaction?: Transaction transaction?: Transaction
}) { }) {
const { video, user, isRemote, isNew, transaction } = parameters const { video, user, isRemote, isNew, notify = true, transaction } = parameters
const doAutoBlacklist = await Hooks.wrapPromiseFun( const doAutoBlacklist = await Hooks.wrapPromiseFun(
autoBlacklistNeeded, autoBlacklistNeeded,
{ video, user, isRemote, isNew }, { video, user, isRemote, isNew },
@ -37,9 +39,10 @@ async function autoBlacklistVideoIfNeeded (parameters: {
defaults: videoBlacklistToCreate, defaults: videoBlacklistToCreate,
transaction transaction
}) })
video.VideoBlacklist = videoBlacklist video.VideoBlacklist = videoBlacklist
if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(video)
logger.info('Video %s auto-blacklisted.', video.uuid) logger.info('Video %s auto-blacklisted.', video.uuid)
return true return true

View File

@ -1715,6 +1715,10 @@ export class VideoModel extends Model<VideoModel> {
return VIDEO_STATES[ id ] || 'Unknown' return VIDEO_STATES[ id ] || 'Unknown'
} }
isBlacklisted () {
return !!this.VideoBlacklist
}
getOriginalFile () { getOriginalFile () {
if (Array.isArray(this.VideoFiles) === false) return undefined if (Array.isArray(this.VideoFiles) === false) return undefined

View File

@ -75,7 +75,7 @@ describe('Test optimize old videos', function () {
}) })
it('Should run optimize script', async function () { it('Should run optimize script', async function () {
this.timeout(120000) this.timeout(200000)
const env = getEnvCli(servers[0]) const env = getEnvCli(servers[0])
await execCLI(`${env} npm run optimize-old-videos`) await execCLI(`${env} npm run optimize-old-videos`)