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