]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import Bluebird from 'bluebird' |
90a8bd30 | 2 | import { URL } from 'url' |
7cde3b9c | 3 | import validator from 'validator' |
90a8bd30 | 4 | import { ContextType } from '@shared/models/activitypub/context' |
3fd3ab2d | 5 | import { ResultList } from '../../shared/models' |
ca6d3622 | 6 | import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants' |
90a8bd30 | 7 | import { MActor, MVideoWithHost } from '../types/models' |
8fffe21a | 8 | import { pageToStartAndCount } from './core-utils' |
90a8bd30 | 9 | import { signJsonLDObject } from './peertube-crypto' |
598edb8a | 10 | |
084a2cd0 C |
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 | ] | |
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 | ||
d0800f76 | 41 | // TODO: remove in a few versions, introduced in 4.2 |
42 | icons: 'as:icon', | |
43 | ||
bb4ba6d9 C |
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 | }, | |
f443a746 C |
53 | latencyMode: { |
54 | '@type': 'sc:Number', | |
55 | '@id': 'pt:latencyMode' | |
56 | }, | |
bb4ba6d9 | 57 | |
084a2cd0 | 58 | Infohash: 'pt:Infohash', |
9934b6f3 C |
59 | Playlist: 'pt:Playlist', |
60 | PlaylistElement: 'pt:PlaylistElement', | |
61 | ||
084a2cd0 C |
62 | originallyPublishedAt: 'sc:datePublished', |
63 | views: { | |
64 | '@type': 'sc:Number', | |
65 | '@id': 'pt:views' | |
66 | }, | |
67 | state: { | |
68 | '@type': 'sc:Number', | |
69 | '@id': 'pt:state' | |
70 | }, | |
71 | size: { | |
72 | '@type': 'sc:Number', | |
73 | '@id': 'pt:size' | |
74 | }, | |
75 | fps: { | |
76 | '@type': 'sc:Number', | |
77 | '@id': 'pt:fps' | |
78 | }, | |
79 | startTimestamp: { | |
80 | '@type': 'sc:Number', | |
81 | '@id': 'pt:startTimestamp' | |
82 | }, | |
83 | stopTimestamp: { | |
84 | '@type': 'sc:Number', | |
85 | '@id': 'pt:stopTimestamp' | |
86 | }, | |
87 | position: { | |
88 | '@type': 'sc:Number', | |
89 | '@id': 'pt:position' | |
90 | }, | |
91 | commentsEnabled: { | |
92 | '@type': 'sc:Boolean', | |
93 | '@id': 'pt:commentsEnabled' | |
94 | }, | |
95 | downloadEnabled: { | |
96 | '@type': 'sc:Boolean', | |
97 | '@id': 'pt:downloadEnabled' | |
98 | }, | |
99 | waitTranscoding: { | |
100 | '@type': 'sc:Boolean', | |
101 | '@id': 'pt:waitTranscoding' | |
102 | }, | |
103 | support: { | |
104 | '@type': 'sc:Text', | |
105 | '@id': 'pt:support' | |
106 | }, | |
107 | likes: { | |
108 | '@id': 'as:likes', | |
109 | '@type': '@id' | |
110 | }, | |
111 | dislikes: { | |
112 | '@id': 'as:dislikes', | |
113 | '@type': '@id' | |
114 | }, | |
115 | playlists: { | |
116 | '@id': 'pt:playlists', | |
117 | '@type': '@id' | |
118 | }, | |
119 | shares: { | |
120 | '@id': 'as:shares', | |
121 | '@type': '@id' | |
122 | }, | |
123 | comments: { | |
124 | '@id': 'as:comments', | |
125 | '@type': '@id' | |
126 | } | |
127 | }) | |
128 | } | |
129 | ||
130 | context.push(additional) | |
598edb8a C |
131 | } |
132 | ||
084a2cd0 C |
133 | return { |
134 | '@context': context | |
135 | } | |
136 | } | |
137 | ||
138 | function activityPubContextify <T> (data: T, type: ContextType = 'All') { | |
139 | return Object.assign({}, data, getContextData(type)) | |
e4f97bab C |
140 | } |
141 | ||
8fffe21a | 142 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
fbc77eb6 RK |
143 | async function activityPubCollectionPagination ( |
144 | baseUrl: string, | |
145 | handler: ActivityPubCollectionPaginationHandler, | |
146 | page?: any, | |
147 | size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE | |
148 | ) { | |
8fffe21a C |
149 | if (!page || !validator.isInt(page)) { |
150 | // We just display the first page URL, we only need the total items | |
151 | const result = await handler(0, 1) | |
152 | ||
153 | return { | |
babecc3c | 154 | id: baseUrl, |
418d092a | 155 | type: 'OrderedCollectionPage', |
8fffe21a | 156 | totalItems: result.total, |
57e4e1c1 C |
157 | first: result.data.length === 0 |
158 | ? undefined | |
159 | : baseUrl + '?page=1' | |
8fffe21a | 160 | } |
16b90975 | 161 | } |
16b90975 | 162 | |
fbc77eb6 | 163 | const { start, count } = pageToStartAndCount(page, size) |
8fffe21a C |
164 | const result = await handler(start, count) |
165 | ||
c1e791ba RK |
166 | let next: string | undefined |
167 | let prev: string | undefined | |
e71bcc0f | 168 | |
c46edbc2 C |
169 | // Assert page is a number |
170 | page = parseInt(page, 10) | |
171 | ||
e71bcc0f | 172 | // There are more results |
fbc77eb6 | 173 | if (result.total > page * size) { |
babecc3c | 174 | next = baseUrl + '?page=' + (page + 1) |
e71bcc0f C |
175 | } |
176 | ||
177 | if (page > 1) { | |
babecc3c | 178 | prev = baseUrl + '?page=' + (page - 1) |
e71bcc0f C |
179 | } |
180 | ||
8fffe21a | 181 | return { |
babecc3c | 182 | id: baseUrl + '?page=' + page, |
e71bcc0f C |
183 | type: 'OrderedCollectionPage', |
184 | prev, | |
185 | next, | |
babecc3c | 186 | partOf: baseUrl, |
8fffe21a C |
187 | orderedItems: result.data, |
188 | totalItems: result.total | |
e4f97bab C |
189 | } |
190 | ||
e4f97bab C |
191 | } |
192 | ||
db4b15f2 | 193 | function buildSignedActivity <T> (byActor: MActor, data: T, contextType?: ContextType) { |
598edb8a | 194 | const activity = activityPubContextify(data, contextType) |
afffe988 | 195 | |
db4b15f2 | 196 | return signJsonLDObject(byActor, activity) |
afffe988 C |
197 | } |
198 | ||
a2f99b54 C |
199 | function getAPId (object: string | { id: string }) { |
200 | if (typeof object === 'string') return object | |
6be84cbc | 201 | |
a2f99b54 | 202 | return object.id |
6be84cbc C |
203 | } |
204 | ||
5c6d985f | 205 | function checkUrlsSameHost (url1: string, url2: string) { |
a1587156 C |
206 | const idHost = new URL(url1).host |
207 | const actorHost = new URL(url2).host | |
5c6d985f C |
208 | |
209 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
6be84cbc C |
210 | } |
211 | ||
d9a2a031 C |
212 | function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) { |
213 | if (!scheme) scheme = REMOTE_SCHEME.HTTP | |
214 | ||
90a8bd30 | 215 | const host = video.VideoChannel.Actor.Server.host |
ca6d3622 | 216 | |
d9a2a031 | 217 | return scheme + '://' + host + path |
ca6d3622 C |
218 | } |
219 | ||
e4f97bab C |
220 | // --------------------------------------------------------------------------- |
221 | ||
222 | export { | |
5c6d985f | 223 | checkUrlsSameHost, |
848f499d | 224 | getAPId, |
e4f97bab | 225 | activityPubContextify, |
0d0e8dd0 | 226 | activityPubCollectionPagination, |
ca6d3622 C |
227 | buildSignedActivity, |
228 | buildRemoteVideoBaseUrl | |
e4f97bab | 229 | } |