]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
8fffe21a 1import * as Bluebird from 'bluebird'
7cde3b9c 2import validator from 'validator'
3fd3ab2d 3import { ResultList } from '../../shared/models'
361805c4 4import { Activity } from '../../shared/models/activitypub'
ca6d3622 5import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
f7509cbe 6import { signJsonLDObject } from './peertube-crypto'
8fffe21a 7import { pageToStartAndCount } from './core-utils'
a1587156 8import { URL } from 'url'
26d6bf65 9import { MActor, MVideoAccountLight } from '../types/models'
e307e4fc 10import { ContextType } from '@shared/models/activitypub/context'
598edb8a 11
084a2cd0
C
12function 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 ]
598edb8a 20
084a2cd0
C
21 if (type !== 'View' && type !== 'Announce') {
22 const additional = {
598edb8a 23 pt: 'https://joinpeertube.org/ns#',
084a2cd0
C
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
bb4ba6d9
C
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
084a2cd0 52 Infohash: 'pt:Infohash',
9934b6f3
C
53 Playlist: 'pt:Playlist',
54 PlaylistElement: 'pt:PlaylistElement',
55
084a2cd0
C
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)
598edb8a
C
125 }
126
084a2cd0
C
127 return {
128 '@context': context
129 }
130}
131
132function activityPubContextify <T> (data: T, type: ContextType = 'All') {
133 return Object.assign({}, data, getContextData(type))
e4f97bab
C
134}
135
8fffe21a 136type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
fbc77eb6
RK
137async function activityPubCollectionPagination (
138 baseUrl: string,
139 handler: ActivityPubCollectionPaginationHandler,
140 page?: any,
141 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
142) {
8fffe21a
C
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 {
babecc3c 148 id: baseUrl,
418d092a 149 type: 'OrderedCollectionPage',
8fffe21a 150 totalItems: result.total,
babecc3c 151 first: baseUrl + '?page=1'
8fffe21a 152 }
16b90975 153 }
16b90975 154
fbc77eb6 155 const { start, count } = pageToStartAndCount(page, size)
8fffe21a
C
156 const result = await handler(start, count)
157
c1e791ba
RK
158 let next: string | undefined
159 let prev: string | undefined
e71bcc0f 160
c46edbc2
C
161 // Assert page is a number
162 page = parseInt(page, 10)
163
e71bcc0f 164 // There are more results
fbc77eb6 165 if (result.total > page * size) {
babecc3c 166 next = baseUrl + '?page=' + (page + 1)
e71bcc0f
C
167 }
168
169 if (page > 1) {
babecc3c 170 prev = baseUrl + '?page=' + (page - 1)
e71bcc0f
C
171 }
172
8fffe21a 173 return {
babecc3c 174 id: baseUrl + '?page=' + page,
e71bcc0f
C
175 type: 'OrderedCollectionPage',
176 prev,
177 next,
babecc3c 178 partOf: baseUrl,
8fffe21a
C
179 orderedItems: result.data,
180 totalItems: result.total
e4f97bab
C
181 }
182
e4f97bab
C
183}
184
598edb8a
C
185function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
186 const activity = activityPubContextify(data, contextType)
afffe988 187
f7509cbe 188 return signJsonLDObject(byActor, activity) as Promise<Activity>
afffe988
C
189}
190
848f499d 191function getAPId (activity: string | { id: string }) {
361805c4 192 if (typeof activity === 'string') return activity
6be84cbc 193
361805c4 194 return activity.id
6be84cbc
C
195}
196
5c6d985f 197function checkUrlsSameHost (url1: string, url2: string) {
a1587156
C
198 const idHost = new URL(url1).host
199 const actorHost = new URL(url2).host
5c6d985f
C
200
201 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
6be84cbc
C
202}
203
ca6d3622
C
204function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
205 const host = video.VideoChannel.Account.Actor.Server.host
206
207 return REMOTE_SCHEME.HTTP + '://' + host + path
208}
209
e4f97bab
C
210// ---------------------------------------------------------------------------
211
212export {
5c6d985f 213 checkUrlsSameHost,
848f499d 214 getAPId,
e4f97bab 215 activityPubContextify,
0d0e8dd0 216 activityPubCollectionPagination,
ca6d3622
C
217 buildSignedActivity,
218 buildRemoteVideoBaseUrl
e4f97bab 219}