]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Translated using Weblate (Vietnamese)
[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 // TODO: remove in a few versions, introduced in 4.2
42 icons: 'as:icon',
43
44 isLiveBroadcast: 'sc:isLiveBroadcast',
45 liveSaveReplay: {
46 '@type': 'sc:Boolean',
47 '@id': 'pt:liveSaveReplay'
48 },
49 permanentLive: {
50 '@type': 'sc:Boolean',
51 '@id': 'pt:permanentLive'
52 },
53
54 Infohash: 'pt:Infohash',
55 Playlist: 'pt:Playlist',
56 PlaylistElement: 'pt:PlaylistElement',
57
58 originallyPublishedAt: 'sc:datePublished',
59 views: {
60 '@type': 'sc:Number',
61 '@id': 'pt:views'
62 },
63 state: {
64 '@type': 'sc:Number',
65 '@id': 'pt:state'
66 },
67 size: {
68 '@type': 'sc:Number',
69 '@id': 'pt:size'
70 },
71 fps: {
72 '@type': 'sc:Number',
73 '@id': 'pt:fps'
74 },
75 startTimestamp: {
76 '@type': 'sc:Number',
77 '@id': 'pt:startTimestamp'
78 },
79 stopTimestamp: {
80 '@type': 'sc:Number',
81 '@id': 'pt:stopTimestamp'
82 },
83 position: {
84 '@type': 'sc:Number',
85 '@id': 'pt:position'
86 },
87 commentsEnabled: {
88 '@type': 'sc:Boolean',
89 '@id': 'pt:commentsEnabled'
90 },
91 downloadEnabled: {
92 '@type': 'sc:Boolean',
93 '@id': 'pt:downloadEnabled'
94 },
95 waitTranscoding: {
96 '@type': 'sc:Boolean',
97 '@id': 'pt:waitTranscoding'
98 },
99 support: {
100 '@type': 'sc:Text',
101 '@id': 'pt:support'
102 },
103 likes: {
104 '@id': 'as:likes',
105 '@type': '@id'
106 },
107 dislikes: {
108 '@id': 'as:dislikes',
109 '@type': '@id'
110 },
111 playlists: {
112 '@id': 'pt:playlists',
113 '@type': '@id'
114 },
115 shares: {
116 '@id': 'as:shares',
117 '@type': '@id'
118 },
119 comments: {
120 '@id': 'as:comments',
121 '@type': '@id'
122 }
123 })
124 }
125
126 context.push(additional)
127 }
128
129 return {
130 '@context': context
131 }
132 }
133
134 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
135 return Object.assign({}, data, getContextData(type))
136 }
137
138 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
139 async function activityPubCollectionPagination (
140 baseUrl: string,
141 handler: ActivityPubCollectionPaginationHandler,
142 page?: any,
143 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
144 ) {
145 if (!page || !validator.isInt(page)) {
146 // We just display the first page URL, we only need the total items
147 const result = await handler(0, 1)
148
149 return {
150 id: baseUrl,
151 type: 'OrderedCollectionPage',
152 totalItems: result.total,
153 first: baseUrl + '?page=1'
154 }
155 }
156
157 const { start, count } = pageToStartAndCount(page, size)
158 const result = await handler(start, count)
159
160 let next: string | undefined
161 let prev: string | undefined
162
163 // Assert page is a number
164 page = parseInt(page, 10)
165
166 // There are more results
167 if (result.total > page * size) {
168 next = baseUrl + '?page=' + (page + 1)
169 }
170
171 if (page > 1) {
172 prev = baseUrl + '?page=' + (page - 1)
173 }
174
175 return {
176 id: baseUrl + '?page=' + page,
177 type: 'OrderedCollectionPage',
178 prev,
179 next,
180 partOf: baseUrl,
181 orderedItems: result.data,
182 totalItems: result.total
183 }
184
185 }
186
187 function buildSignedActivity <T> (byActor: MActor, data: T, contextType?: ContextType) {
188 const activity = activityPubContextify(data, contextType)
189
190 return signJsonLDObject(byActor, activity)
191 }
192
193 function getAPId (object: string | { id: string }) {
194 if (typeof object === 'string') return object
195
196 return object.id
197 }
198
199 function checkUrlsSameHost (url1: string, url2: string) {
200 const idHost = new URL(url1).host
201 const actorHost = new URL(url2).host
202
203 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
204 }
205
206 function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
207 if (!scheme) scheme = REMOTE_SCHEME.HTTP
208
209 const host = video.VideoChannel.Actor.Server.host
210
211 return scheme + '://' + host + path
212 }
213
214 // ---------------------------------------------------------------------------
215
216 export {
217 checkUrlsSameHost,
218 getAPId,
219 activityPubContextify,
220 activityPubCollectionPagination,
221 buildSignedActivity,
222 buildRemoteVideoBaseUrl
223 }