]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
1188d6cf9fd46e3843d46ac7b4e3e5539d868077
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import * as Bluebird from 'bluebird'
2 import validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
6 import { signJsonLDObject } from './peertube-crypto'
7 import { pageToStartAndCount } from './core-utils'
8 import { URL } from 'url'
9 import { MActor, MVideoAccountLight } from '../types/models'
10 import { ContextType } from '@shared/models/activitypub/context'
11
12 function getContextData (type: ContextType) {
13 const context: any[] = [
14 'https://www.w3.org/ns/activitystreams',
15 'https://w3id.org/security/v1',
16 {
17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
18 }
19 ]
20
21 if (type !== 'View' && type !== 'Announce') {
22 const additional = {
23 pt: 'https://joinpeertube.org/ns#',
24 sc: 'http://schema.org#'
25 }
26
27 if (type === 'CacheFile') {
28 Object.assign(additional, {
29 expires: 'sc:expires',
30 CacheFile: 'pt:CacheFile'
31 })
32 } else {
33 Object.assign(additional, {
34 Hashtag: 'as:Hashtag',
35 uuid: 'sc:identifier',
36 category: 'sc:category',
37 licence: 'sc:license',
38 subtitleLanguage: 'sc:subtitleLanguage',
39 sensitive: 'as:sensitive',
40 language: 'sc:inLanguage',
41
42 isLiveBroadcast: 'sc:isLiveBroadcast',
43 liveSaveReplay: {
44 '@type': 'sc:Boolean',
45 '@id': 'pt:liveSaveReplay'
46 },
47 permanentLive: {
48 '@type': 'sc:Boolean',
49 '@id': 'pt:permanentLive'
50 },
51
52 Infohash: 'pt:Infohash',
53 Playlist: 'pt:Playlist',
54 PlaylistElement: 'pt:PlaylistElement',
55
56 originallyPublishedAt: 'sc:datePublished',
57 views: {
58 '@type': 'sc:Number',
59 '@id': 'pt:views'
60 },
61 state: {
62 '@type': 'sc:Number',
63 '@id': 'pt:state'
64 },
65 size: {
66 '@type': 'sc:Number',
67 '@id': 'pt:size'
68 },
69 fps: {
70 '@type': 'sc:Number',
71 '@id': 'pt:fps'
72 },
73 startTimestamp: {
74 '@type': 'sc:Number',
75 '@id': 'pt:startTimestamp'
76 },
77 stopTimestamp: {
78 '@type': 'sc:Number',
79 '@id': 'pt:stopTimestamp'
80 },
81 position: {
82 '@type': 'sc:Number',
83 '@id': 'pt:position'
84 },
85 commentsEnabled: {
86 '@type': 'sc:Boolean',
87 '@id': 'pt:commentsEnabled'
88 },
89 downloadEnabled: {
90 '@type': 'sc:Boolean',
91 '@id': 'pt:downloadEnabled'
92 },
93 waitTranscoding: {
94 '@type': 'sc:Boolean',
95 '@id': 'pt:waitTranscoding'
96 },
97 support: {
98 '@type': 'sc:Text',
99 '@id': 'pt:support'
100 },
101 likes: {
102 '@id': 'as:likes',
103 '@type': '@id'
104 },
105 dislikes: {
106 '@id': 'as:dislikes',
107 '@type': '@id'
108 },
109 playlists: {
110 '@id': 'pt:playlists',
111 '@type': '@id'
112 },
113 shares: {
114 '@id': 'as:shares',
115 '@type': '@id'
116 },
117 comments: {
118 '@id': 'as:comments',
119 '@type': '@id'
120 }
121 })
122 }
123
124 context.push(additional)
125 }
126
127 return {
128 '@context': context
129 }
130 }
131
132 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
133 return Object.assign({}, data, getContextData(type))
134 }
135
136 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
137 async function activityPubCollectionPagination (
138 baseUrl: string,
139 handler: ActivityPubCollectionPaginationHandler,
140 page?: any,
141 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
142 ) {
143 if (!page || !validator.isInt(page)) {
144 // We just display the first page URL, we only need the total items
145 const result = await handler(0, 1)
146
147 return {
148 id: baseUrl,
149 type: 'OrderedCollectionPage',
150 totalItems: result.total,
151 first: baseUrl + '?page=1'
152 }
153 }
154
155 const { start, count } = pageToStartAndCount(page, size)
156 const result = await handler(start, count)
157
158 let next: string | undefined
159 let prev: string | undefined
160
161 // Assert page is a number
162 page = parseInt(page, 10)
163
164 // There are more results
165 if (result.total > page * size) {
166 next = baseUrl + '?page=' + (page + 1)
167 }
168
169 if (page > 1) {
170 prev = baseUrl + '?page=' + (page - 1)
171 }
172
173 return {
174 id: baseUrl + '?page=' + page,
175 type: 'OrderedCollectionPage',
176 prev,
177 next,
178 partOf: baseUrl,
179 orderedItems: result.data,
180 totalItems: result.total
181 }
182
183 }
184
185 function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
186 const activity = activityPubContextify(data, contextType)
187
188 return signJsonLDObject(byActor, activity) as Promise<Activity>
189 }
190
191 function getAPId (activity: string | { id: string }) {
192 if (typeof activity === 'string') return activity
193
194 return activity.id
195 }
196
197 function checkUrlsSameHost (url1: string, url2: string) {
198 const idHost = new URL(url1).host
199 const actorHost = new URL(url2).host
200
201 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
202 }
203
204 function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
205 const host = video.VideoChannel.Account.Actor.Server.host
206
207 return REMOTE_SCHEME.HTTP + '://' + host + path
208 }
209
210 // ---------------------------------------------------------------------------
211
212 export {
213 checkUrlsSameHost,
214 getAPId,
215 activityPubContextify,
216 activityPubCollectionPagination,
217 buildSignedActivity,
218 buildRemoteVideoBaseUrl
219 }