]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/context.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / context.ts
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 }),
132
133 WatchAction: buildContext({
134 WatchAction: 'sc:WatchAction',
135 startTimestamp: {
136 '@type': 'sc:Number',
137 '@id': 'pt:startTimestamp'
138 },
139 stopTimestamp: {
140 '@type': 'sc:Number',
141 '@id': 'pt:stopTimestamp'
142 },
143 watchSection: {
144 '@type': 'sc:Number',
145 '@id': 'pt:stopTimestamp'
146 },
147 uuid: 'sc:identifier'
148 }),
149
150 Collection: buildContext(),
151 Follow: buildContext(),
152 Reject: buildContext(),
153 Accept: buildContext(),
154 View: buildContext(),
155 Announce: buildContext(),
156 Comment: buildContext(),
157 Delete: buildContext(),
158 Rate: buildContext()
159 }
160
161 function getContextData (type: ContextType) {
162 return {
163 '@context': contextStore[type]
164 }
165 }
166
167 function buildContext (contextValue?: ContextValue) {
168 const baseContext = [
169 'https://www.w3.org/ns/activitystreams',
170 'https://w3id.org/security/v1',
171 {
172 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
173 }
174 ]
175
176 if (!contextValue) return baseContext
177
178 return [
179 ...baseContext,
180
181 {
182 pt: 'https://joinpeertube.org/ns#',
183 sc: 'http://schema.org/',
184
185 ...contextValue
186 }
187 ]
188 }