]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Support is:blacklisted as video-abuse-list filter
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 5e4b7d44c0b86f0162bbd02912a801258216276c..ccb9d64ca4ad24a583429d58f75e23a5b647596e 100644 (file)
@@ -125,6 +125,7 @@ import { VideoFile } from '@shared/models/videos/video-file.model'
 import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
 import { ModelCache } from '@server/models/model-cache'
 import { buildListQuery, BuildVideosQueryOptions, wrapForAPIResults } from './video-query-builder'
+import { buildNSFWFilter } from '@server/helpers/express-utils'
 
 export enum ScopeNames {
   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
@@ -627,9 +628,9 @@ export class VideoModel extends Model<VideoModel> {
   @HasMany(() => VideoAbuseModel, {
     foreignKey: {
       name: 'videoId',
-      allowNull: false
+      allowNull: true
     },
-    onDelete: 'cascade'
+    onDelete: 'set null'
   })
   VideoAbuses: VideoAbuseModel[]
 
@@ -797,6 +798,33 @@ export class VideoModel extends Model<VideoModel> {
     ModelCache.Instance.invalidateCache('video', instance.id)
   }
 
+  @BeforeDestroy
+  static async saveEssentialDataToAbuses (instance: VideoModel, options) {
+    const tasks: Promise<any>[] = []
+
+    logger.info('Saving video abuses details of video %s.', instance.url)
+
+    if (!Array.isArray(instance.VideoAbuses)) {
+      instance.VideoAbuses = await instance.$get('VideoAbuses')
+
+      if (instance.VideoAbuses.length === 0) return undefined
+    }
+
+    const details = instance.toFormattedDetailsJSON()
+
+    for (const abuse of instance.VideoAbuses) {
+      abuse.deletedVideo = details
+      tasks.push(abuse.save({ transaction: options.transaction }))
+    }
+
+    Promise.all(tasks)
+           .catch(err => {
+             logger.error('Some errors when saving details of video %s in its abuses before destroy hook.', instance.uuid, { err })
+           })
+
+    return undefined
+  }
+
   static listLocal (): Bluebird<MVideoWithAllFiles[]> {
     const query = {
       where: {
@@ -1301,16 +1329,25 @@ export class VideoModel extends Model<VideoModel> {
         remote: false
       }
     })
-    const totalVideos = await VideoModel.count()
 
     let totalLocalVideoViews = await VideoModel.sum('views', {
       where: {
         remote: false
       }
     })
+
     // Sequelize could return null...
     if (!totalLocalVideoViews) totalLocalVideoViews = 0
 
+    const { total: totalVideos } = await VideoModel.listForApi({
+      start: 0,
+      count: 0,
+      sort: '-publishedAt',
+      nsfw: buildNSFWFilter(),
+      includeLocalVideos: true,
+      withFiles: false
+    })
+
     return {
       totalLocalVideos,
       totalLocalVideoViews,
@@ -1419,6 +1456,8 @@ export class VideoModel extends Model<VideoModel> {
     }
 
     function getModels () {
+      if (options.count === 0) return Promise.resolve([])
+
       const { query, replacements, order } = buildListQuery(VideoModel, options)
       const queryModels = wrapForAPIResults(query, replacements, options, order)
 
@@ -1849,7 +1888,8 @@ export class VideoModel extends Model<VideoModel> {
 
   getVideoFileMetadataUrl (videoFile: MVideoFile, baseUrlHttp: string) {
     const path = '/api/v1/videos/'
-    return videoFile.metadata
+
+    return this.isOwned()
       ? baseUrlHttp + path + this.uuid + '/metadata/' + videoFile.id
       : videoFile.metadataUrl
   }