diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/activitypub.ts | 197 |
1 files changed, 106 insertions, 91 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 326785b68..2d49e6869 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -8,102 +8,117 @@ import { pageToStartAndCount } from './core-utils' | |||
8 | import { URL } from 'url' | 8 | import { URL } from 'url' |
9 | import { MActor, MVideoAccountLight } from '../typings/models' | 9 | import { MActor, MVideoAccountLight } from '../typings/models' |
10 | 10 | ||
11 | export type ContextType = 'All' | 'View' | 'Announce' | 11 | export type ContextType = 'All' | 'View' | 'Announce' | 'CacheFile' |
12 | |||
13 | function getContextData (type: ContextType) { | ||
14 | const context: any[] = [ | ||
15 | 'https://www.w3.org/ns/activitystreams', | ||
16 | 'https://w3id.org/security/v1', | ||
17 | { | ||
18 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' | ||
19 | } | ||
20 | ] | ||
12 | 21 | ||
13 | function activityPubContextify <T> (data: T, type: ContextType = 'All') { | 22 | if (type !== 'View' && type !== 'Announce') { |
14 | const base = { | 23 | const additional = { |
15 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' | 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, { | ||
35 | Hashtag: 'as:Hashtag', | ||
36 | uuid: 'sc:identifier', | ||
37 | category: 'sc:category', | ||
38 | licence: 'sc:license', | ||
39 | subtitleLanguage: 'sc:subtitleLanguage', | ||
40 | sensitive: 'as:sensitive', | ||
41 | language: 'sc:inLanguage', | ||
42 | |||
43 | Infohash: 'pt:Infohash', | ||
44 | originallyPublishedAt: 'sc:datePublished', | ||
45 | views: { | ||
46 | '@type': 'sc:Number', | ||
47 | '@id': 'pt:views' | ||
48 | }, | ||
49 | state: { | ||
50 | '@type': 'sc:Number', | ||
51 | '@id': 'pt:state' | ||
52 | }, | ||
53 | size: { | ||
54 | '@type': 'sc:Number', | ||
55 | '@id': 'pt:size' | ||
56 | }, | ||
57 | fps: { | ||
58 | '@type': 'sc:Number', | ||
59 | '@id': 'pt:fps' | ||
60 | }, | ||
61 | startTimestamp: { | ||
62 | '@type': 'sc:Number', | ||
63 | '@id': 'pt:startTimestamp' | ||
64 | }, | ||
65 | stopTimestamp: { | ||
66 | '@type': 'sc:Number', | ||
67 | '@id': 'pt:stopTimestamp' | ||
68 | }, | ||
69 | position: { | ||
70 | '@type': 'sc:Number', | ||
71 | '@id': 'pt:position' | ||
72 | }, | ||
73 | commentsEnabled: { | ||
74 | '@type': 'sc:Boolean', | ||
75 | '@id': 'pt:commentsEnabled' | ||
76 | }, | ||
77 | downloadEnabled: { | ||
78 | '@type': 'sc:Boolean', | ||
79 | '@id': 'pt:downloadEnabled' | ||
80 | }, | ||
81 | waitTranscoding: { | ||
82 | '@type': 'sc:Boolean', | ||
83 | '@id': 'pt:waitTranscoding' | ||
84 | }, | ||
85 | support: { | ||
86 | '@type': 'sc:Text', | ||
87 | '@id': 'pt:support' | ||
88 | }, | ||
89 | likes: { | ||
90 | '@id': 'as:likes', | ||
91 | '@type': '@id' | ||
92 | }, | ||
93 | dislikes: { | ||
94 | '@id': 'as:dislikes', | ||
95 | '@type': '@id' | ||
96 | }, | ||
97 | playlists: { | ||
98 | '@id': 'pt:playlists', | ||
99 | '@type': '@id' | ||
100 | }, | ||
101 | shares: { | ||
102 | '@id': 'as:shares', | ||
103 | '@type': '@id' | ||
104 | }, | ||
105 | comments: { | ||
106 | '@id': 'as:comments', | ||
107 | '@type': '@id' | ||
108 | } | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | context.push(additional) | ||
16 | } | 113 | } |
17 | 114 | ||
18 | if (type === 'All') { | 115 | return { |
19 | Object.assign(base, { | 116 | '@context': context |
20 | pt: 'https://joinpeertube.org/ns#', | ||
21 | sc: 'http://schema.org#', | ||
22 | Hashtag: 'as:Hashtag', | ||
23 | uuid: 'sc:identifier', | ||
24 | category: 'sc:category', | ||
25 | licence: 'sc:license', | ||
26 | subtitleLanguage: 'sc:subtitleLanguage', | ||
27 | sensitive: 'as:sensitive', | ||
28 | language: 'sc:inLanguage', | ||
29 | expires: 'sc:expires', | ||
30 | CacheFile: 'pt:CacheFile', | ||
31 | Infohash: 'pt:Infohash', | ||
32 | originallyPublishedAt: 'sc:datePublished', | ||
33 | views: { | ||
34 | '@type': 'sc:Number', | ||
35 | '@id': 'pt:views' | ||
36 | }, | ||
37 | state: { | ||
38 | '@type': 'sc:Number', | ||
39 | '@id': 'pt:state' | ||
40 | }, | ||
41 | size: { | ||
42 | '@type': 'sc:Number', | ||
43 | '@id': 'pt:size' | ||
44 | }, | ||
45 | fps: { | ||
46 | '@type': 'sc:Number', | ||
47 | '@id': 'pt:fps' | ||
48 | }, | ||
49 | startTimestamp: { | ||
50 | '@type': 'sc:Number', | ||
51 | '@id': 'pt:startTimestamp' | ||
52 | }, | ||
53 | stopTimestamp: { | ||
54 | '@type': 'sc:Number', | ||
55 | '@id': 'pt:stopTimestamp' | ||
56 | }, | ||
57 | position: { | ||
58 | '@type': 'sc:Number', | ||
59 | '@id': 'pt:position' | ||
60 | }, | ||
61 | commentsEnabled: { | ||
62 | '@type': 'sc:Boolean', | ||
63 | '@id': 'pt:commentsEnabled' | ||
64 | }, | ||
65 | downloadEnabled: { | ||
66 | '@type': 'sc:Boolean', | ||
67 | '@id': 'pt:downloadEnabled' | ||
68 | }, | ||
69 | waitTranscoding: { | ||
70 | '@type': 'sc:Boolean', | ||
71 | '@id': 'pt:waitTranscoding' | ||
72 | }, | ||
73 | support: { | ||
74 | '@type': 'sc:Text', | ||
75 | '@id': 'pt:support' | ||
76 | }, | ||
77 | likes: { | ||
78 | '@id': 'as:likes', | ||
79 | '@type': '@id' | ||
80 | }, | ||
81 | dislikes: { | ||
82 | '@id': 'as:dislikes', | ||
83 | '@type': '@id' | ||
84 | }, | ||
85 | playlists: { | ||
86 | '@id': 'pt:playlists', | ||
87 | '@type': '@id' | ||
88 | }, | ||
89 | shares: { | ||
90 | '@id': 'as:shares', | ||
91 | '@type': '@id' | ||
92 | }, | ||
93 | comments: { | ||
94 | '@id': 'as:comments', | ||
95 | '@type': '@id' | ||
96 | } | ||
97 | }) | ||
98 | } | 117 | } |
118 | } | ||
99 | 119 | ||
100 | return Object.assign({}, data, { | 120 | function activityPubContextify <T> (data: T, type: ContextType = 'All') { |
101 | '@context': [ | 121 | return Object.assign({}, data, getContextData(type)) |
102 | 'https://www.w3.org/ns/activitystreams', | ||
103 | 'https://w3id.org/security/v1', | ||
104 | base | ||
105 | ] | ||
106 | }) | ||
107 | } | 122 | } |
108 | 123 | ||
109 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | 124 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |