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