]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-comment.ts
Add "local" videos in menu
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
index c10d7c7c8c17f0ef73fb89f577e051d7f12de411..bf8da924d5555ee5d06911941100f5ee47881c7d 100644 (file)
@@ -1,6 +1,6 @@
 import * as Sequelize from 'sequelize'
 import {
-  AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
+  AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
   UpdatedAt
 } from 'sequelize-typescript'
 import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects'
@@ -104,6 +104,10 @@ enum ScopeNames {
     },
     {
       fields: [ 'videoId', 'originCommentId' ]
+    },
+    {
+      fields: [ 'url' ],
+      unique: true
     }
   ]
 })
@@ -175,10 +179,38 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
   })
   Account: AccountModel
 
-  @AfterDestroy
-  static async sendDeleteIfOwned (instance: VideoCommentModel) {
+  @BeforeDestroy
+  static async sendDeleteIfOwned (instance: VideoCommentModel, options) {
+    if (!instance.Account || !instance.Account.Actor) {
+      instance.Account = await instance.$get('Account', {
+        include: [ ActorModel ],
+        transaction: options.transaction
+      }) as AccountModel
+    }
+
+    if (!instance.Video) {
+      instance.Video = await instance.$get('Video', {
+        include: [
+          {
+            model: VideoChannelModel,
+            include: [
+              {
+                model: AccountModel,
+                include: [
+                  {
+                    model: ActorModel
+                  }
+                ]
+              }
+            ]
+          }
+        ],
+        transaction: options.transaction
+      }) as VideoModel
+    }
+
     if (instance.isOwned()) {
-      await sendDeleteVideoComment(instance, undefined)
+      await sendDeleteVideoComment(instance, options.transaction)
     }
   }
 
@@ -236,7 +268,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     const query = {
       offset: start,
       limit: count,
-      order: [ getSort(sort) ],
+      order: getSort(sort),
       where: {
         videoId,
         inReplyToCommentId: null
@@ -294,6 +326,32 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       .findAll(query)
   }
 
+  static async getStats () {
+    const totalLocalVideoComments = await VideoCommentModel.count({
+      include: [
+        {
+          model: AccountModel,
+          required: true,
+          include: [
+            {
+              model: ActorModel,
+              required: true,
+              where: {
+                serverId: null
+              }
+            }
+          ]
+        }
+      ]
+    })
+    const totalVideoComments = await VideoCommentModel.count()
+
+    return {
+      totalLocalVideoComments,
+      totalVideoComments
+    }
+  }
+
   getThreadId (): number {
     return this.originCommentId || this.id
   }