]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-comment.ts
Update video channel routes
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
index 47e3211a3f35304f87a09d04e000d9a7fb79a0e5..5386a10aa4892a8fa9f9b58f6150da30c1c0b31d 100644 (file)
@@ -307,15 +307,15 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     const query = {
       order: [ [ 'createdAt', order ] ],
       where: {
-        [ Sequelize.Op.or ]: [
-          { id: comment.getThreadId() },
-          { originCommentId: comment.getThreadId() }
-        ],
         id: {
+          [ Sequelize.Op.in ]: Sequelize.literal('(' +
+            'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
+            'SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ' + comment.id + ' UNION ' +
+            'SELECT p.id, p."inReplyToCommentId" from "videoComment" p ' +
+            'INNER JOIN children c ON c."inReplyToCommentId" = p.id) ' +
+            'SELECT id FROM children' +
+          ')'),
           [ Sequelize.Op.ne ]: comment.id
-        },
-        createdAt: {
-          [ Sequelize.Op.lt ]: comment.createdAt
         }
       },
       transaction: t
@@ -326,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
   }