]>
Commit | Line | Data |
---|---|---|
1 | import { ContextType } from '@shared/models' | |
2 | ||
3 | function activityPubContextify <T> (data: T, type: ContextType) { | |
4 | return { ...getContextData(type), ...data } | |
5 | } | |
6 | ||
7 | // --------------------------------------------------------------------------- | |
8 | ||
9 | export { | |
10 | getContextData, | |
11 | activityPubContextify | |
12 | } | |
13 | ||
14 | // --------------------------------------------------------------------------- | |
15 | ||
16 | type ContextValue = { [ id: string ]: (string | { '@type': string, '@id': string }) } | |
17 | ||
18 | const contextStore: { [ id in ContextType ]: (string | { [ id: string ]: string })[] } = { | |
19 | Video: buildContext({ | |
20 | Hashtag: 'as:Hashtag', | |
21 | uuid: 'sc:identifier', | |
22 | category: 'sc:category', | |
23 | licence: 'sc:license', | |
24 | subtitleLanguage: 'sc:subtitleLanguage', | |
25 | sensitive: 'as:sensitive', | |
26 | language: 'sc:inLanguage', | |
27 | identifier: 'sc:identifier', | |
28 | ||
29 | // TODO: remove in a few versions, introduced in 4.2 | |
30 | icons: 'as:icon', | |
31 | ||
32 | isLiveBroadcast: 'sc:isLiveBroadcast', | |
33 | liveSaveReplay: { | |
34 | '@type': 'sc:Boolean', | |
35 | '@id': 'pt:liveSaveReplay' | |
36 | }, | |
37 | permanentLive: { | |
38 | '@type': 'sc:Boolean', | |
39 | '@id': 'pt:permanentLive' | |
40 | }, | |
41 | latencyMode: { | |
42 | '@type': 'sc:Number', | |
43 | '@id': 'pt:latencyMode' | |
44 | }, | |
45 | ||
46 | Infohash: 'pt:Infohash', | |
47 | ||
48 | originallyPublishedAt: 'sc:datePublished', | |
49 | views: { | |
50 | '@type': 'sc:Number', | |
51 | '@id': 'pt:views' | |
52 | }, | |
53 | state: { | |
54 | '@type': 'sc:Number', | |
55 | '@id': 'pt:state' | |
56 | }, | |
57 | size: { | |
58 | '@type': 'sc:Number', | |
59 | '@id': 'pt:size' | |
60 | }, | |
61 | fps: { | |
62 | '@type': 'sc:Number', | |
63 | '@id': 'pt:fps' | |
64 | }, | |
65 | commentsEnabled: { | |
66 | '@type': 'sc:Boolean', | |
67 | '@id': 'pt:commentsEnabled' | |
68 | }, | |
69 | downloadEnabled: { | |
70 | '@type': 'sc:Boolean', | |
71 | '@id': 'pt:downloadEnabled' | |
72 | }, | |
73 | waitTranscoding: { | |
74 | '@type': 'sc:Boolean', | |
75 | '@id': 'pt:waitTranscoding' | |
76 | }, | |
77 | support: { | |
78 | '@type': 'sc:Text', | |
79 | '@id': 'pt:support' | |
80 | }, | |
81 | likes: { | |
82 | '@id': 'as:likes', | |
83 | '@type': '@id' | |
84 | }, | |
85 | dislikes: { | |
86 | '@id': 'as:dislikes', | |
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 | ||
99 | Playlist: buildContext({ | |
100 | Playlist: 'pt:Playlist', | |
101 | PlaylistElement: 'pt:PlaylistElement', | |
102 | position: { | |
103 | '@type': 'sc:Number', | |
104 | '@id': 'pt:position' | |
105 | }, | |
106 | startTimestamp: { | |
107 | '@type': 'sc:Number', | |
108 | '@id': 'pt:startTimestamp' | |
109 | }, | |
110 | stopTimestamp: { | |
111 | '@type': 'sc:Number', | |
112 | '@id': 'pt:stopTimestamp' | |
113 | }, | |
114 | uuid: 'sc:identifier' | |
115 | }), | |
116 | ||
117 | CacheFile: buildContext({ | |
118 | expires: 'sc:expires', | |
119 | CacheFile: 'pt:CacheFile' | |
120 | }), | |
121 | ||
122 | Flag: buildContext({ | |
123 | Hashtag: 'as:Hashtag' | |
124 | }), | |
125 | ||
126 | Actor: buildContext({ | |
127 | playlists: { | |
128 | '@id': 'pt:playlists', | |
129 | '@type': '@id' | |
130 | }, | |
131 | support: { | |
132 | '@type': 'sc:Text', | |
133 | '@id': 'pt:support' | |
134 | }, | |
135 | ||
136 | // TODO: remove in a few versions, introduced in 4.2 | |
137 | icons: 'as:icon' | |
138 | }), | |
139 | ||
140 | WatchAction: buildContext({ | |
141 | WatchAction: 'sc:WatchAction', | |
142 | startTimestamp: { | |
143 | '@type': 'sc:Number', | |
144 | '@id': 'pt:startTimestamp' | |
145 | }, | |
146 | stopTimestamp: { | |
147 | '@type': 'sc:Number', | |
148 | '@id': 'pt:stopTimestamp' | |
149 | }, | |
150 | watchSection: { | |
151 | '@type': 'sc:Number', | |
152 | '@id': 'pt:stopTimestamp' | |
153 | }, | |
154 | uuid: 'sc:identifier' | |
155 | }), | |
156 | ||
157 | Collection: buildContext(), | |
158 | Follow: buildContext(), | |
159 | Reject: buildContext(), | |
160 | Accept: buildContext(), | |
161 | View: buildContext(), | |
162 | Announce: buildContext(), | |
163 | Comment: buildContext(), | |
164 | Delete: buildContext(), | |
165 | Rate: buildContext() | |
166 | } | |
167 | ||
168 | function getContextData (type: ContextType) { | |
169 | return { | |
170 | '@context': contextStore[type] | |
171 | } | |
172 | } | |
173 | ||
174 | function buildContext (contextValue?: ContextValue) { | |
175 | const baseContext = [ | |
176 | 'https://www.w3.org/ns/activitystreams', | |
177 | 'https://w3id.org/security/v1', | |
178 | { | |
179 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' | |
180 | } | |
181 | ] | |
182 | ||
183 | if (!contextValue) return baseContext | |
184 | ||
185 | return [ | |
186 | ...baseContext, | |
187 | ||
188 | { | |
189 | pt: 'https://joinpeertube.org/ns#', | |
190 | sc: 'http://schema.org/', | |
191 | ||
192 | ...contextValue | |
193 | } | |
194 | ] | |
195 | } |