]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Fix AP audience
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import Bluebird from 'bluebird'
2 import { URL } from 'url'
3 import validator from 'validator'
4 import { ContextType } from '@shared/models/activitypub/context'
5 import { ResultList } from '../../shared/models'
6 import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
7 import { MActor, MVideoWithHost } from '../types/models'
8 import { pageToStartAndCount } from './core-utils'
9 import { signJsonLDObject } from './peertube-crypto'
10
11 function 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 ]
19
20 if (type !== 'View' && type !== 'Announce') {
21 const additional = {
22 pt: 'https://joinpeertube.org/ns#',
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
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
51 Infohash: 'pt:Infohash',
52 Playlist: 'pt:Playlist',
53 PlaylistElement: 'pt:PlaylistElement',
54
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)
124 }
125
126 return {
127 '@context': context
128 }
129 }
130
131 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
132 return Object.assign({}, data, getContextData(type))
133 }
134
135 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
136 async function activityPubCollectionPagination (
137 baseUrl: string,
138 handler: ActivityPubCollectionPaginationHandler,
139 page?: any,
140 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
141 ) {
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 {
147 id: baseUrl,
148 type: 'OrderedCollectionPage',
149 totalItems: result.total,
150 first: baseUrl + '?page=1'
151 }
152 }
153
154 const { start, count } = pageToStartAndCount(page, size)
155 const result = await handler(start, count)
156
157 let next: string | undefined
158 let prev: string | undefined
159
160 // Assert page is a number
161 page = parseInt(page, 10)
162
163 // There are more results
164 if (result.total > page * size) {
165 next = baseUrl + '?page=' + (page + 1)
166 }
167
168 if (page > 1) {
169 prev = baseUrl + '?page=' + (page - 1)
170 }
171
172 return {
173 id: baseUrl + '?page=' + page,
174 type: 'OrderedCollectionPage',
175 prev,
176 next,
177 partOf: baseUrl,
178 orderedItems: result.data,
179 totalItems: result.total
180 }
181
182 }
183
184 function buildSignedActivity <T> (byActor: MActor, data: T, contextType?: ContextType) {
185 const activity = activityPubContextify(data, contextType)
186
187 return signJsonLDObject(byActor, activity)
188 }
189
190 function getAPId (object: string | { id: string }) {
191 if (typeof object === 'string') return object
192
193 return object.id
194 }
195
196 function checkUrlsSameHost (url1: string, url2: string) {
197 const idHost = new URL(url1).host
198 const actorHost = new URL(url2).host
199
200 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
201 }
202
203 function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
204 if (!scheme) scheme = REMOTE_SCHEME.HTTP
205
206 const host = video.VideoChannel.Actor.Server.host
207
208 return scheme + '://' + host + path
209 }
210
211 // ---------------------------------------------------------------------------
212
213 export {
214 checkUrlsSameHost,
215 getAPId,
216 activityPubContextify,
217 activityPubCollectionPagination,
218 buildSignedActivity,
219 buildRemoteVideoBaseUrl
220 }