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.ts72
1 files changed, 41 insertions, 31 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index 2469b37b1..62d78373e 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -1,11 +1,12 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as validator from 'validator' 2import * as validator from 'validator'
3import { ResultList } from '../../shared/models' 3import { ResultList } from '../../shared/models'
4import { Activity, ActivityPubActor } from '../../shared/models/activitypub' 4import { Activity } from '../../shared/models/activitypub'
5import { ACTIVITY_PUB } from '../initializers' 5import { ACTIVITY_PUB } from '../initializers'
6import { ActorModel } from '../models/activitypub/actor' 6import { ActorModel } from '../models/activitypub/actor'
7import { signObject } from './peertube-crypto' 7import { signJsonLDObject } from './peertube-crypto'
8import { pageToStartAndCount } from './core-utils' 8import { pageToStartAndCount } from './core-utils'
9import { parse } from 'url'
9 10
10function activityPubContextify <T> (data: T) { 11function activityPubContextify <T> (data: T) {
11 return Object.assign(data, { 12 return Object.assign(data, {
@@ -14,25 +15,26 @@ function activityPubContextify <T> (data: T) {
14 'https://w3id.org/security/v1', 15 'https://w3id.org/security/v1',
15 { 16 {
16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', 17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
17 pt: 'https://joinpeertube.org/ns', 18 pt: 'https://joinpeertube.org/ns#',
18 schema: 'http://schema.org#', 19 sc: 'http://schema.org#',
19 Hashtag: 'as:Hashtag', 20 Hashtag: 'as:Hashtag',
20 uuid: 'schema:identifier', 21 uuid: 'sc:identifier',
21 category: 'schema:category', 22 category: 'sc:category',
22 licence: 'schema:license', 23 licence: 'sc:license',
23 subtitleLanguage: 'schema:subtitleLanguage', 24 subtitleLanguage: 'sc:subtitleLanguage',
24 sensitive: 'as:sensitive', 25 sensitive: 'as:sensitive',
25 language: 'schema:inLanguage', 26 language: 'sc:inLanguage',
26 views: 'schema:Number', 27 views: 'sc:Number',
27 stats: 'schema:Number', 28 state: 'sc:Number',
28 size: 'schema:Number', 29 size: 'sc:Number',
29 fps: 'schema:Number', 30 fps: 'sc:Number',
30 commentsEnabled: 'schema:Boolean', 31 commentsEnabled: 'sc:Boolean',
31 downloadEnabled: 'schema:Boolean', 32 downloadEnabled: 'sc:Boolean',
32 waitTranscoding: 'schema:Boolean', 33 waitTranscoding: 'sc:Boolean',
33 expires: 'schema:expires', 34 expires: 'sc:expires',
34 support: 'schema:Text', 35 support: 'sc:Text',
35 CacheFile: 'pt:CacheFile' 36 CacheFile: 'pt:CacheFile',
37 Infohash: 'pt:Infohash'
36 }, 38 },
37 { 39 {
38 likes: { 40 likes: {
@@ -57,16 +59,16 @@ function activityPubContextify <T> (data: T) {
57} 59}
58 60
59type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> 61type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
60async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { 62async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
61 if (!page || !validator.isInt(page)) { 63 if (!page || !validator.isInt(page)) {
62 // We just display the first page URL, we only need the total items 64 // We just display the first page URL, we only need the total items
63 const result = await handler(0, 1) 65 const result = await handler(0, 1)
64 66
65 return { 67 return {
66 id: url, 68 id: baseUrl,
67 type: 'OrderedCollection', 69 type: 'OrderedCollection',
68 totalItems: result.total, 70 totalItems: result.total,
69 first: url + '?page=1' 71 first: baseUrl + '?page=1'
70 } 72 }
71 } 73 }
72 74
@@ -81,19 +83,19 @@ async function activityPubCollectionPagination (url: string, handler: ActivityPu
81 83
82 // There are more results 84 // There are more results
83 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { 85 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
84 next = url + '?page=' + (page + 1) 86 next = baseUrl + '?page=' + (page + 1)
85 } 87 }
86 88
87 if (page > 1) { 89 if (page > 1) {
88 prev = url + '?page=' + (page - 1) 90 prev = baseUrl + '?page=' + (page - 1)
89 } 91 }
90 92
91 return { 93 return {
92 id: url + '?page=' + page, 94 id: baseUrl + '?page=' + page,
93 type: 'OrderedCollectionPage', 95 type: 'OrderedCollectionPage',
94 prev, 96 prev,
95 next, 97 next,
96 partOf: url, 98 partOf: baseUrl,
97 orderedItems: result.data, 99 orderedItems: result.data,
98 totalItems: result.total 100 totalItems: result.total
99 } 101 }
@@ -103,19 +105,27 @@ async function activityPubCollectionPagination (url: string, handler: ActivityPu
103function buildSignedActivity (byActor: ActorModel, data: Object) { 105function buildSignedActivity (byActor: ActorModel, data: Object) {
104 const activity = activityPubContextify(data) 106 const activity = activityPubContextify(data)
105 107
106 return signObject(byActor, activity) as Promise<Activity> 108 return signJsonLDObject(byActor, activity) as Promise<Activity>
107} 109}
108 110
109function getActorUrl (activityActor: string | ActivityPubActor) { 111function getAPId (activity: string | { id: string }) {
110 if (typeof activityActor === 'string') return activityActor 112 if (typeof activity === 'string') return activity
111 113
112 return activityActor.id 114 return activity.id
115}
116
117function checkUrlsSameHost (url1: string, url2: string) {
118 const idHost = parse(url1).host
119 const actorHost = parse(url2).host
120
121 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
113} 122}
114 123
115// --------------------------------------------------------------------------- 124// ---------------------------------------------------------------------------
116 125
117export { 126export {
118 getActorUrl, 127 checkUrlsSameHost,
128 getAPId,
119 activityPubContextify, 129 activityPubContextify,
120 activityPubCollectionPagination, 130 activityPubCollectionPagination,
121 buildSignedActivity 131 buildSignedActivity