]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-comment.ts
Send video comment comments to followers/origin
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
index 8e84bfc06ab0603429b4811c8837a73c8f93136f..25cd6d5635eee82787d59838137a805aaa79fa22 100644 (file)
@@ -3,6 +3,7 @@ import {
   AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
   UpdatedAt
 } from 'sequelize-typescript'
+import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
 import { CONSTRAINTS_FIELDS } from '../../initializers'
@@ -11,7 +12,8 @@ import { getSort, throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
 
 enum ScopeNames {
-  WITH_ACCOUNT = 'WITH_ACCOUNT'
+  WITH_ACCOUNT = 'WITH_ACCOUNT',
+  WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO'
 }
 
 @Scopes({
@@ -19,6 +21,14 @@ enum ScopeNames {
     include: [
       () => AccountModel
     ]
+  },
+  [ScopeNames.WITH_IN_REPLY_TO]: {
+    include: [
+      {
+        model: () => VideoCommentModel,
+        as: 'InReplyTo'
+      }
+    ]
   }
 })
 @Table({
@@ -68,6 +78,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
     foreignKey: {
       allowNull: true
     },
+    as: 'InReplyTo',
     onDelete: 'CASCADE'
   })
   InReplyToVideoComment: VideoCommentModel
@@ -180,4 +191,23 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       }
     } as VideoComment
   }
+
+  toActivityPubObject (): VideoCommentObject {
+    let inReplyTo: string
+    // New thread, so in AS we reply to the video
+    if (this.inReplyToCommentId === null) {
+      inReplyTo = this.Video.url
+    } else {
+      inReplyTo = this.InReplyToVideoComment.url
+    }
+
+    return {
+      type: 'Note' as 'Note',
+      id: this.url,
+      content: this.text,
+      inReplyTo,
+      published: this.createdAt.toISOString(),
+      url: this.url
+    }
+  }
 }