]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-comment.ts
Add filter inputs for blacklisted videos and muted accounts/servers
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
index 28f011b03b3a1b13a6fc7b577d886f09b488098e..7cdff8c2cbefb6ba6d162d4b89cf83d6c1be8e31 100644 (file)
@@ -27,6 +27,7 @@ import {
   MCommentOwnerVideoReply
 } from '../../typings/models/video'
 import { MUserAccountId } from '@server/typings/models'
+import { VideoPrivacy } from '@shared/models'
 
 enum ScopeNames {
   WITH_ACCOUNT = 'WITH_ACCOUNT',
@@ -56,6 +57,19 @@ enum ScopeNames {
               ')'
             ),
             'totalReplies'
+          ],
+          [
+            Sequelize.literal(
+              '(' +
+                'SELECT COUNT("replies"."id") ' +
+                'FROM "videoComment" AS "replies" ' +
+                'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
+                'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
+                'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
+                'AND "replies"."accountId" = "videoChannel"."accountId"' +
+              ')'
+            ),
+            'totalRepliesFromVideoAuthor'
           ]
         ]
       }
@@ -244,10 +258,10 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
   }
 
   static async listThreadsForApi (parameters: {
-    videoId: number,
-    start: number,
-    count: number,
-    sort: string,
+    videoId: number
+    start: number
+    count: number
+    sort: string
     user?: MUserAccountId
   }) {
     const { videoId, start, count, sort, user } = parameters
@@ -287,8 +301,8 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
   }
 
   static async listThreadCommentsForApi (parameters: {
-    videoId: number,
-    threadId: number,
+    videoId: number
+    threadId: number
     user?: MUserAccountId
   }) {
     const { videoId, threadId, user } = parameters
@@ -301,7 +315,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
       where: {
         videoId,
-        [ Op.or ]: [
+        [Op.or]: [
           { id: threadId },
           { originCommentId: threadId }
         ],
@@ -333,7 +347,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       order: [ [ 'createdAt', order ] ] as Order,
       where: {
         id: {
-          [ Op.in ]: Sequelize.literal('(' +
+          [Op.in]: Sequelize.literal('(' +
             'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
               `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
               'UNION ' +
@@ -342,7 +356,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
             ') ' +
             'SELECT id FROM children' +
           ')'),
-          [ Op.ne ]: comment.id
+          [Op.ne]: comment.id
         }
       },
       transaction: t
@@ -372,12 +386,17 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       order: [ [ 'createdAt', 'DESC' ] ] as Order,
       offset: start,
       limit: count,
-      where: {},
+      where: {
+        deletedAt: null
+      },
       include: [
         {
           attributes: [ 'name', 'uuid' ],
           model: VideoModel.unscoped(),
-          required: true
+          required: true,
+          where: {
+            privacy: VideoPrivacy.PUBLIC
+          }
         }
       ]
     }
@@ -448,7 +467,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
   }
 
   isDeleted () {
-    return null !== this.deletedAt
+    return this.deletedAt !== null
   }
 
   extractMentions () {
@@ -501,6 +520,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       updatedAt: this.updatedAt,
       deletedAt: this.deletedAt,
       isDeleted: this.isDeleted(),
+      totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
       totalReplies: this.get('totalReplies') || 0,
       account: this.Account ? this.Account.toFormattedJSON() : null
     } as VideoComment