]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/activitypub.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
index 239d8291d8ef9936b4cb183f020eec0c45f44546..1188d6cf9fd46e3843d46ac7b4e3e5539d868077 100644 (file)
@@ -2,21 +2,35 @@ import * as Bluebird from 'bluebird'
 import validator from 'validator'
 import { ResultList } from '../../shared/models'
 import { Activity } from '../../shared/models/activitypub'
-import { ACTIVITY_PUB } from '../initializers/constants'
+import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
 import { signJsonLDObject } from './peertube-crypto'
 import { pageToStartAndCount } from './core-utils'
-import { parse } from 'url'
-import { MActor } from '../typings/models'
-
-function activityPubContextify <T> (data: T) {
-  return Object.assign(data, {
-    '@context': [
-      'https://www.w3.org/ns/activitystreams',
-      'https://w3id.org/security/v1',
-      {
-        RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
-        pt: 'https://joinpeertube.org/ns#',
-        sc: 'http://schema.org#',
+import { URL } from 'url'
+import { MActor, MVideoAccountLight } from '../types/models'
+import { ContextType } from '@shared/models/activitypub/context'
+
+function getContextData (type: ContextType) {
+  const context: any[] = [
+    'https://www.w3.org/ns/activitystreams',
+    'https://w3id.org/security/v1',
+    {
+      RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
+    }
+  ]
+
+  if (type !== 'View' && type !== 'Announce') {
+    const additional = {
+      pt: 'https://joinpeertube.org/ns#',
+      sc: 'http://schema.org#'
+    }
+
+    if (type === 'CacheFile') {
+      Object.assign(additional, {
+        expires: 'sc:expires',
+        CacheFile: 'pt:CacheFile'
+      })
+    } else {
+      Object.assign(additional, {
         Hashtag: 'as:Hashtag',
         uuid: 'sc:identifier',
         category: 'sc:category',
@@ -24,9 +38,21 @@ function activityPubContextify <T> (data: T) {
         subtitleLanguage: 'sc:subtitleLanguage',
         sensitive: 'as:sensitive',
         language: 'sc:inLanguage',
-        expires: 'sc:expires',
-        CacheFile: 'pt:CacheFile',
+
+        isLiveBroadcast: 'sc:isLiveBroadcast',
+        liveSaveReplay: {
+          '@type': 'sc:Boolean',
+          '@id': 'pt:liveSaveReplay'
+        },
+        permanentLive: {
+          '@type': 'sc:Boolean',
+          '@id': 'pt:permanentLive'
+        },
+
         Infohash: 'pt:Infohash',
+        Playlist: 'pt:Playlist',
+        PlaylistElement: 'pt:PlaylistElement',
+
         originallyPublishedAt: 'sc:datePublished',
         views: {
           '@type': 'sc:Number',
@@ -71,9 +97,7 @@ function activityPubContextify <T> (data: T) {
         support: {
           '@type': 'sc:Text',
           '@id': 'pt:support'
-        }
-      },
-      {
+        },
         likes: {
           '@id': 'as:likes',
           '@type': '@id'
@@ -94,9 +118,19 @@ function activityPubContextify <T> (data: T) {
           '@id': 'as:comments',
           '@type': '@id'
         }
-      }
-    ]
-  })
+      })
+    }
+
+    context.push(additional)
+  }
+
+  return {
+    '@context': context
+  }
+}
+
+function activityPubContextify <T> (data: T, type: ContextType = 'All') {
+  return Object.assign({}, data, getContextData(type))
 }
 
 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
@@ -148,8 +182,8 @@ async function activityPubCollectionPagination (
 
 }
 
-function buildSignedActivity (byActor: MActor, data: Object) {
-  const activity = activityPubContextify(data)
+function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
+  const activity = activityPubContextify(data, contextType)
 
   return signJsonLDObject(byActor, activity) as Promise<Activity>
 }
@@ -161,12 +195,18 @@ function getAPId (activity: string | { id: string }) {
 }
 
 function checkUrlsSameHost (url1: string, url2: string) {
-  const idHost = parse(url1).host
-  const actorHost = parse(url2).host
+  const idHost = new URL(url1).host
+  const actorHost = new URL(url2).host
 
   return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
 }
 
+function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
+  const host = video.VideoChannel.Account.Actor.Server.host
+
+  return REMOTE_SCHEME.HTTP + '://' + host + path
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -174,5 +214,6 @@ export {
   getAPId,
   activityPubContextify,
   activityPubCollectionPagination,
-  buildSignedActivity
+  buildSignedActivity,
+  buildRemoteVideoBaseUrl
 }