]> 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 9f9e8fba79fe2b31d9e64af34c865e454d6df99b..1188d6cf9fd46e3843d46ac7b4e3e5539d868077 100644 (file)
@@ -5,18 +5,32 @@ import { Activity } from '../../shared/models/activitypub'
 import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
 import { signJsonLDObject } from './peertube-crypto'
 import { pageToStartAndCount } from './core-utils'
-import { parse } from 'url'
-import { MActor, MVideoAccountLight } 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,8 +195,8 @@ 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()
 }