import { each, eachLimit, eachSeries, series, waterfall } from 'async'
-import request = require('request')
+import * as request from 'request'
+import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database'
import {
} from '../helpers'
import {
RequestScheduler,
+ RequestSchedulerOptions,
+
RequestVideoQaduScheduler,
- RequestVideoEventScheduler
+ RequestVideoQaduSchedulerOptions,
+
+ RequestVideoEventScheduler,
+ RequestVideoEventSchedulerOptions
} from './request'
+import {
+ PodInstance,
+ VideoInstance
+} from '../models'
+import {
+ RequestEndpoint,
+ RequestVideoEventType,
+ RequestVideoQaduType
+} from '../../shared'
+
+type QaduParam = { videoId: string, type: RequestVideoQaduType }
+type EventParam = { videoId: string, type: RequestVideoEventType }
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
requestVideoEventScheduler.activate()
}
-function addVideoToFriends (videoData, transaction, callback) {
+function addVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
const options = {
type: ENDPOINT_ACTIONS.ADD,
endpoint: REQUEST_ENDPOINTS.VIDEOS,
createRequest(options, callback)
}
-function updateVideoToFriends (videoData, transaction, callback) {
+function updateVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
const options = {
type: ENDPOINT_ACTIONS.UPDATE,
endpoint: REQUEST_ENDPOINTS.VIDEOS,
createRequest(options, callback)
}
-function removeVideoToFriends (videoParams) {
+function removeVideoToFriends (videoParams: Object) {
const options = {
type: ENDPOINT_ACTIONS.REMOVE,
endpoint: REQUEST_ENDPOINTS.VIDEOS,
- data: videoParams
+ data: videoParams,
+ transaction: null
}
createRequest(options)
}
-function reportAbuseVideoToFriend (reportData, video) {
+function reportAbuseVideoToFriend (reportData: Object, video: VideoInstance) {
const options = {
type: ENDPOINT_ACTIONS.REPORT_ABUSE,
endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: reportData,
- toIds: [ video.Author.podId ]
+ toIds: [ video.Author.podId ],
+ transaction: null
}
createRequest(options)
}
-function quickAndDirtyUpdateVideoToFriends (qaduParams, transaction?, callback?) {
+function quickAndDirtyUpdateVideoToFriends (qaduParam: QaduParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
const options = {
- videoId: qaduParams.videoId,
- type: qaduParams.type,
+ videoId: qaduParam.videoId,
+ type: qaduParam.type,
transaction
}
return createVideoQaduRequest(options, callback)
}
-function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCallback) {
+function quickAndDirtyUpdatesVideoToFriends (
+ qadusParams: QaduParam[],
+ transaction: Sequelize.Transaction,
+ finalCallback: (err: Error) => void
+) {
const tasks = []
qadusParams.forEach(function (qaduParams) {
series(tasks, finalCallback)
}
-function addEventToRemoteVideo (eventParams, transaction?, callback?) {
+function addEventToRemoteVideo (eventParam: EventParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
const options = {
- videoId: eventParams.videoId,
- type: eventParams.type,
+ videoId: eventParam.videoId,
+ type: eventParam.type,
transaction
}
createVideoEventRequest(options, callback)
}
-function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) {
+function addEventsToRemoteVideo (eventsParams: EventParam[], transaction: Sequelize.Transaction, finalCallback: (err: Error) => void) {
const tasks = []
eventsParams.forEach(function (eventParams) {
series(tasks, finalCallback)
}
-function hasFriends (callback) {
+function hasFriends (callback: (err: Error, hasFriends?: boolean) => void) {
db.Pod.countAll(function (err, count) {
if (err) return callback(err)
})
}
-function makeFriends (hosts, callback) {
+function makeFriends (hosts: string[], callback: (err: Error) => void) {
const podsScore = {}
logger.info('Make friends!')
eachSeries(hosts, function (host, callbackEach) {
computeForeignPodsList(host, podsScore, callbackEach)
- }, function (err) {
+ }, function (err: Error) {
if (err) return callback(err)
logger.debug('Pods scores computed.', { podsScore: podsScore })
})
}
-function quitFriends (callback) {
+function quitFriends (callback: (err: Error) => void) {
// Stop pool requests
requestScheduler.deactivate()
function announceIQuitMyFriends (pods, callbackAsync) {
const requestParams = {
- method: 'POST',
+ method: 'POST' as 'POST',
path: '/api/' + API_VERSION + '/remote/pods/remove',
sign: true,
toPod: null
pod.destroy().asCallback(callbackEach)
}, callbackAsync)
}
- ], function (err) {
+ ], function (err: Error) {
// Don't forget to re activate the scheduler, even if there was an error
requestScheduler.activate()
})
}
-function sendOwnedVideosToPod (podId) {
+function sendOwnedVideosToPod (podId: number) {
db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
if (err) {
logger.error('Cannot get the list of videos we own.')
type: 'add',
endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: remoteVideo,
- toIds: [ podId ]
+ toIds: [ podId ],
+ transaction: null
}
createRequest(options)
})
// ---------------------------------------------------------------------------
-function computeForeignPodsList (host, podsScore, callback) {
+function computeForeignPodsList (host: string, podsScore: { [ host: string ]: number }, callback: (err: Error) => void) {
getForeignPodsList(host, function (err, res) {
if (err) return callback(err)
else podsScore[foreignPodHost] = 1
})
- return callback()
+ return callback(null)
})
}
-function computeWinningPods (hosts, podsScore) {
+function computeWinningPods (hosts: string[], podsScore: { [ host: string ]: number }) {
// Build the list of pods to add
// Only add a pod if it exists in more than a half base pods
const podsList = []
return podsList
}
-function getForeignPodsList (host, callback) {
+function getForeignPodsList (host: string, callback: (err: Error, foreignPodsList?: any) => void) {
const path = '/api/' + API_VERSION + '/pods'
request.get(REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) {
})
}
-function makeRequestsToWinningPods (cert, podsList, callback) {
+function makeRequestsToWinningPods (cert: string, podsList: PodInstance[], callback: (err: Error) => void) {
// Stop pool requests
requestScheduler.deactivate()
// Flush pool requests
requestScheduler.forceSend()
- eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: { host: string }, callbackEach) {
+ eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: PodInstance, callbackEach) {
const params = {
url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/pods/',
- method: 'POST',
+ method: 'POST' as 'POST',
json: {
host: CONFIG.WEBSERVER.HOST,
email: CONFIG.ADMIN.EMAIL,
requestScheduler.activate()
logger.debug('makeRequestsToWinningPods finished.')
- return callback()
+ return callback(null)
})
}
// Wrapper that populate "toIds" argument with all our friends if it is not specified
-// { type, endpoint, data, toIds, transaction }
-function createRequest (options, callback?) {
+type CreateRequestOptions = {
+ type: string
+ endpoint: RequestEndpoint
+ data: Object
+ toIds?: number[]
+ transaction: Sequelize.Transaction
+}
+function createRequest (options: CreateRequestOptions, callback?: (err: Error) => void) {
if (!callback) callback = function () { /* empty */ }
- if (options.toIds) return requestScheduler.createRequest(options, callback)
+
+ if (options.toIds !== undefined) return requestScheduler.createRequest(options as RequestSchedulerOptions, callback)
// If the "toIds" pods is not specified, we send the request to all our friends
db.Pod.listAllIds(options.transaction, function (err, podIds) {
})
}
-function createVideoQaduRequest (options, callback) {
+function createVideoQaduRequest (options: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
if (!callback) callback = createEmptyCallback()
requestVideoQaduScheduler.createRequest(options, callback)
}
-function createVideoEventRequest (options, callback) {
+function createVideoEventRequest (options: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
if (!callback) callback = createEmptyCallback()
requestVideoEventScheduler.createRequest(options, callback)
}
-function isMe (host) {
+function isMe (host: string) {
return host === CONFIG.WEBSERVER.HOST
}