]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/video-comments.ts
Fix HLS federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-comments.ts
index 8ab0cdba4211de4615350eb76e9d0ba11ffe2fc4..3f9d8f0fc4a4c85c46f3c84e8eb8f76180790aa3 100644 (file)
@@ -1,13 +1,15 @@
 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
-import { isVideoCommentObjectValid } from '../../helpers/custom-validators/activitypub/video-comments'
+import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
 import { logger } from '../../helpers/logger'
 import { doRequest } from '../../helpers/requests'
-import { ACTIVITY_PUB } from '../../initializers'
+import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
 import { ActorModel } from '../../models/activitypub/actor'
 import { VideoModel } from '../../models/video/video'
 import { VideoCommentModel } from '../../models/video/video-comment'
 import { getOrCreateActorAndServerAndModel } from './actor'
-import { getOrCreateAccountAndVideoAndChannel } from './videos'
+import { getOrCreateVideoAndAccountAndChannel } from './videos'
+import * as Bluebird from 'bluebird'
+import { checkUrlsSameHost } from '../../helpers/activitypub'
 
 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
   let originCommentId: number = null
@@ -15,7 +17,7 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto
 
   // If this is not a reply to the video (thread), create or get the parent comment
   if (video.url !== comment.inReplyTo) {
-    const [ parent ] = await addVideoComment(video, comment.inReplyTo)
+    const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
     if (!parent) {
       logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
       return undefined
@@ -26,21 +28,20 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto
   }
 
   return {
-    url: comment.url,
+    url: comment.id,
     text: comment.content,
     videoId: video.id,
     accountId: actor.Account.id,
     inReplyToCommentId,
     originCommentId,
-    createdAt: new Date(comment.published),
-    updatedAt: new Date(comment.updated)
+    createdAt: new Date(comment.published)
   }
 }
 
-async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
-  for (const commentUrl of commentUrls) {
-    await addVideoComment(instance, commentUrl)
-  }
+async function addVideoComments (commentUrls: string[], instance: VideoModel) {
+  return Bluebird.map(commentUrls, commentUrl => {
+    return addVideoComment(instance, commentUrl)
+  }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
 }
 
 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
@@ -52,24 +53,31 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
     activityPub: true
   })
 
-  if (isVideoCommentObjectValid(body) === false) {
+  if (sanitizeAndCheckVideoCommentObject(body) === false) {
     logger.debug('Remote video comment JSON is not valid.', { body })
-    return undefined
+    return { created: false }
   }
 
   const actorUrl = body.attributedTo
-  if (!actorUrl) return []
+  if (!actorUrl) return { created: false }
+
+  if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
+    throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
+  }
 
-  const actor = await getOrCreateActorAndServerAndModel(actorUrl)
+  if (checkUrlsSameHost(body.id, commentUrl) !== true) {
+    throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
+  }
+
+  const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
   const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
-  if (!entry) return []
+  if (!entry) return { created: false }
 
-  return VideoCommentModel.findOrCreate({
-    where: {
-      url: body.id
-    },
-    defaults: entry
-  })
+  const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true })
+  comment.Account = actor.Account
+  comment.Video = videoInstance
+
+  return { comment, created }
 }
 
 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
@@ -90,7 +98,8 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
 
   try {
     // Maybe it's a reply to a video?
-    const { video } = await getOrCreateAccountAndVideoAndChannel(url)
+    // If yes, it's done: we resolved all the thread
+    const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
 
     if (comments.length !== 0) {
       const firstReply = comments[ comments.length - 1 ]
@@ -123,16 +132,24 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
       activityPub: true
     })
 
-    if (isVideoCommentObjectValid(body) === false) {
+    if (sanitizeAndCheckVideoCommentObject(body) === false) {
       throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
     }
 
     const actorUrl = body.attributedTo
     if (!actorUrl) throw new Error('Miss attributed to in comment')
 
+    if (checkUrlsSameHost(url, actorUrl) !== true) {
+      throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
+    }
+
+    if (checkUrlsSameHost(body.id, url) !== true) {
+      throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
+    }
+
     const actor = await getOrCreateActorAndServerAndModel(actorUrl)
     const comment = new VideoCommentModel({
-      url: body.url,
+      url: body.id,
       text: body.content,
       videoId: null,
       accountId: actor.Account.id,