]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as request from 'request' |
69818c93 | 2 | import * as Sequelize from 'sequelize' |
6fcd19ba | 3 | import * as Promise from 'bluebird' |
f981dae8 | 4 | import { join } from 'path' |
9f10b292 | 5 | |
e02643f3 | 6 | import { database as db } from '../initializers/database' |
65fcc311 C |
7 | import { |
8 | API_VERSION, | |
9 | CONFIG, | |
10 | REQUESTS_IN_PARALLEL, | |
11 | REQUEST_ENDPOINTS, | |
12 | REQUEST_ENDPOINT_ACTIONS, | |
f981dae8 C |
13 | REMOTE_SCHEME, |
14 | STATIC_PATHS | |
65fcc311 C |
15 | } from '../initializers' |
16 | import { | |
17 | logger, | |
18 | getMyPublicCert, | |
19 | makeSecureRequest, | |
6fcd19ba | 20 | makeRetryRequest |
65fcc311 C |
21 | } from '../helpers' |
22 | import { | |
23 | RequestScheduler, | |
69818c93 C |
24 | RequestSchedulerOptions, |
25 | ||
65fcc311 | 26 | RequestVideoQaduScheduler, |
69818c93 C |
27 | RequestVideoQaduSchedulerOptions, |
28 | ||
29 | RequestVideoEventScheduler, | |
30 | RequestVideoEventSchedulerOptions | |
65fcc311 | 31 | } from './request' |
ee9e7b61 C |
32 | import { |
33 | PodInstance, | |
34 | VideoInstance | |
35 | } from '../models' | |
36 | import { | |
37 | RequestEndpoint, | |
38 | RequestVideoEventType, | |
4771e000 C |
39 | RequestVideoQaduType, |
40 | RemoteVideoCreateData, | |
41 | RemoteVideoUpdateData, | |
42 | RemoteVideoRemoveData, | |
e6d4b0ff C |
43 | RemoteVideoReportAbuseData, |
44 | ResultList, | |
0aef76c4 | 45 | Pod as FormattedPod |
ee9e7b61 | 46 | } from '../../shared' |
69818c93 | 47 | |
0a6658fd C |
48 | type QaduParam = { videoId: number, type: RequestVideoQaduType } |
49 | type EventParam = { videoId: number, type: RequestVideoEventType } | |
65fcc311 C |
50 | |
51 | const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] | |
9e167724 | 52 | |
fe783f6b | 53 | const requestScheduler = new RequestScheduler() |
99fdec46 C |
54 | const requestVideoQaduScheduler = new RequestVideoQaduScheduler() |
55 | const requestVideoEventScheduler = new RequestVideoEventScheduler() | |
da691c46 | 56 | |
65fcc311 | 57 | function activateSchedulers () { |
c1a7ab7f | 58 | requestScheduler.activate() |
99fdec46 C |
59 | requestVideoQaduScheduler.activate() |
60 | requestVideoEventScheduler.activate() | |
c1a7ab7f C |
61 | } |
62 | ||
4771e000 | 63 | function addVideoToFriends (videoData: RemoteVideoCreateData, transaction: Sequelize.Transaction) { |
ed04d94f | 64 | const options = { |
da691c46 | 65 | type: ENDPOINT_ACTIONS.ADD, |
65fcc311 | 66 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
ed04d94f C |
67 | data: videoData, |
68 | transaction | |
69 | } | |
6fcd19ba | 70 | return createRequest(options) |
9f10b292 C |
71 | } |
72 | ||
4771e000 | 73 | function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Sequelize.Transaction) { |
ed04d94f | 74 | const options = { |
da691c46 | 75 | type: ENDPOINT_ACTIONS.UPDATE, |
65fcc311 | 76 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
ed04d94f C |
77 | data: videoData, |
78 | transaction | |
79 | } | |
6fcd19ba | 80 | return createRequest(options) |
55fa55a9 C |
81 | } |
82 | ||
91f6f169 | 83 | function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) { |
ed04d94f | 84 | const options = { |
da691c46 | 85 | type: ENDPOINT_ACTIONS.REMOVE, |
65fcc311 | 86 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
69818c93 | 87 | data: videoParams, |
91f6f169 | 88 | transaction |
ed04d94f | 89 | } |
6fcd19ba | 90 | return createRequest(options) |
55fa55a9 C |
91 | } |
92 | ||
4771e000 | 93 | function reportAbuseVideoToFriend (reportData: RemoteVideoReportAbuseData, video: VideoInstance, transaction: Sequelize.Transaction) { |
bd14d16a | 94 | const options = { |
da691c46 | 95 | type: ENDPOINT_ACTIONS.REPORT_ABUSE, |
65fcc311 | 96 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
bd14d16a | 97 | data: reportData, |
69818c93 | 98 | toIds: [ video.Author.podId ], |
6fcd19ba | 99 | transaction |
bd14d16a | 100 | } |
6fcd19ba | 101 | return createRequest(options) |
7b1f49de C |
102 | } |
103 | ||
6fcd19ba | 104 | function quickAndDirtyUpdateVideoToFriends (qaduParam: QaduParam, transaction?: Sequelize.Transaction) { |
9e167724 | 105 | const options = { |
69818c93 C |
106 | videoId: qaduParam.videoId, |
107 | type: qaduParam.type, | |
9e167724 C |
108 | transaction |
109 | } | |
6fcd19ba | 110 | return createVideoQaduRequest(options) |
9e167724 C |
111 | } |
112 | ||
6fcd19ba | 113 | function quickAndDirtyUpdatesVideoToFriends (qadusParams: QaduParam[], transaction: Sequelize.Transaction) { |
d38b8281 C |
114 | const tasks = [] |
115 | ||
075f16ca | 116 | qadusParams.forEach(qaduParams => { |
6fcd19ba | 117 | tasks.push(quickAndDirtyUpdateVideoToFriends(qaduParams, transaction)) |
d38b8281 C |
118 | }) |
119 | ||
6fcd19ba | 120 | return Promise.all(tasks) |
d38b8281 C |
121 | } |
122 | ||
6fcd19ba | 123 | function addEventToRemoteVideo (eventParam: EventParam, transaction?: Sequelize.Transaction) { |
e4c87ec2 | 124 | const options = { |
69818c93 C |
125 | videoId: eventParam.videoId, |
126 | type: eventParam.type, | |
e4c87ec2 C |
127 | transaction |
128 | } | |
6fcd19ba | 129 | return createVideoEventRequest(options) |
e4c87ec2 C |
130 | } |
131 | ||
6fcd19ba | 132 | function addEventsToRemoteVideo (eventsParams: EventParam[], transaction: Sequelize.Transaction) { |
d38b8281 C |
133 | const tasks = [] |
134 | ||
075f16ca | 135 | eventsParams.forEach(eventParams => { |
6fcd19ba | 136 | tasks.push(addEventToRemoteVideo(eventParams, transaction)) |
d38b8281 C |
137 | }) |
138 | ||
6fcd19ba | 139 | return Promise.all(tasks) |
d38b8281 C |
140 | } |
141 | ||
6fcd19ba C |
142 | function hasFriends () { |
143 | return db.Pod.countAll().then(count => count !== 0) | |
9f10b292 C |
144 | } |
145 | ||
6fcd19ba | 146 | function makeFriends (hosts: string[]) { |
bc503c2a | 147 | const podsScore = {} |
9f10b292 C |
148 | |
149 | logger.info('Make friends!') | |
6fcd19ba C |
150 | return getMyPublicCert() |
151 | .then(cert => { | |
709756b8 | 152 | return Promise.each(hosts, host => computeForeignPodsList(host, podsScore)).then(() => cert) |
6fcd19ba C |
153 | }) |
154 | .then(cert => { | |
bc503c2a | 155 | logger.debug('Pods scores computed.', { podsScore: podsScore }) |
49abbbbe | 156 | const podsList = computeWinningPods(hosts, podsScore) |
bc503c2a | 157 | logger.debug('Pods that we keep.', { podsToKeep: podsList }) |
9f10b292 | 158 | |
6fcd19ba | 159 | return makeRequestsToWinningPods(cert, podsList) |
c173e565 | 160 | }) |
9f10b292 C |
161 | } |
162 | ||
6fcd19ba | 163 | function quitFriends () { |
9f10b292 | 164 | // Stop pool requests |
c1a7ab7f | 165 | requestScheduler.deactivate() |
9f10b292 | 166 | |
6fcd19ba C |
167 | return requestScheduler.flush() |
168 | .then(() => { | |
169 | return requestVideoQaduScheduler.flush() | |
170 | }) | |
171 | .then(() => { | |
172 | return db.Pod.list() | |
173 | }) | |
174 | .then(pods => { | |
528a9efa | 175 | const requestParams = { |
69818c93 | 176 | method: 'POST' as 'POST', |
65fcc311 | 177 | path: '/api/' + API_VERSION + '/remote/pods/remove', |
65fcc311 | 178 | toPod: null |
c173e565 C |
179 | } |
180 | ||
e7ea2817 | 181 | // Announce we quit them |
528a9efa C |
182 | // We don't care if the request fails |
183 | // The other pod will exclude us automatically after a while | |
6fcd19ba | 184 | return Promise.map(pods, pod => { |
528a9efa | 185 | requestParams.toPod = pod |
709756b8 | 186 | |
6fcd19ba C |
187 | return makeSecureRequest(requestParams) |
188 | }, { concurrency: REQUESTS_IN_PARALLEL }) | |
189 | .then(() => pods) | |
190 | .catch(err => { | |
ad0997ad | 191 | logger.error('Some errors while quitting friends.', err) |
6fcd19ba | 192 | // Don't stop the process |
d592e0a9 | 193 | return pods |
e7ea2817 | 194 | }) |
6fcd19ba C |
195 | }) |
196 | .then(pods => { | |
197 | const tasks = [] | |
198 | pods.forEach(pod => tasks.push(pod.destroy())) | |
e7ea2817 | 199 | |
6fcd19ba C |
200 | return Promise.all(pods) |
201 | }) | |
202 | .then(() => { | |
203 | logger.info('Removed all remote videos.') | |
204 | // Don't forget to re activate the scheduler, even if there was an error | |
205 | return requestScheduler.activate() | |
206 | }) | |
207 | .finally(() => requestScheduler.activate()) | |
9f10b292 | 208 | } |
c173e565 | 209 | |
69818c93 | 210 | function sendOwnedVideosToPod (podId: number) { |
6fcd19ba C |
211 | db.Video.listOwnedAndPopulateAuthorAndTags() |
212 | .then(videosList => { | |
213 | const tasks = [] | |
214 | videosList.forEach(video => { | |
215 | const promise = video.toAddRemoteJSON() | |
216 | .then(remoteVideo => { | |
217 | const options = { | |
218 | type: 'add', | |
219 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | |
220 | data: remoteVideo, | |
221 | toIds: [ podId ], | |
222 | transaction: null | |
223 | } | |
224 | return createRequest(options) | |
225 | }) | |
226 | .catch(err => { | |
ad0997ad | 227 | logger.error('Cannot convert video to remote.', err) |
6fcd19ba C |
228 | // Don't break the process |
229 | return undefined | |
230 | }) | |
231 | ||
232 | tasks.push(promise) | |
528a9efa | 233 | }) |
6fcd19ba C |
234 | |
235 | return Promise.all(tasks) | |
528a9efa | 236 | }) |
9f10b292 | 237 | } |
c173e565 | 238 | |
f981dae8 C |
239 | function fetchRemotePreview (pod: PodInstance, video: VideoInstance) { |
240 | const host = video.Author.Pod.host | |
241 | const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) | |
242 | ||
243 | return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) | |
244 | } | |
245 | ||
d5f5a670 GS |
246 | function removeFriend (pod: PodInstance) { |
247 | const requestParams = { | |
248 | method: 'POST' as 'POST', | |
249 | path: '/api/' + API_VERSION + '/remote/pods/remove', | |
250 | toPod: pod | |
251 | } | |
252 | ||
253 | return makeSecureRequest(requestParams) | |
bda65bdc | 254 | .catch(err => logger.warn('Cannot notify friends %s we are quitting him.', pod.host, err)) |
d5f5a670 | 255 | .then(() => pod.destroy()) |
bda65bdc C |
256 | .then(() => logger.info('Removed friend %s.', pod.host)) |
257 | .catch(err => logger.error('Cannot destroy friend %s.', pod.host, err)) | |
d5f5a670 GS |
258 | } |
259 | ||
99fdec46 C |
260 | function getRequestScheduler () { |
261 | return requestScheduler | |
262 | } | |
263 | ||
264 | function getRequestVideoQaduScheduler () { | |
265 | return requestVideoQaduScheduler | |
266 | } | |
267 | ||
268 | function getRequestVideoEventScheduler () { | |
269 | return requestVideoEventScheduler | |
270 | } | |
271 | ||
9f10b292 | 272 | // --------------------------------------------------------------------------- |
c173e565 | 273 | |
65fcc311 C |
274 | export { |
275 | activateSchedulers, | |
276 | addVideoToFriends, | |
277 | updateVideoToFriends, | |
278 | reportAbuseVideoToFriend, | |
279 | quickAndDirtyUpdateVideoToFriends, | |
280 | quickAndDirtyUpdatesVideoToFriends, | |
281 | addEventToRemoteVideo, | |
282 | addEventsToRemoteVideo, | |
283 | hasFriends, | |
284 | makeFriends, | |
285 | quitFriends, | |
d5f5a670 | 286 | removeFriend, |
65fcc311 C |
287 | removeVideoToFriends, |
288 | sendOwnedVideosToPod, | |
289 | getRequestScheduler, | |
290 | getRequestVideoQaduScheduler, | |
f981dae8 C |
291 | getRequestVideoEventScheduler, |
292 | fetchRemotePreview | |
65fcc311 | 293 | } |
c173e565 | 294 | |
9f10b292 | 295 | // --------------------------------------------------------------------------- |
c173e565 | 296 | |
6fcd19ba C |
297 | function computeForeignPodsList (host: string, podsScore: { [ host: string ]: number }) { |
298 | // TODO: type res | |
e6d4b0ff C |
299 | return getForeignPodsList(host).then(res => { |
300 | const foreignPodsList: { host: string }[] = res.data | |
528a9efa C |
301 | |
302 | // Let's give 1 point to the pod we ask the friends list | |
49abbbbe | 303 | foreignPodsList.push({ host }) |
89d1d8ba | 304 | |
6fcd19ba | 305 | foreignPodsList.forEach(foreignPod => { |
49abbbbe | 306 | const foreignPodHost = foreignPod.host |
89d1d8ba | 307 | |
49abbbbe C |
308 | if (podsScore[foreignPodHost]) podsScore[foreignPodHost]++ |
309 | else podsScore[foreignPodHost] = 1 | |
89d1d8ba | 310 | }) |
cbe2f7c3 | 311 | |
6fcd19ba | 312 | return undefined |
89d1d8ba C |
313 | }) |
314 | } | |
315 | ||
69818c93 | 316 | function computeWinningPods (hosts: string[], podsScore: { [ host: string ]: number }) { |
89d1d8ba C |
317 | // Build the list of pods to add |
318 | // Only add a pod if it exists in more than a half base pods | |
bc503c2a | 319 | const podsList = [] |
49abbbbe | 320 | const baseScore = hosts.length / 2 |
1e4b0080 | 321 | |
6fcd19ba | 322 | Object.keys(podsScore).forEach(podHost => { |
2c49ca42 | 323 | // If the pod is not me and with a good score we add it |
49abbbbe C |
324 | if (isMe(podHost) === false && podsScore[podHost] > baseScore) { |
325 | podsList.push({ host: podHost }) | |
2c49ca42 | 326 | } |
89d1d8ba | 327 | }) |
e7ea2817 | 328 | |
bc503c2a | 329 | return podsList |
89d1d8ba C |
330 | } |
331 | ||
6fcd19ba | 332 | function getForeignPodsList (host: string) { |
0aef76c4 | 333 | return new Promise< ResultList<FormattedPod> >((res, rej) => { |
8a02bd04 | 334 | const path = '/api/' + API_VERSION + '/remote/pods/list' |
c173e565 | 335 | |
8a02bd04 | 336 | request.post(REMOTE_SCHEME.HTTP + '://' + host + path, (err, response, body) => { |
6fcd19ba | 337 | if (err) return rej(err) |
8425cb89 | 338 | |
6fcd19ba C |
339 | try { |
340 | const json = JSON.parse(body) | |
341 | return res(json) | |
342 | } catch (err) { | |
343 | return rej(err) | |
344 | } | |
345 | }) | |
9f10b292 C |
346 | }) |
347 | } | |
89d1d8ba | 348 | |
6fcd19ba | 349 | function makeRequestsToWinningPods (cert: string, podsList: PodInstance[]) { |
89d1d8ba | 350 | // Stop pool requests |
c1a7ab7f | 351 | requestScheduler.deactivate() |
89d1d8ba | 352 | // Flush pool requests |
c1a7ab7f | 353 | requestScheduler.forceSend() |
89d1d8ba | 354 | |
6fcd19ba | 355 | return Promise.map(podsList, pod => { |
528a9efa | 356 | const params = { |
8a02bd04 | 357 | url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/remote/pods/add', |
69818c93 | 358 | method: 'POST' as 'POST', |
528a9efa | 359 | json: { |
65fcc311 C |
360 | host: CONFIG.WEBSERVER.HOST, |
361 | email: CONFIG.ADMIN.EMAIL, | |
528a9efa C |
362 | publicKey: cert |
363 | } | |
89d1d8ba C |
364 | } |
365 | ||
6fcd19ba C |
366 | return makeRetryRequest(params) |
367 | .then(({ response, body }) => { | |
368 | body = body as { cert: string, email: string } | |
369 | ||
370 | if (response.statusCode === 200) { | |
371 | const podObj = db.Pod.build({ host: pod.host, publicKey: body.cert, email: body.email }) | |
372 | return podObj.save() | |
373 | .then(podCreated => { | |
374 | ||
375 | // Add our videos to the request scheduler | |
376 | sendOwnedVideosToPod(podCreated.id) | |
377 | }) | |
378 | .catch(err => { | |
ad0997ad | 379 | logger.error('Cannot add friend %s pod.', pod.host, err) |
6fcd19ba C |
380 | }) |
381 | } else { | |
382 | logger.error('Status not 200 for %s pod.', pod.host) | |
383 | } | |
384 | }) | |
385 | .catch(err => { | |
386 | logger.error('Error with adding %s pod.', pod.host, { error: err.stack }) | |
528a9efa | 387 | // Don't break the process |
6fcd19ba C |
388 | }) |
389 | }, { concurrency: REQUESTS_IN_PARALLEL }) | |
390 | .then(() => logger.debug('makeRequestsToWinningPods finished.')) | |
391 | .finally(() => { | |
528a9efa C |
392 | // Final callback, we've ended all the requests |
393 | // Now we made new friends, we can re activate the pool of requests | |
c1a7ab7f | 394 | requestScheduler.activate() |
89d1d8ba C |
395 | }) |
396 | } | |
00057e85 | 397 | |
55fa55a9 | 398 | // Wrapper that populate "toIds" argument with all our friends if it is not specified |
69818c93 C |
399 | type CreateRequestOptions = { |
400 | type: string | |
ee9e7b61 | 401 | endpoint: RequestEndpoint |
69818c93 C |
402 | data: Object |
403 | toIds?: number[] | |
404 | transaction: Sequelize.Transaction | |
405 | } | |
6fcd19ba C |
406 | function createRequest (options: CreateRequestOptions) { |
407 | if (options.toIds !== undefined) return requestScheduler.createRequest(options as RequestSchedulerOptions) | |
feb4bdfd | 408 | |
55fa55a9 | 409 | // If the "toIds" pods is not specified, we send the request to all our friends |
6fcd19ba | 410 | return db.Pod.listAllIds(options.transaction).then(podIds => { |
ed04d94f | 411 | const newOptions = Object.assign(options, { toIds: podIds }) |
6fcd19ba | 412 | return requestScheduler.createRequest(newOptions) |
00057e85 C |
413 | }) |
414 | } | |
2c49ca42 | 415 | |
6fcd19ba C |
416 | function createVideoQaduRequest (options: RequestVideoQaduSchedulerOptions) { |
417 | return requestVideoQaduScheduler.createRequest(options) | |
9e167724 C |
418 | } |
419 | ||
6fcd19ba C |
420 | function createVideoEventRequest (options: RequestVideoEventSchedulerOptions) { |
421 | return requestVideoEventScheduler.createRequest(options) | |
e4c87ec2 C |
422 | } |
423 | ||
69818c93 | 424 | function isMe (host: string) { |
65fcc311 | 425 | return host === CONFIG.WEBSERVER.HOST |
2c49ca42 | 426 | } |