]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/misc.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / misc.ts
index 0baa22c2617d5dc0c3de9fb13eafeac43a16b870..461619ea758c6f4a0ad98016fca5902ce424ed07 100644 (file)
@@ -1,32 +1,20 @@
 import * as magnetUtil from 'magnet-uri'
 import { VideoTorrentObject } from '../../../../shared'
-import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object'
-import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
+import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
+import { VideoPrivacy } from '../../../../shared/models/videos'
 import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
+import { logger } from '../../../helpers/logger'
 import { doRequest } from '../../../helpers/requests'
-import { database as db } from '../../../initializers'
-import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants'
-import { AccountInstance } from '../../../models/account/account-interface'
-import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
-import { VideoFileAttributes } from '../../../models/video/video-file-interface'
-import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface'
-import { getOrCreateAccountAndServer } from '../account'
-
-function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
-  return {
-    name: videoChannelObject.name,
-    description: videoChannelObject.content,
-    uuid: videoChannelObject.uuid,
-    url: videoChannelObject.id,
-    createdAt: new Date(videoChannelObject.published),
-    updatedAt: new Date(videoChannelObject.updated),
-    remote: true,
-    accountId: account.id
-  }
-}
+import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { VideoChannelModel } from '../../../models/video/video-channel'
+import { VideoCommentModel } from '../../../models/video/video-comment'
+import { VideoShareModel } from '../../../models/video/video-share'
+import { getOrCreateActorAndServerAndModel } from '../actor'
 
 async function videoActivityObjectToDBAttributes (
-  videoChannel: VideoChannelInstance,
+  videoChannel: VideoChannelModel,
   videoObject: VideoTorrentObject,
   to: string[] = [],
   cc: string[] = []
@@ -56,7 +44,7 @@ async function videoActivityObjectToDBAttributes (
     description = videoObject.content
   }
 
-  const videoData: VideoAttributes = {
+  return {
     name: videoObject.name,
     uuid: videoObject.uuid,
     url: videoObject.id,
@@ -65,6 +53,7 @@ async function videoActivityObjectToDBAttributes (
     language,
     description,
     nsfw: videoObject.nsfw,
+    commentsEnabled: videoObject.commentsEnabled,
     channelId: videoChannel.id,
     duration: parseInt(duration, 10),
     createdAt: new Date(videoObject.published),
@@ -76,11 +65,9 @@ async function videoActivityObjectToDBAttributes (
     remote: true,
     privacy
   }
-
-  return videoData
 }
 
-function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
+function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
   const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
   const fileUrls = videoObject.url.filter(u => {
     return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
@@ -90,7 +77,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO
     throw new Error('Cannot find video files for ' + videoCreated.url)
   }
 
-  const attributes: VideoFileAttributes[] = []
+  const attributes = []
   for (const fileUrl of fileUrls) {
     // Fetch associated magnet uri
     const magnet = videoObject.url.find(u => {
@@ -115,60 +102,93 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO
   return attributes
 }
 
-async function addVideoShares (instance: VideoInstance, shares: string[]) {
-  for (const share of shares) {
-    // Fetch url
-    const json = await doRequest({
-      uri: share,
-      json: true
-    })
-    const actor = json['actor']
-    if (!actor) continue
-
-    const account = await getOrCreateAccountAndServer(actor)
+async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
+  let originCommentId: number = null
+  let inReplyToCommentId: number = null
 
-    const entry = {
-      accountId: account.id,
-      videoId: instance.id
+  // 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)
+    if (!parent) {
+      logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
+      return undefined
     }
 
-    await db.VideoShare.findOrCreate({
-      where: entry,
-      defaults: entry
-    })
+    originCommentId = parent.originCommentId || parent.id
+    inReplyToCommentId = parent.id
+  }
+
+  return {
+    url: comment.url,
+    text: comment.content,
+    videoId: video.id,
+    accountId: actor.Account.id,
+    inReplyToCommentId,
+    originCommentId,
+    createdAt: new Date(comment.published),
+    updatedAt: new Date(comment.updated)
   }
 }
 
-async function addVideoChannelShares (instance: VideoChannelInstance, shares: string[]) {
-  for (const share of shares) {
+async function addVideoShares (instance: VideoModel, shareUrls: string[]) {
+  for (const shareUrl of shareUrls) {
     // Fetch url
-    const json = await doRequest({
-      uri: share,
-      json: true
+    const { body } = await doRequest({
+      uri: shareUrl,
+      json: true,
+      activityPub: true
     })
-    const actor = json['actor']
-    if (!actor) continue
+    const actorUrl = body.actor
+    if (!actorUrl) continue
 
-    const account = await getOrCreateAccountAndServer(actor)
+    const actor = await getOrCreateActorAndServerAndModel(actorUrl)
 
     const entry = {
-      accountId: account.id,
-      videoChannelId: instance.id
+      actorId: actor.id,
+      videoId: instance.id
     }
 
-    await db.VideoChannelShare.findOrCreate({
+    await VideoShareModel.findOrCreate({
       where: entry,
       defaults: entry
     })
   }
 }
 
+async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
+  for (const commentUrl of commentUrls) {
+    await addVideoComment(instance, commentUrl)
+  }
+}
+
+async function addVideoComment (instance: VideoModel, commentUrl: string) {
+  // Fetch url
+  const { body } = await doRequest({
+    uri: commentUrl,
+    json: true,
+    activityPub: true
+  })
+
+  const actorUrl = body.attributedTo
+  if (!actorUrl) return []
+
+  const actor = await getOrCreateActorAndServerAndModel(actorUrl)
+  const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body)
+  if (!entry) return []
+
+  return VideoCommentModel.findOrCreate({
+    where: {
+      url: body.id
+    },
+    defaults: entry
+  })
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   videoFileActivityUrlToDBAttributes,
   videoActivityObjectToDBAttributes,
-  videoChannelActivityObjectToDBAttributes,
-  addVideoChannelShares,
-  addVideoShares
+  addVideoShares,
+  addVideoComments
 }