diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/helpers/activitypub.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r-- | server/helpers/activitypub.ts | 72 |
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 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as validator from 'validator' | 2 | import * as validator from 'validator' |
3 | import { ResultList } from '../../shared/models' | 3 | import { ResultList } from '../../shared/models' |
4 | import { Activity, ActivityPubActor } from '../../shared/models/activitypub' | 4 | import { Activity } from '../../shared/models/activitypub' |
5 | import { ACTIVITY_PUB } from '../initializers' | 5 | import { ACTIVITY_PUB } from '../initializers' |
6 | import { ActorModel } from '../models/activitypub/actor' | 6 | import { ActorModel } from '../models/activitypub/actor' |
7 | import { signObject } from './peertube-crypto' | 7 | import { signJsonLDObject } from './peertube-crypto' |
8 | import { pageToStartAndCount } from './core-utils' | 8 | import { pageToStartAndCount } from './core-utils' |
9 | import { parse } from 'url' | ||
9 | 10 | ||
10 | function activityPubContextify <T> (data: T) { | 11 | function 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 | ||
59 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | 61 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
60 | async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { | 62 | async 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 | |||
103 | function buildSignedActivity (byActor: ActorModel, data: Object) { | 105 | function 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 | ||
109 | function getActorUrl (activityActor: string | ActivityPubActor) { | 111 | function 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 | |||
117 | function 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 | ||
117 | export { | 126 | export { |
118 | getActorUrl, | 127 | checkUrlsSameHost, |
128 | getAPId, | ||
119 | activityPubContextify, | 129 | activityPubContextify, |
120 | activityPubCollectionPagination, | 130 | activityPubCollectionPagination, |
121 | buildSignedActivity | 131 | buildSignedActivity |