]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/context.ts
3bc40e2aa2749114f9786d0b40a76948776bfd03
[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 = {
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
28 // TODO: remove in a few versions, introduced in 4.2
29 icons: 'as:icon',
30
31 isLiveBroadcast: 'sc:isLiveBroadcast',
32 liveSaveReplay: {
33 '@type': 'sc:Boolean',
34 '@id': 'pt:liveSaveReplay'
35 },
36 permanentLive: {
37 '@type': 'sc:Boolean',
38 '@id': 'pt:permanentLive'
39 },
40 latencyMode: {
41 '@type': 'sc:Number',
42 '@id': 'pt:latencyMode'
43 },
44
45 Infohash: 'pt:Infohash',
46
47 originallyPublishedAt: 'sc:datePublished',
48 views: {
49 '@type': 'sc:Number',
50 '@id': 'pt:views'
51 },
52 state: {
53 '@type': 'sc:Number',
54 '@id': 'pt:state'
55 },
56 size: {
57 '@type': 'sc:Number',
58 '@id': 'pt:size'
59 },
60 fps: {
61 '@type': 'sc:Number',
62 '@id': 'pt:fps'
63 },
64 commentsEnabled: {
65 '@type': 'sc:Boolean',
66 '@id': 'pt:commentsEnabled'
67 },
68 downloadEnabled: {
69 '@type': 'sc:Boolean',
70 '@id': 'pt:downloadEnabled'
71 },
72 waitTranscoding: {
73 '@type': 'sc:Boolean',
74 '@id': 'pt:waitTranscoding'
75 },
76 support: {
77 '@type': 'sc:Text',
78 '@id': 'pt:support'
79 },
80 likes: {
81 '@id': 'as:likes',
82 '@type': '@id'
83 },
84 dislikes: {
85 '@id': 'as:dislikes',
86 '@type': '@id'
87 },
88 shares: {
89 '@id': 'as:shares',
90 '@type': '@id'
91 },
92 comments: {
93 '@id': 'as:comments',
94 '@type': '@id'
95 }
96 }),
97
98 Playlist: buildContext({
99 Playlist: 'pt:Playlist',
100 PlaylistElement: 'pt:PlaylistElement',
101 position: {
102 '@type': 'sc:Number',
103 '@id': 'pt:position'
104 },
105 startTimestamp: {
106 '@type': 'sc:Number',
107 '@id': 'pt:startTimestamp'
108 },
109 stopTimestamp: {
110 '@type': 'sc:Number',
111 '@id': 'pt:stopTimestamp'
112 }
113 }),
114
115 CacheFile: buildContext({
116 expires: 'sc:expires',
117 CacheFile: 'pt:CacheFile'
118 }),
119
120 Flag: buildContext({
121 Hashtag: 'as:Hashtag'
122 }),
123
124 Actor: buildContext({
125 playlists: {
126 '@id': 'pt:playlists',
127 '@type': '@id'
128 }
129 }),
130
131 Follow: buildContext(),
132 Reject: buildContext(),
133 Accept: buildContext(),
134 View: buildContext(),
135 Announce: buildContext(),
136 Comment: buildContext(),
137 Delete: buildContext(),
138 Rate: buildContext()
139 }
140
141 function getContextData (type: ContextType) {
142 return {
143 '@context': contextStore[type]
144 }
145 }
146
147 function buildContext (contextValue?: ContextValue) {
148 const baseContext = [
149 'https://www.w3.org/ns/activitystreams',
150 'https://w3id.org/security/v1',
151 {
152 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
153 }
154 ]
155
156 if (!contextValue) return baseContext
157
158 return [
159 ...baseContext,
160
161 {
162 pt: 'https://joinpeertube.org/ns#',
163 sc: 'http://schema.org#',
164
165 ...contextValue
166 }
167 ]
168 }