aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/activitypub.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r--server/helpers/activitypub.ts81
1 files changed, 55 insertions, 26 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index 239d8291d..2d49e6869 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -2,21 +2,36 @@ import * as Bluebird from 'bluebird'
2import validator from 'validator' 2import validator from 'validator'
3import { ResultList } from '../../shared/models' 3import { ResultList } from '../../shared/models'
4import { Activity } from '../../shared/models/activitypub' 4import { Activity } from '../../shared/models/activitypub'
5import { ACTIVITY_PUB } from '../initializers/constants' 5import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
6import { signJsonLDObject } from './peertube-crypto' 6import { signJsonLDObject } from './peertube-crypto'
7import { pageToStartAndCount } from './core-utils' 7import { pageToStartAndCount } from './core-utils'
8import { parse } from 'url' 8import { URL } from 'url'
9import { MActor } from '../typings/models' 9import { MActor, MVideoAccountLight } from '../typings/models'
10 10
11function activityPubContextify <T> (data: T) { 11export type ContextType = 'All' | 'View' | 'Announce' | 'CacheFile'
12 return Object.assign(data, { 12
13 '@context': [ 13function getContextData (type: ContextType) {
14 'https://www.w3.org/ns/activitystreams', 14 const context: any[] = [
15 'https://w3id.org/security/v1', 15 'https://www.w3.org/ns/activitystreams',
16 { 16 'https://w3id.org/security/v1',
17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', 17 {
18 pt: 'https://joinpeertube.org/ns#', 18 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
19 sc: 'http://schema.org#', 19 }
20 ]
21
22 if (type !== 'View' && type !== 'Announce') {
23 const additional = {
24 pt: 'https://joinpeertube.org/ns#',
25 sc: 'http://schema.org#'
26 }
27
28 if (type === 'CacheFile') {
29 Object.assign(additional, {
30 expires: 'sc:expires',
31 CacheFile: 'pt:CacheFile'
32 })
33 } else {
34 Object.assign(additional, {
20 Hashtag: 'as:Hashtag', 35 Hashtag: 'as:Hashtag',
21 uuid: 'sc:identifier', 36 uuid: 'sc:identifier',
22 category: 'sc:category', 37 category: 'sc:category',
@@ -24,8 +39,7 @@ function activityPubContextify <T> (data: T) {
24 subtitleLanguage: 'sc:subtitleLanguage', 39 subtitleLanguage: 'sc:subtitleLanguage',
25 sensitive: 'as:sensitive', 40 sensitive: 'as:sensitive',
26 language: 'sc:inLanguage', 41 language: 'sc:inLanguage',
27 expires: 'sc:expires', 42
28 CacheFile: 'pt:CacheFile',
29 Infohash: 'pt:Infohash', 43 Infohash: 'pt:Infohash',
30 originallyPublishedAt: 'sc:datePublished', 44 originallyPublishedAt: 'sc:datePublished',
31 views: { 45 views: {
@@ -71,9 +85,7 @@ function activityPubContextify <T> (data: T) {
71 support: { 85 support: {
72 '@type': 'sc:Text', 86 '@type': 'sc:Text',
73 '@id': 'pt:support' 87 '@id': 'pt:support'
74 } 88 },
75 },
76 {
77 likes: { 89 likes: {
78 '@id': 'as:likes', 90 '@id': 'as:likes',
79 '@type': '@id' 91 '@type': '@id'
@@ -94,9 +106,19 @@ function activityPubContextify <T> (data: T) {
94 '@id': 'as:comments', 106 '@id': 'as:comments',
95 '@type': '@id' 107 '@type': '@id'
96 } 108 }
97 } 109 })
98 ] 110 }
99 }) 111
112 context.push(additional)
113 }
114
115 return {
116 '@context': context
117 }
118}
119
120function activityPubContextify <T> (data: T, type: ContextType = 'All') {
121 return Object.assign({}, data, getContextData(type))
100} 122}
101 123
102type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> 124type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
@@ -148,8 +170,8 @@ async function activityPubCollectionPagination (
148 170
149} 171}
150 172
151function buildSignedActivity (byActor: MActor, data: Object) { 173function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
152 const activity = activityPubContextify(data) 174 const activity = activityPubContextify(data, contextType)
153 175
154 return signJsonLDObject(byActor, activity) as Promise<Activity> 176 return signJsonLDObject(byActor, activity) as Promise<Activity>
155} 177}
@@ -161,12 +183,18 @@ function getAPId (activity: string | { id: string }) {
161} 183}
162 184
163function checkUrlsSameHost (url1: string, url2: string) { 185function checkUrlsSameHost (url1: string, url2: string) {
164 const idHost = parse(url1).host 186 const idHost = new URL(url1).host
165 const actorHost = parse(url2).host 187 const actorHost = new URL(url2).host
166 188
167 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() 189 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
168} 190}
169 191
192function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
193 const host = video.VideoChannel.Account.Actor.Server.host
194
195 return REMOTE_SCHEME.HTTP + '://' + host + path
196}
197
170// --------------------------------------------------------------------------- 198// ---------------------------------------------------------------------------
171 199
172export { 200export {
@@ -174,5 +202,6 @@ export {
174 getAPId, 202 getAPId,
175 activityPubContextify, 203 activityPubContextify,
176 activityPubCollectionPagination, 204 activityPubCollectionPagination,
177 buildSignedActivity 205 buildSignedActivity,
206 buildRemoteVideoBaseUrl
178} 207}