aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/friends.ts')
-rw-r--r--server/lib/friends.ts94
1 files changed, 57 insertions, 37 deletions
diff --git a/server/lib/friends.ts b/server/lib/friends.ts
index 6b0fbd2bf..e097f9254 100644
--- a/server/lib/friends.ts
+++ b/server/lib/friends.ts
@@ -1,5 +1,6 @@
1import { each, eachLimit, eachSeries, series, waterfall } from 'async' 1import { each, eachLimit, eachSeries, series, waterfall } from 'async'
2import * as request from 'request' 2import * as request from 'request'
3import * as Sequelize from 'sequelize'
3 4
4import { database as db } from '../initializers/database' 5import { database as db } from '../initializers/database'
5import { 6import {
@@ -19,9 +20,18 @@ import {
19} from '../helpers' 20} from '../helpers'
20import { 21import {
21 RequestScheduler, 22 RequestScheduler,
23 RequestSchedulerOptions,
24
22 RequestVideoQaduScheduler, 25 RequestVideoQaduScheduler,
23 RequestVideoEventScheduler 26 RequestVideoQaduSchedulerOptions,
27
28 RequestVideoEventScheduler,
29 RequestVideoEventSchedulerOptions
24} from './request' 30} from './request'
31import { PodInstance, VideoInstance } from '../models'
32
33type QaduParam = { videoId: string, type: string }
34type EventParam = { videoId: string, type: string }
25 35
26const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] 36const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
27 37
@@ -35,7 +45,7 @@ function activateSchedulers () {
35 requestVideoEventScheduler.activate() 45 requestVideoEventScheduler.activate()
36} 46}
37 47
38function addVideoToFriends (videoData, transaction, callback) { 48function addVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
39 const options = { 49 const options = {
40 type: ENDPOINT_ACTIONS.ADD, 50 type: ENDPOINT_ACTIONS.ADD,
41 endpoint: REQUEST_ENDPOINTS.VIDEOS, 51 endpoint: REQUEST_ENDPOINTS.VIDEOS,
@@ -45,7 +55,7 @@ function addVideoToFriends (videoData, transaction, callback) {
45 createRequest(options, callback) 55 createRequest(options, callback)
46} 56}
47 57
48function updateVideoToFriends (videoData, transaction, callback) { 58function updateVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
49 const options = { 59 const options = {
50 type: ENDPOINT_ACTIONS.UPDATE, 60 type: ENDPOINT_ACTIONS.UPDATE,
51 endpoint: REQUEST_ENDPOINTS.VIDEOS, 61 endpoint: REQUEST_ENDPOINTS.VIDEOS,
@@ -55,35 +65,37 @@ function updateVideoToFriends (videoData, transaction, callback) {
55 createRequest(options, callback) 65 createRequest(options, callback)
56} 66}
57 67
58function removeVideoToFriends (videoParams) { 68function removeVideoToFriends (videoParams: Object) {
59 const options = { 69 const options = {
60 type: ENDPOINT_ACTIONS.REMOVE, 70 type: ENDPOINT_ACTIONS.REMOVE,
61 endpoint: REQUEST_ENDPOINTS.VIDEOS, 71 endpoint: REQUEST_ENDPOINTS.VIDEOS,
62 data: videoParams 72 data: videoParams,
73 transaction: null
63 } 74 }
64 createRequest(options) 75 createRequest(options)
65} 76}
66 77
67function reportAbuseVideoToFriend (reportData, video) { 78function reportAbuseVideoToFriend (reportData: Object, video: VideoInstance) {
68 const options = { 79 const options = {
69 type: ENDPOINT_ACTIONS.REPORT_ABUSE, 80 type: ENDPOINT_ACTIONS.REPORT_ABUSE,
70 endpoint: REQUEST_ENDPOINTS.VIDEOS, 81 endpoint: REQUEST_ENDPOINTS.VIDEOS,
71 data: reportData, 82 data: reportData,
72 toIds: [ video.Author.podId ] 83 toIds: [ video.Author.podId ],
84 transaction: null
73 } 85 }
74 createRequest(options) 86 createRequest(options)
75} 87}
76 88
77function quickAndDirtyUpdateVideoToFriends (qaduParams, transaction?, callback?) { 89function quickAndDirtyUpdateVideoToFriends (qaduParam: QaduParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
78 const options = { 90 const options = {
79 videoId: qaduParams.videoId, 91 videoId: qaduParam.videoId,
80 type: qaduParams.type, 92 type: qaduParam.type,
81 transaction 93 transaction
82 } 94 }
83 return createVideoQaduRequest(options, callback) 95 return createVideoQaduRequest(options, callback)
84} 96}
85 97
86function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCallback) { 98function quickAndDirtyUpdatesVideoToFriends (qadusParams: QaduParam[], transaction: Sequelize.Transaction, finalCallback: (err: Error) => void) {
87 const tasks = [] 99 const tasks = []
88 100
89 qadusParams.forEach(function (qaduParams) { 101 qadusParams.forEach(function (qaduParams) {
@@ -97,16 +109,16 @@ function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCall
97 series(tasks, finalCallback) 109 series(tasks, finalCallback)
98} 110}
99 111
100function addEventToRemoteVideo (eventParams, transaction?, callback?) { 112function addEventToRemoteVideo (eventParam: EventParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
101 const options = { 113 const options = {
102 videoId: eventParams.videoId, 114 videoId: eventParam.videoId,
103 type: eventParams.type, 115 type: eventParam.type,
104 transaction 116 transaction
105 } 117 }
106 createVideoEventRequest(options, callback) 118 createVideoEventRequest(options, callback)
107} 119}
108 120
109function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) { 121function addEventsToRemoteVideo (eventsParams: EventParam[], transaction: Sequelize.Transaction, finalCallback: (err: Error) => void) {
110 const tasks = [] 122 const tasks = []
111 123
112 eventsParams.forEach(function (eventParams) { 124 eventsParams.forEach(function (eventParams) {
@@ -120,7 +132,7 @@ function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) {
120 series(tasks, finalCallback) 132 series(tasks, finalCallback)
121} 133}
122 134
123function hasFriends (callback) { 135function hasFriends (callback: (err: Error, hasFriends?: boolean) => void) {
124 db.Pod.countAll(function (err, count) { 136 db.Pod.countAll(function (err, count) {
125 if (err) return callback(err) 137 if (err) return callback(err)
126 138
@@ -129,7 +141,7 @@ function hasFriends (callback) {
129 }) 141 })
130} 142}
131 143
132function makeFriends (hosts, callback) { 144function makeFriends (hosts: string[], callback: (err: Error) => void) {
133 const podsScore = {} 145 const podsScore = {}
134 146
135 logger.info('Make friends!') 147 logger.info('Make friends!')
@@ -141,7 +153,7 @@ function makeFriends (hosts, callback) {
141 153
142 eachSeries(hosts, function (host, callbackEach) { 154 eachSeries(hosts, function (host, callbackEach) {
143 computeForeignPodsList(host, podsScore, callbackEach) 155 computeForeignPodsList(host, podsScore, callbackEach)
144 }, function (err) { 156 }, function (err: Error) {
145 if (err) return callback(err) 157 if (err) return callback(err)
146 158
147 logger.debug('Pods scores computed.', { podsScore: podsScore }) 159 logger.debug('Pods scores computed.', { podsScore: podsScore })
@@ -153,7 +165,7 @@ function makeFriends (hosts, callback) {
153 }) 165 })
154} 166}
155 167
156function quitFriends (callback) { 168function quitFriends (callback: (err: Error) => void) {
157 // Stop pool requests 169 // Stop pool requests
158 requestScheduler.deactivate() 170 requestScheduler.deactivate()
159 171
@@ -172,7 +184,7 @@ function quitFriends (callback) {
172 184
173 function announceIQuitMyFriends (pods, callbackAsync) { 185 function announceIQuitMyFriends (pods, callbackAsync) {
174 const requestParams = { 186 const requestParams = {
175 method: 'POST', 187 method: 'POST' as 'POST',
176 path: '/api/' + API_VERSION + '/remote/pods/remove', 188 path: '/api/' + API_VERSION + '/remote/pods/remove',
177 sign: true, 189 sign: true,
178 toPod: null 190 toPod: null
@@ -199,7 +211,7 @@ function quitFriends (callback) {
199 pod.destroy().asCallback(callbackEach) 211 pod.destroy().asCallback(callbackEach)
200 }, callbackAsync) 212 }, callbackAsync)
201 } 213 }
202 ], function (err) { 214 ], function (err: Error) {
203 // Don't forget to re activate the scheduler, even if there was an error 215 // Don't forget to re activate the scheduler, even if there was an error
204 requestScheduler.activate() 216 requestScheduler.activate()
205 217
@@ -210,7 +222,7 @@ function quitFriends (callback) {
210 }) 222 })
211} 223}
212 224
213function sendOwnedVideosToPod (podId) { 225function sendOwnedVideosToPod (podId: number) {
214 db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) { 226 db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
215 if (err) { 227 if (err) {
216 logger.error('Cannot get the list of videos we own.') 228 logger.error('Cannot get the list of videos we own.')
@@ -229,7 +241,8 @@ function sendOwnedVideosToPod (podId) {
229 type: 'add', 241 type: 'add',
230 endpoint: REQUEST_ENDPOINTS.VIDEOS, 242 endpoint: REQUEST_ENDPOINTS.VIDEOS,
231 data: remoteVideo, 243 data: remoteVideo,
232 toIds: [ podId ] 244 toIds: [ podId ],
245 transaction: null
233 } 246 }
234 createRequest(options) 247 createRequest(options)
235 }) 248 })
@@ -272,7 +285,7 @@ export {
272 285
273// --------------------------------------------------------------------------- 286// ---------------------------------------------------------------------------
274 287
275function computeForeignPodsList (host, podsScore, callback) { 288function computeForeignPodsList (host: string, podsScore: { [ host: string ]: number }, callback: (err: Error) => void) {
276 getForeignPodsList(host, function (err, res) { 289 getForeignPodsList(host, function (err, res) {
277 if (err) return callback(err) 290 if (err) return callback(err)
278 291
@@ -288,11 +301,11 @@ function computeForeignPodsList (host, podsScore, callback) {
288 else podsScore[foreignPodHost] = 1 301 else podsScore[foreignPodHost] = 1
289 }) 302 })
290 303
291 return callback() 304 return callback(null)
292 }) 305 })
293} 306}
294 307
295function computeWinningPods (hosts, podsScore) { 308function computeWinningPods (hosts: string[], podsScore: { [ host: string ]: number }) {
296 // Build the list of pods to add 309 // Build the list of pods to add
297 // Only add a pod if it exists in more than a half base pods 310 // Only add a pod if it exists in more than a half base pods
298 const podsList = [] 311 const podsList = []
@@ -308,7 +321,7 @@ function computeWinningPods (hosts, podsScore) {
308 return podsList 321 return podsList
309} 322}
310 323
311function getForeignPodsList (host, callback) { 324function getForeignPodsList (host: string, callback: (err: Error, foreignPodsList?: any) => void) {
312 const path = '/api/' + API_VERSION + '/pods' 325 const path = '/api/' + API_VERSION + '/pods'
313 326
314 request.get(REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) { 327 request.get(REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) {
@@ -323,16 +336,16 @@ function getForeignPodsList (host, callback) {
323 }) 336 })
324} 337}
325 338
326function makeRequestsToWinningPods (cert, podsList, callback) { 339function makeRequestsToWinningPods (cert: string, podsList: PodInstance[], callback: (err: Error) => void) {
327 // Stop pool requests 340 // Stop pool requests
328 requestScheduler.deactivate() 341 requestScheduler.deactivate()
329 // Flush pool requests 342 // Flush pool requests
330 requestScheduler.forceSend() 343 requestScheduler.forceSend()
331 344
332 eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: { host: string }, callbackEach) { 345 eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: PodInstance, callbackEach) {
333 const params = { 346 const params = {
334 url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/pods/', 347 url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/pods/',
335 method: 'POST', 348 method: 'POST' as 'POST',
336 json: { 349 json: {
337 host: CONFIG.WEBSERVER.HOST, 350 host: CONFIG.WEBSERVER.HOST,
338 email: CONFIG.ADMIN.EMAIL, 351 email: CONFIG.ADMIN.EMAIL,
@@ -371,15 +384,22 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
371 requestScheduler.activate() 384 requestScheduler.activate()
372 385
373 logger.debug('makeRequestsToWinningPods finished.') 386 logger.debug('makeRequestsToWinningPods finished.')
374 return callback() 387 return callback(null)
375 }) 388 })
376} 389}
377 390
378// Wrapper that populate "toIds" argument with all our friends if it is not specified 391// Wrapper that populate "toIds" argument with all our friends if it is not specified
379// { type, endpoint, data, toIds, transaction } 392type CreateRequestOptions = {
380function createRequest (options, callback?) { 393 type: string
394 endpoint: string
395 data: Object
396 toIds?: number[]
397 transaction: Sequelize.Transaction
398}
399function createRequest (options: CreateRequestOptions, callback?: (err: Error) => void) {
381 if (!callback) callback = function () { /* empty */ } 400 if (!callback) callback = function () { /* empty */ }
382 if (options.toIds) return requestScheduler.createRequest(options, callback) 401
402 if (options.toIds !== undefined) return requestScheduler.createRequest(options as RequestSchedulerOptions, callback)
383 403
384 // If the "toIds" pods is not specified, we send the request to all our friends 404 // If the "toIds" pods is not specified, we send the request to all our friends
385 db.Pod.listAllIds(options.transaction, function (err, podIds) { 405 db.Pod.listAllIds(options.transaction, function (err, podIds) {
@@ -393,18 +413,18 @@ function createRequest (options, callback?) {
393 }) 413 })
394} 414}
395 415
396function createVideoQaduRequest (options, callback) { 416function createVideoQaduRequest (options: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
397 if (!callback) callback = createEmptyCallback() 417 if (!callback) callback = createEmptyCallback()
398 418
399 requestVideoQaduScheduler.createRequest(options, callback) 419 requestVideoQaduScheduler.createRequest(options, callback)
400} 420}
401 421
402function createVideoEventRequest (options, callback) { 422function createVideoEventRequest (options: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
403 if (!callback) callback = createEmptyCallback() 423 if (!callback) callback = createEmptyCallback()
404 424
405 requestVideoEventScheduler.createRequest(options, callback) 425 requestVideoEventScheduler.createRequest(options, callback)
406} 426}
407 427
408function isMe (host) { 428function isMe (host: string) {
409 return host === CONFIG.WEBSERVER.HOST 429 return host === CONFIG.WEBSERVER.HOST
410} 430}