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