]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-comment.ts
Move typescript utils in its own directory
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
index c465eb3e706585533c4bf5742f1b0610e4b52311..7f28b86b457115ef95dbb97e7885f1ab2030cefb 100644 (file)
@@ -1,19 +1,33 @@
-import * as Bluebird from 'bluebird'
 import { uniq } from 'lodash'
-import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import { FindAndCountOptions, FindOptions, Op, Order, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
+import {
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  ForeignKey,
+  HasMany,
+  Is,
+  Model,
+  Scopes,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
 import { getServerActor } from '@server/models/application/application'
 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
+import { AttributesOnly } from '@shared/typescript-utils'
 import { VideoPrivacy } from '@shared/models'
 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
-import { VideoComment } from '../../../shared/models/videos/video-comment.model'
+import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/comment/video-comment.model'
 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
 import { regexpCapture } from '../../helpers/regexp'
 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
 import {
   MComment,
+  MCommentAdminFormattable,
   MCommentAP,
   MCommentFormattable,
   MCommentId,
@@ -24,13 +38,21 @@ import {
   MCommentOwnerVideoReply,
   MVideoImmutable
 } from '../../types/models/video'
+import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
 import { AccountModel } from '../account/account'
-import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
-import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
+import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
+import {
+  buildBlockedAccountSQL,
+  buildBlockedAccountSQLOptimized,
+  buildLocalAccountIdsIn,
+  getCommentSort,
+  searchAttribute,
+  throwIfNotValid
+} from '../utils'
 import { VideoModel } from './video'
 import { VideoChannelModel } from './video-channel'
 
-enum ScopeNames {
+export enum ScopeNames {
   WITH_ACCOUNT = 'WITH_ACCOUNT',
   WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
   WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
@@ -47,14 +69,10 @@ enum ScopeNames {
             Sequelize.literal(
               '(' +
                 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
-                'SELECT COUNT("replies"."id") - (' +
-                  'SELECT COUNT("replies"."id") ' +
-                  'FROM "videoComment" AS "replies" ' +
-                  'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
-                  'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
-                ')' +
+                'SELECT COUNT("replies"."id") ' +
                 'FROM "videoComment" AS "replies" ' +
                 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
+                'AND "deletedAt" IS NULL ' +
                 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
               ')'
             ),
@@ -152,7 +170,7 @@ enum ScopeNames {
     }
   ]
 })
-export class VideoCommentModel extends Model<VideoCommentModel> {
+export class VideoCommentModel extends Model<Partial<AttributesOnly<VideoCommentModel>>> {
   @CreatedAt
   createdAt: Date
 
@@ -224,7 +242,16 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
   })
   Account: AccountModel
 
-  static loadById (id: number, t?: Transaction): Bluebird<MComment> {
+  @HasMany(() => VideoCommentAbuseModel, {
+    foreignKey: {
+      name: 'videoCommentId',
+      allowNull: true
+    },
+    onDelete: 'set null'
+  })
+  CommentAbuses: VideoCommentAbuseModel[]
+
+  static loadById (id: number, t?: Transaction): Promise<MComment> {
     const query: FindOptions = {
       where: {
         id
@@ -236,7 +263,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     return VideoCommentModel.findOne(query)
   }
 
-  static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
+  static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> {
     const query: FindOptions = {
       where: {
         id
@@ -250,7 +277,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       .findOne(query)
   }
 
-  static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
+  static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> {
     const query: FindOptions = {
       where: {
         url
@@ -262,7 +289,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
   }
 
-  static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
+  static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> {
     const query: FindOptions = {
       where: {
         url
@@ -280,6 +307,98 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
   }
 
+  static listCommentsForApi (parameters: {
+    start: number
+    count: number
+    sort: string
+
+    isLocal?: boolean
+    search?: string
+    searchAccount?: string
+    searchVideo?: string
+  }) {
+    const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters
+
+    const where: WhereOptions = {
+      deletedAt: null
+    }
+
+    const whereAccount: WhereOptions = {}
+    const whereActor: WhereOptions = {}
+    const whereVideo: WhereOptions = {}
+
+    if (isLocal === true) {
+      Object.assign(whereActor, {
+        serverId: null
+      })
+    } else if (isLocal === false) {
+      Object.assign(whereActor, {
+        serverId: {
+          [Op.ne]: null
+        }
+      })
+    }
+
+    if (search) {
+      Object.assign(where, {
+        [Op.or]: [
+          searchAttribute(search, 'text'),
+          searchAttribute(search, '$Account.Actor.preferredUsername$'),
+          searchAttribute(search, '$Account.name$'),
+          searchAttribute(search, '$Video.name$')
+        ]
+      })
+    }
+
+    if (searchAccount) {
+      Object.assign(whereActor, {
+        [Op.or]: [
+          searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
+          searchAttribute(searchAccount, '$Account.name$')
+        ]
+      })
+    }
+
+    if (searchVideo) {
+      Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
+    }
+
+    const query: FindAndCountOptions = {
+      offset: start,
+      limit: count,
+      order: getCommentSort(sort),
+      where,
+      include: [
+        {
+          model: AccountModel.unscoped(),
+          required: true,
+          where: whereAccount,
+          include: [
+            {
+              attributes: {
+                exclude: unusedActorAttributesForAPI
+              },
+              model: ActorModel, // Default scope includes avatar and server
+              required: true,
+              where: whereActor
+            }
+          ]
+        },
+        {
+          model: VideoModel.unscoped(),
+          required: true,
+          where: whereVideo
+        }
+      ]
+    }
+
+    return VideoCommentModel
+      .findAndCountAll(query)
+      .then(({ rows, count }) => {
+        return { total: count, data: rows }
+      })
+  }
+
   static async listThreadsForApi (parameters: {
     videoId: number
     isVideoOwned: boolean
@@ -292,7 +411,15 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
 
     const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
 
-    const query = {
+    const accountBlockedWhere = {
+      accountId: {
+        [Op.notIn]: Sequelize.literal(
+          '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
+        )
+      }
+    }
+
+    const queryList = {
       offset: start,
       limit: count,
       order: getCommentSort(sort),
@@ -306,13 +433,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
           },
           {
             [Op.or]: [
-              {
-                accountId: {
-                  [Op.notIn]: Sequelize.literal(
-                    '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
-                  )
-                }
-              },
+              accountBlockedWhere,
               {
                 accountId: null
               }
@@ -322,19 +443,27 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       }
     }
 
-    const scopes: (string | ScopeOptions)[] = [
+    const scopesList: (string | ScopeOptions)[] = [
       ScopeNames.WITH_ACCOUNT_FOR_API,
       {
         method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
       }
     ]
 
-    return VideoCommentModel
-      .scope(scopes)
-      .findAndCountAll(query)
-      .then(({ rows, count }) => {
-        return { total: count, data: rows }
-      })
+    const queryCount = {
+      where: {
+        videoId,
+        deletedAt: null,
+        ...accountBlockedWhere
+      }
+    }
+
+    return Promise.all([
+      VideoCommentModel.scope(scopesList).findAndCountAll(queryList),
+      VideoCommentModel.count(queryCount)
+    ]).then(([ { rows, count }, totalNotDeletedComments ]) => {
+      return { total: count, data: rows, totalNotDeletedComments }
+    })
   }
 
   static async listThreadCommentsForApi (parameters: {
@@ -351,15 +480,28 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
       where: {
         videoId,
-        [Op.or]: [
-          { id: threadId },
-          { originCommentId: threadId }
-        ],
-        accountId: {
-          [Op.notIn]: Sequelize.literal(
-            '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
-          )
-        }
+        [Op.and]: [
+          {
+            [Op.or]: [
+              { id: threadId },
+              { originCommentId: threadId }
+            ]
+          },
+          {
+            [Op.or]: [
+              {
+                accountId: {
+                  [Op.notIn]: Sequelize.literal(
+                    '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
+                  )
+                }
+              },
+              {
+                accountId: null
+              }
+            ]
+          }
+        ]
       }
     }
 
@@ -370,15 +512,14 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       }
     ]
 
-    return VideoCommentModel
-      .scope(scopes)
+    return VideoCommentModel.scope(scopes)
       .findAndCountAll(query)
       .then(({ rows, count }) => {
         return { total: count, data: rows }
       })
   }
 
-  static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
+  static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
     const query = {
       order: [ [ 'createdAt', order ] ] as Order,
       where: {
@@ -437,19 +578,20 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     const serverActor = await getServerActor()
     const { start, count, videoId, accountId, videoChannelId } = parameters
 
-    const accountExclusion = {
-      [Op.notIn]: Sequelize.literal(
-        '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
-      )
+    const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
+      '"VideoCommentModel"."accountId"',
+      [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
+    )
+
+    if (accountId) {
+      whereAnd.push({
+        accountId
+      })
+    }
+
+    const accountWhere = {
+      [Op.and]: whereAnd
     }
-    const accountWhere = accountId
-      ? {
-          [Op.and]: {
-            ...accountExclusion,
-            [Op.eq]: accountId
-          }
-        }
-      : accountExclusion
 
     const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
 
@@ -551,6 +693,18 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     }
   }
 
+  static listRemoteCommentUrlsOfLocalVideos () {
+    const query = `SELECT "videoComment".url FROM "videoComment" ` +
+      `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
+      `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
+      `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
+
+    return VideoCommentModel.sequelize.query<{ url: string }>(query, {
+      type: QueryTypes.SELECT,
+      raw: true
+    }).then(rows => rows.map(r => r.url))
+  }
+
   static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
     const query = {
       where: {
@@ -585,6 +739,12 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     return this.Account.isOwned()
   }
 
+  markAsDeleted () {
+    this.text = ''
+    this.deletedAt = new Date()
+    this.accountId = null
+  }
+
   isDeleted () {
     return this.deletedAt !== null
   }
@@ -632,19 +792,51 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       id: this.id,
       url: this.url,
       text: this.text,
-      threadId: this.originCommentId || this.id,
+
+      threadId: this.getThreadId(),
       inReplyToCommentId: this.inReplyToCommentId || null,
       videoId: this.videoId,
+
       createdAt: this.createdAt,
       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
+
+      account: this.Account
+        ? this.Account.toFormattedJSON()
+        : null
     } as VideoComment
   }
 
+  toFormattedAdminJSON (this: MCommentAdminFormattable) {
+    return {
+      id: this.id,
+      url: this.url,
+      text: this.text,
+
+      threadId: this.getThreadId(),
+      inReplyToCommentId: this.inReplyToCommentId || null,
+      videoId: this.videoId,
+
+      createdAt: this.createdAt,
+      updatedAt: this.updatedAt,
+
+      video: {
+        id: this.Video.id,
+        uuid: this.Video.uuid,
+        name: this.Video.name
+      },
+
+      account: this.Account
+        ? this.Account.toFormattedJSON()
+        : null
+    } as VideoCommentAdmin
+  }
+
   toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
     let inReplyTo: string
     // New thread, so in AS we reply to the video