Type functions

This commit is contained in:
Chocobozzz 2017-06-10 22:15:25 +02:00
parent 4d4e5cd4dc
commit 69818c9394
96 changed files with 990 additions and 544 deletions

View File

@ -82,8 +82,10 @@
"@types/config": "^0.0.32", "@types/config": "^0.0.32",
"@types/express": "^4.0.35", "@types/express": "^4.0.35",
"@types/lodash": "^4.14.64", "@types/lodash": "^4.14.64",
"@types/magnet-uri": "^5.1.1",
"@types/mkdirp": "^0.3.29", "@types/mkdirp": "^0.3.29",
"@types/morgan": "^1.7.32", "@types/morgan": "^1.7.32",
"@types/multer": "^0.0.34",
"@types/node": "^7.0.18", "@types/node": "^7.0.18",
"@types/request": "^0.0.43", "@types/request": "^0.0.43",
"@types/sequelize": "^4.0.55", "@types/sequelize": "^4.0.55",

View File

@ -1,6 +1,6 @@
import * as express from 'express' import * as express from 'express'
import { CONFIG } from '../../initializers'; import { CONFIG } from '../../initializers'
import { logger } from '../../helpers' import { logger } from '../../helpers'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
@ -9,7 +9,7 @@ const clientsRouter = express.Router()
clientsRouter.get('/local', getLocalClient) clientsRouter.get('/local', getLocalClient)
// Get the client credentials for the PeerTube front end // Get the client credentials for the PeerTube front end
function getLocalClient (req, res, next) { function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
const serverHostname = CONFIG.WEBSERVER.HOSTNAME const serverHostname = CONFIG.WEBSERVER.HOSTNAME
const serverPort = CONFIG.WEBSERVER.PORT const serverPort = CONFIG.WEBSERVER.PORT
let headerHostShouldBe = serverHostname let headerHostShouldBe = serverHostname

View File

@ -7,7 +7,7 @@ const configRouter = express.Router()
configRouter.get('/', getConfig) configRouter.get('/', getConfig)
// Get the client credentials for the PeerTube front end // Get the client credentials for the PeerTube front end
function getConfig (req, res, next) { function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
res.json({ res.json({
signup: { signup: {
enabled: CONFIG.SIGNUP.ENABLED enabled: CONFIG.SIGNUP.ENABLED

View File

@ -28,6 +28,6 @@ export { apiRouter }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function pong (req, res, next) { function pong (req: express.Request, res: express.Response, next: express.NextFunction) {
return res.send('pong').status(200).end() return res.send('pong').status(200).end()
} }

View File

@ -21,6 +21,9 @@ import {
setBodyHostPort, setBodyHostPort,
setBodyHostsPort setBodyHostsPort
} from '../../middlewares' } from '../../middlewares'
import {
PodInstance
} from '../../models'
const podsRouter = express.Router() const podsRouter = express.Router()
@ -51,10 +54,10 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addPods (req, res, next) { function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
const informations = req.body const informations = req.body
waterfall([ waterfall<string, Error>([
function addPod (callback) { function addPod (callback) {
const pod = db.Pod.build(informations) const pod = db.Pod.build(informations)
pod.save().asCallback(function (err, podCreated) { pod.save().asCallback(function (err, podCreated) {
@ -63,7 +66,7 @@ function addPods (req, res, next) {
}) })
}, },
function sendMyVideos (podCreated, callback) { function sendMyVideos (podCreated: PodInstance, callback) {
sendOwnedVideosToPod(podCreated.id) sendOwnedVideosToPod(podCreated.id)
callback(null) callback(null)
@ -86,7 +89,7 @@ function addPods (req, res, next) {
}) })
} }
function listPods (req, res, next) { function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
db.Pod.list(function (err, podsList) { db.Pod.list(function (err, podsList) {
if (err) return next(err) if (err) return next(err)
@ -94,8 +97,8 @@ function listPods (req, res, next) {
}) })
} }
function makeFriendsController (req, res, next) { function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
const hosts = req.body.hosts const hosts = req.body.hosts as string[]
makeFriends(hosts, function (err) { makeFriends(hosts, function (err) {
if (err) { if (err) {
@ -109,7 +112,7 @@ function makeFriendsController (req, res, next) {
res.type('json').status(204).end() res.type('json').status(204).end()
} }
function quitFriendsController (req, res, next) { function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
quitFriends(function (err) { quitFriends(function (err) {
if (err) return next(err) if (err) return next(err)

View File

@ -21,7 +21,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function removePods (req, res, next) { function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
const host = req.body.signature.host const host = req.body.signature.host
waterfall([ waterfall([

View File

@ -1,4 +1,5 @@
import * as express from 'express' import * as express from 'express'
import * as Sequelize from 'sequelize'
import { eachSeries, waterfall } from 'async' import { eachSeries, waterfall } from 'async'
import { database as db } from '../../../initializers/database' import { database as db } from '../../../initializers/database'
@ -23,6 +24,7 @@ import {
startSerializableTransaction startSerializableTransaction
} from '../../../helpers' } from '../../../helpers'
import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib'
import { PodInstance, VideoInstance } from '../../../models'
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
@ -64,7 +66,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function remoteVideos (req, res, next) { function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data const requests = req.body.data
const fromPod = res.locals.secure.pod const fromPod = res.locals.secure.pod
@ -89,7 +91,7 @@ function remoteVideos (req, res, next) {
return res.type('json').status(204).end() return res.type('json').status(204).end()
} }
function remoteVideosQadu (req, res, next) { function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data const requests = req.body.data
const fromPod = res.locals.secure.pod const fromPod = res.locals.secure.pod
@ -104,7 +106,7 @@ function remoteVideosQadu (req, res, next) {
return res.type('json').status(204).end() return res.type('json').status(204).end()
} }
function remoteVideosEvents (req, res, next) { function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data const requests = req.body.data
const fromPod = res.locals.secure.pod const fromPod = res.locals.secure.pod
@ -119,7 +121,7 @@ function remoteVideosEvents (req, res, next) {
return res.type('json').status(204).end() return res.type('json').status(204).end()
} }
function processVideosEventsRetryWrapper (eventData, fromPod, finalCallback) { function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
const options = { const options = {
arguments: [ eventData, fromPod ], arguments: [ eventData, fromPod ],
errorMessage: 'Cannot process videos events with many retries.' errorMessage: 'Cannot process videos events with many retries.'
@ -128,7 +130,7 @@ function processVideosEventsRetryWrapper (eventData, fromPod, finalCallback) {
retryTransactionWrapper(processVideosEvents, options, finalCallback) retryTransactionWrapper(processVideosEvents, options, finalCallback)
} }
function processVideosEvents (eventData, fromPod, finalCallback) { function processVideosEvents (eventData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
waterfall([ waterfall([
startSerializableTransaction, startSerializableTransaction,
@ -187,7 +189,7 @@ function processVideosEvents (eventData, fromPod, finalCallback) {
commitTransaction commitTransaction
], function (err, t) { ], function (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
logger.debug('Cannot process a video event.', { error: err }) logger.debug('Cannot process a video event.', { error: err })
return rollbackTransaction(err, t, finalCallback) return rollbackTransaction(err, t, finalCallback)
@ -198,7 +200,7 @@ function processVideosEvents (eventData, fromPod, finalCallback) {
}) })
} }
function quickAndDirtyUpdateVideoRetryWrapper (videoData, fromPod, finalCallback) { function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
const options = { const options = {
arguments: [ videoData, fromPod ], arguments: [ videoData, fromPod ],
errorMessage: 'Cannot update quick and dirty the remote video with many retries.' errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
@ -207,7 +209,7 @@ function quickAndDirtyUpdateVideoRetryWrapper (videoData, fromPod, finalCallback
retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback) retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback)
} }
function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) { function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
let videoName let videoName
waterfall([ waterfall([
@ -243,7 +245,7 @@ function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) {
commitTransaction commitTransaction
], function (err, t) { ], function (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
logger.debug('Cannot quick and dirty update the remote video.', { error: err }) logger.debug('Cannot quick and dirty update the remote video.', { error: err })
return rollbackTransaction(err, t, finalCallback) return rollbackTransaction(err, t, finalCallback)
@ -255,7 +257,7 @@ function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) {
} }
// Handle retries on fail // Handle retries on fail
function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { function addRemoteVideoRetryWrapper (videoToCreateData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
const options = { const options = {
arguments: [ videoToCreateData, fromPod ], arguments: [ videoToCreateData, fromPod ],
errorMessage: 'Cannot insert the remote video with many retries.' errorMessage: 'Cannot insert the remote video with many retries.'
@ -264,7 +266,7 @@ function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback)
retryTransactionWrapper(addRemoteVideo, options, finalCallback) retryTransactionWrapper(addRemoteVideo, options, finalCallback)
} }
function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
logger.debug('Adding remote video "%s".', videoToCreateData.remoteId) logger.debug('Adding remote video "%s".', videoToCreateData.remoteId)
waterfall([ waterfall([
@ -359,7 +361,7 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
commitTransaction commitTransaction
], function (err, t) { ], function (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
// This is just a debug because we will retry the insert // This is just a debug because we will retry the insert
logger.debug('Cannot insert the remote video.', { error: err }) logger.debug('Cannot insert the remote video.', { error: err })
@ -372,7 +374,7 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
} }
// Handle retries on fail // Handle retries on fail
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
const options = { const options = {
arguments: [ videoAttributesToUpdate, fromPod ], arguments: [ videoAttributesToUpdate, fromPod ],
errorMessage: 'Cannot update the remote video with many retries' errorMessage: 'Cannot update the remote video with many retries'
@ -381,7 +383,7 @@ function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalC
retryTransactionWrapper(updateRemoteVideo, options, finalCallback) retryTransactionWrapper(updateRemoteVideo, options, finalCallback)
} }
function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance, finalCallback: (err: Error) => void) {
logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId) logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId)
waterfall([ waterfall([
@ -435,7 +437,7 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
commitTransaction commitTransaction
], function (err, t) { ], function (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
// This is just a debug because we will retry the insert // This is just a debug because we will retry the insert
logger.debug('Cannot update the remote video.', { error: err }) logger.debug('Cannot update the remote video.', { error: err })
@ -447,7 +449,7 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
}) })
} }
function removeRemoteVideo (videoToRemoveData, fromPod, callback) { function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance, callback: (err: Error) => void) {
// We need the instance because we have to remove some other stuffs (thumbnail etc) // We need the instance because we have to remove some other stuffs (thumbnail etc)
fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) { fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) {
// Do not return the error, continue the process // Do not return the error, continue the process
@ -465,7 +467,7 @@ function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
}) })
} }
function reportAbuseRemoteVideo (reportData, fromPod, callback) { function reportAbuseRemoteVideo (reportData: any, fromPod: PodInstance, callback: (err: Error) => void) {
fetchOwnedVideo(reportData.videoRemoteId, function (err, video) { fetchOwnedVideo(reportData.videoRemoteId, function (err, video) {
if (err || !video) { if (err || !video) {
if (!err) err = new Error('video not found') if (!err) err = new Error('video not found')
@ -494,7 +496,7 @@ function reportAbuseRemoteVideo (reportData, fromPod, callback) {
}) })
} }
function fetchOwnedVideo (id, callback) { function fetchOwnedVideo (id: string, callback: (err: Error, video?: VideoInstance) => void) {
db.Video.load(id, function (err, video) { db.Video.load(id, function (err, video) {
if (err || !video) { if (err || !video) {
if (!err) err = new Error('video not found') if (!err) err = new Error('video not found')
@ -507,7 +509,7 @@ function fetchOwnedVideo (id, callback) {
}) })
} }
function fetchRemoteVideo (podHost, remoteId, callback) { function fetchRemoteVideo (podHost: string, remoteId: string, callback: (err: Error, video?: VideoInstance) => void) {
db.Video.loadByHostAndRemoteId(podHost, remoteId, function (err, video) { db.Video.loadByHostAndRemoteId(podHost, remoteId, function (err, video) {
if (err || !video) { if (err || !video) {
if (!err) err = new Error('video not found') if (!err) err = new Error('video not found')

View File

@ -2,6 +2,7 @@ import * as express from 'express'
import { parallel } from 'async' import { parallel } from 'async'
import { import {
BaseRequestScheduler,
getRequestScheduler, getRequestScheduler,
getRequestVideoQaduScheduler, getRequestVideoQaduScheduler,
getRequestVideoEventScheduler getRequestVideoEventScheduler
@ -24,7 +25,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getStatsRequests (req, res, next) { function getStatsRequests (req: express.Request, res: express.Response, next: express.NextFunction) {
parallel({ parallel({
requestScheduler: buildRequestSchedulerFunction(getRequestScheduler()), requestScheduler: buildRequestSchedulerFunction(getRequestScheduler()),
requestVideoQaduScheduler: buildRequestSchedulerFunction(getRequestVideoQaduScheduler()), requestVideoQaduScheduler: buildRequestSchedulerFunction(getRequestVideoQaduScheduler()),
@ -38,7 +39,7 @@ function getStatsRequests (req, res, next) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function buildRequestSchedulerFunction (requestScheduler) { function buildRequestSchedulerFunction (requestScheduler: BaseRequestScheduler) {
return function (callback) { return function (callback) {
requestScheduler.remainingRequestsCount(function (err, count) { requestScheduler.remainingRequestsCount(function (err, count) {
if (err) return callback(err) if (err) return callback(err)

View File

@ -76,7 +76,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function ensureRegistrationEnabled (req, res, next) { function ensureRegistrationEnabled (req: express.Request, res: express.Response, next: express.NextFunction) {
const registrationEnabled = CONFIG.SIGNUP.ENABLED const registrationEnabled = CONFIG.SIGNUP.ENABLED
if (registrationEnabled === true) { if (registrationEnabled === true) {
@ -86,7 +86,7 @@ function ensureRegistrationEnabled (req, res, next) {
return res.status(400).send('User registration is not enabled.') return res.status(400).send('User registration is not enabled.')
} }
function createUser (req, res, next) { function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = db.User.build({ const user = db.User.build({
username: req.body.username, username: req.body.username,
password: req.body.password, password: req.body.password,
@ -95,14 +95,14 @@ function createUser (req, res, next) {
role: USER_ROLES.USER role: USER_ROLES.USER
}) })
user.save().asCallback(function (err, createdUser) { user.save().asCallback(function (err) {
if (err) return next(err) if (err) return next(err)
return res.type('json').status(204).end() return res.type('json').status(204).end()
}) })
} }
function getUserInformation (req, res, next) { function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
if (err) return next(err) if (err) return next(err)
@ -110,9 +110,9 @@ function getUserInformation (req, res, next) {
}) })
} }
function getUserVideoRating (req, res, next) { function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoId = req.params.videoId const videoId = '' + req.params.videoId
const userId = res.locals.oauth.token.User.id const userId = +res.locals.oauth.token.User.id
db.UserVideoRate.load(userId, videoId, null, function (err, ratingObj) { db.UserVideoRate.load(userId, videoId, null, function (err, ratingObj) {
if (err) return next(err) if (err) return next(err)
@ -126,7 +126,7 @@ function getUserVideoRating (req, res, next) {
}) })
} }
function listUsers (req, res, next) { function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
if (err) return next(err) if (err) return next(err)
@ -134,7 +134,7 @@ function listUsers (req, res, next) {
}) })
} }
function removeUser (req, res, next) { function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
waterfall([ waterfall([
function loadUser (callback) { function loadUser (callback) {
db.User.loadById(req.params.id, callback) db.User.loadById(req.params.id, callback)
@ -153,7 +153,7 @@ function removeUser (req, res, next) {
}) })
} }
function updateUser (req, res, next) { function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
if (err) return next(err) if (err) return next(err)
@ -168,6 +168,6 @@ function updateUser (req, res, next) {
}) })
} }
function success (req, res, next) { function success (req: express.Request, res: express.Response, next: express.NextFunction) {
res.end() res.end()
} }

View File

@ -1,4 +1,5 @@
import * as express from 'express' import * as express from 'express'
import * as Sequelize from 'sequelize'
import { waterfall } from 'async' import { waterfall } from 'async'
import { database as db } from '../../../initializers/database' import { database as db } from '../../../initializers/database'
@ -46,7 +47,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function listVideoAbuses (req, res, next) { function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) { db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) {
if (err) return next(err) if (err) return next(err)
@ -54,7 +55,7 @@ function listVideoAbuses (req, res, next) {
}) })
} }
function reportVideoAbuseRetryWrapper (req, res, next) { function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
const options = { const options = {
arguments: [ req, res ], arguments: [ req, res ],
errorMessage: 'Cannot report abuse to the video with many retries.' errorMessage: 'Cannot report abuse to the video with many retries.'
@ -67,7 +68,7 @@ function reportVideoAbuseRetryWrapper (req, res, next) {
}) })
} }
function reportVideoAbuse (req, res, finalCallback) { function reportVideoAbuse (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
const reporterUsername = res.locals.oauth.token.User.username const reporterUsername = res.locals.oauth.token.User.username
@ -105,7 +106,7 @@ function reportVideoAbuse (req, res, finalCallback) {
commitTransaction commitTransaction
], function andFinally (err, t) { ], function andFinally (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
logger.debug('Cannot update the video.', { error: err }) logger.debug('Cannot update the video.', { error: err })
return rollbackTransaction(err, t, finalCallback) return rollbackTransaction(err, t, finalCallback)

View File

@ -25,7 +25,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addVideoToBlacklist (req, res, next) { function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
const toCreate = { const toCreate = {

View File

@ -1,4 +1,5 @@
import * as express from 'express' import * as express from 'express'
import * as Sequelize from 'sequelize'
import * as fs from 'fs' import * as fs from 'fs'
import * as multer from 'multer' import * as multer from 'multer'
import * as path from 'path' import * as path from 'path'
@ -124,21 +125,21 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function listVideoCategories (req, res, next) { function listVideoCategories (req: express.Request, res: express.Response, next: express.NextFunction) {
res.json(VIDEO_CATEGORIES) res.json(VIDEO_CATEGORIES)
} }
function listVideoLicences (req, res, next) { function listVideoLicences (req: express.Request, res: express.Response, next: express.NextFunction) {
res.json(VIDEO_LICENCES) res.json(VIDEO_LICENCES)
} }
function listVideoLanguages (req, res, next) { function listVideoLanguages (req: express.Request, res: express.Response, next: express.NextFunction) {
res.json(VIDEO_LANGUAGES) res.json(VIDEO_LANGUAGES)
} }
// Wrapper to video add that retry the function if there is a database error // Wrapper to video add that retry the function if there is a database error
// We need this because we run the transaction in SERIALIZABLE isolation that can fail // We need this because we run the transaction in SERIALIZABLE isolation that can fail
function addVideoRetryWrapper (req, res, next) { function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
const options = { const options = {
arguments: [ req, res, req.files.videofile[0] ], arguments: [ req, res, req.files.videofile[0] ],
errorMessage: 'Cannot insert the video with many retries.' errorMessage: 'Cannot insert the video with many retries.'
@ -152,7 +153,7 @@ function addVideoRetryWrapper (req, res, next) {
}) })
} }
function addVideo (req, res, videoFile, finalCallback) { function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File, finalCallback: (err: Error) => void) {
const videoInfos = req.body const videoInfos = req.body
waterfall([ waterfall([
@ -190,7 +191,7 @@ function addVideo (req, res, videoFile, finalCallback) {
language: videoInfos.language, language: videoInfos.language,
nsfw: videoInfos.nsfw, nsfw: videoInfos.nsfw,
description: videoInfos.description, description: videoInfos.description,
duration: videoFile.duration, duration: videoFile['duration'], // duration was added by a previous middleware
authorId: author.id authorId: author.id
} }
@ -254,7 +255,7 @@ function addVideo (req, res, videoFile, finalCallback) {
commitTransaction commitTransaction
], function andFinally (err, t) { ], function andFinally (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
// This is just a debug because we will retry the insert // This is just a debug because we will retry the insert
logger.debug('Cannot insert the video.', { error: err }) logger.debug('Cannot insert the video.', { error: err })
@ -266,7 +267,7 @@ function addVideo (req, res, videoFile, finalCallback) {
}) })
} }
function updateVideoRetryWrapper (req, res, next) { function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
const options = { const options = {
arguments: [ req, res ], arguments: [ req, res ],
errorMessage: 'Cannot update the video with many retries.' errorMessage: 'Cannot update the video with many retries.'
@ -280,7 +281,7 @@ function updateVideoRetryWrapper (req, res, next) {
}) })
} }
function updateVideo (req, res, finalCallback) { function updateVideo (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
const videoFieldsSave = videoInstance.toJSON() const videoFieldsSave = videoInstance.toJSON()
const videoInfosToUpdate = req.body const videoInfosToUpdate = req.body
@ -341,7 +342,7 @@ function updateVideo (req, res, finalCallback) {
commitTransaction commitTransaction
], function andFinally (err, t) { ], function andFinally (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
logger.debug('Cannot update the video.', { error: err }) logger.debug('Cannot update the video.', { error: err })
@ -361,7 +362,7 @@ function updateVideo (req, res, finalCallback) {
}) })
} }
function getVideo (req, res, next) { function getVideo (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
if (videoInstance.isOwned()) { if (videoInstance.isOwned()) {
@ -393,7 +394,7 @@ function getVideo (req, res, next) {
res.json(videoInstance.toFormatedJSON()) res.json(videoInstance.toFormatedJSON())
} }
function listVideos (req, res, next) { function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
if (err) return next(err) if (err) return next(err)
@ -401,7 +402,7 @@ function listVideos (req, res, next) {
}) })
} }
function removeVideo (req, res, next) { function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoInstance = res.locals.video const videoInstance = res.locals.video
videoInstance.destroy().asCallback(function (err) { videoInstance.destroy().asCallback(function (err) {
@ -414,7 +415,7 @@ function removeVideo (req, res, next) {
}) })
} }
function searchVideos (req, res, next) { function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
db.Video.searchAndPopulateAuthorAndPodAndTags( db.Video.searchAndPopulateAuthorAndPodAndTags(
req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
function (err, videosList, videosTotal) { function (err, videosList, videosTotal) {

View File

@ -1,4 +1,5 @@
import * as express from 'express' import * as express from 'express'
import * as Sequelize from 'sequelize'
import { waterfall } from 'async' import { waterfall } from 'async'
import { database as db } from '../../../initializers/database' import { database as db } from '../../../initializers/database'
@ -39,7 +40,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function rateVideoRetryWrapper (req, res, next) { function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
const options = { const options = {
arguments: [ req, res ], arguments: [ req, res ],
errorMessage: 'Cannot update the user video rate.' errorMessage: 'Cannot update the user video rate.'
@ -52,7 +53,7 @@ function rateVideoRetryWrapper (req, res, next) {
}) })
} }
function rateVideo (req, res, finalCallback) { function rateVideo (req: express.Request, res: express.Response, finalCallback: (err: Error) => void) {
const rateType = req.body.rating const rateType = req.body.rating
const videoInstance = res.locals.video const videoInstance = res.locals.video
const userInstance = res.locals.oauth.token.User const userInstance = res.locals.oauth.token.User
@ -168,7 +169,7 @@ function rateVideo (req, res, finalCallback) {
commitTransaction commitTransaction
], function (err, t) { ], function (err: Error, t: Sequelize.Transaction) {
if (err) { if (err) {
// This is just a debug because we will retry the insert // This is just a debug because we will retry the insert
logger.debug('Cannot add the user video rate.', { error: err }) logger.debug('Cannot add the user video rate.', { error: err })

View File

@ -12,6 +12,7 @@ import {
STATIC_MAX_AGE STATIC_MAX_AGE
} from '../initializers' } from '../initializers'
import { root } from '../helpers' import { root } from '../helpers'
import { VideoInstance } from '../models'
const clientsRouter = express.Router() const clientsRouter = express.Router()
@ -25,7 +26,7 @@ const indexPath = join(distPath, 'index.html')
// Do not use a template engine for a so little thing // Do not use a template engine for a so little thing
clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
clientsRouter.use('/videos/embed', function (req, res, next) { clientsRouter.use('/videos/embed', function (req: express.Request, res: express.Response, next: express.NextFunction) {
res.sendFile(embedPath) res.sendFile(embedPath)
}) })
@ -33,7 +34,7 @@ clientsRouter.use('/videos/embed', function (req, res, next) {
clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
// 404 for static files not found // 404 for static files not found
clientsRouter.use('/client/*', function (req, res, next) { clientsRouter.use('/client/*', function (req: express.Request, res: express.Response, next: express.NextFunction) {
res.sendStatus(404) res.sendStatus(404)
}) })
@ -45,7 +46,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addOpenGraphTags (htmlStringPage, video) { function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
let basePreviewUrlHttp let basePreviewUrlHttp
if (video.isOwned()) { if (video.isOwned()) {
@ -88,8 +89,8 @@ function addOpenGraphTags (htmlStringPage, video) {
return htmlStringPage.replace(opengraphComment, tagsString) return htmlStringPage.replace(opengraphComment, tagsString)
} }
function generateWatchHtmlPage (req, res, next) { function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoId = req.params.id const videoId = '' + req.params.id
// Let Angular application handle errors // Let Angular application handle errors
if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath) if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
@ -102,7 +103,7 @@ function generateWatchHtmlPage (req, res, next) {
video: function (callback) { video: function (callback) {
db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback) db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
} }
}, function (err, result: any) { }, function (err: Error, result: { file: Buffer, video: VideoInstance }) {
if (err) return next(err) if (err) return next(err)
const html = result.file.toString() const html = result.file.toString()

View File

@ -1,8 +1,8 @@
function exists (value) { function exists (value: any) {
return value !== undefined && value !== null return value !== undefined && value !== null
} }
function isArray (value) { function isArray (value: any) {
return Array.isArray(value) return Array.isArray(value)
} }
@ -12,3 +12,12 @@ export {
exists, exists,
isArray isArray
} }
declare global {
namespace ExpressValidator {
export interface Validator {
exists,
isArray
}
}
}

View File

@ -1,12 +1,12 @@
import * as validator from 'validator' import * as validator from 'validator'
import { isArray } from './misc' import { isArray, exists } from './misc'
function isHostValid (host) { function isHostValid (host: string) {
return validator.isURL(host) && host.split('://').length === 1 return exists(host) && validator.isURL(host) && host.split('://').length === 1
} }
function isEachUniqueHostValid (hosts) { function isEachUniqueHostValid (hosts: string[]) {
return isArray(hosts) && return isArray(hosts) &&
hosts.length !== 0 && hosts.length !== 0 &&
hosts.every(function (host) { hosts.every(function (host) {
@ -20,3 +20,12 @@ export {
isEachUniqueHostValid, isEachUniqueHostValid,
isHostValid isHostValid
} }
declare global {
namespace ExpressValidator {
export interface Validator {
isEachUniqueHostValid
isHostValid
}
}
}

View File

@ -1 +1 @@
export * from './videos'; export * from './videos'

View File

@ -31,7 +31,7 @@ import {
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
function isEachRemoteRequestVideosValid (requests) { function isEachRemoteRequestVideosValid (requests: any[]) {
return isArray(requests) && return isArray(requests) &&
requests.every(function (request) { requests.every(function (request) {
const video = request.data const video = request.data
@ -61,7 +61,7 @@ function isEachRemoteRequestVideosValid (requests) {
}) })
} }
function isEachRemoteRequestVideosQaduValid (requests) { function isEachRemoteRequestVideosQaduValid (requests: any[]) {
return isArray(requests) && return isArray(requests) &&
requests.every(function (request) { requests.every(function (request) {
const video = request.data const video = request.data
@ -70,14 +70,14 @@ function isEachRemoteRequestVideosQaduValid (requests) {
return ( return (
isVideoRemoteIdValid(video.remoteId) && isVideoRemoteIdValid(video.remoteId) &&
(has(video, 'views') === false || isVideoViewsValid) && (has(video, 'views') === false || isVideoViewsValid(video.views)) &&
(has(video, 'likes') === false || isVideoLikesValid) && (has(video, 'likes') === false || isVideoLikesValid(video.likes)) &&
(has(video, 'dislikes') === false || isVideoDislikesValid) (has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes))
) )
}) })
} }
function isEachRemoteRequestVideosEventsValid (requests) { function isEachRemoteRequestVideosEventsValid (requests: any[]) {
return isArray(requests) && return isArray(requests) &&
requests.every(function (request) { requests.every(function (request) {
const eventData = request.data const eventData = request.data
@ -100,9 +100,19 @@ export {
isEachRemoteRequestVideosEventsValid isEachRemoteRequestVideosEventsValid
} }
declare global {
namespace ExpressValidator {
export interface Validator {
isEachRemoteRequestVideosValid,
isEachRemoteRequestVideosQaduValid,
isEachRemoteRequestVideosEventsValid
}
}
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function isCommonVideoAttributesValid (video) { function isCommonVideoAttributesValid (video: any) {
return isVideoDateValid(video.createdAt) && return isVideoDateValid(video.createdAt) &&
isVideoDateValid(video.updatedAt) && isVideoDateValid(video.updatedAt) &&
isVideoCategoryValid(video.category) && isVideoCategoryValid(video.category) &&
@ -121,18 +131,18 @@ function isCommonVideoAttributesValid (video) {
isVideoDislikesValid(video.dislikes) isVideoDislikesValid(video.dislikes)
} }
function isRequestTypeAddValid (value) { function isRequestTypeAddValid (value: string) {
return value === ENDPOINT_ACTIONS.ADD return value === ENDPOINT_ACTIONS.ADD
} }
function isRequestTypeUpdateValid (value) { function isRequestTypeUpdateValid (value: string) {
return value === ENDPOINT_ACTIONS.UPDATE return value === ENDPOINT_ACTIONS.UPDATE
} }
function isRequestTypeRemoveValid (value) { function isRequestTypeRemoveValid (value: string) {
return value === ENDPOINT_ACTIONS.REMOVE return value === ENDPOINT_ACTIONS.REMOVE
} }
function isRequestTypeReportAbuseValid (value) { function isRequestTypeReportAbuseValid (value: string) {
return value === ENDPOINT_ACTIONS.REPORT_ABUSE return value === ENDPOINT_ACTIONS.REPORT_ABUSE
} }

View File

@ -1,25 +1,26 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as validator from 'validator' import * as validator from 'validator'
import { exists } from './misc'
import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
function isUserPasswordValid (value) { function isUserPasswordValid (value: string) {
return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
} }
function isUserRoleValid (value) { function isUserRoleValid (value: string) {
return values(USER_ROLES).indexOf(value) !== -1 return values(USER_ROLES).indexOf(value) !== -1
} }
function isUserUsernameValid (value) { function isUserUsernameValid (value: string) {
const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
} }
function isUserDisplayNSFWValid (value) { function isUserDisplayNSFWValid (value: any) {
return validator.isBoolean(value) return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -30,3 +31,14 @@ export {
isUserUsernameValid, isUserUsernameValid,
isUserDisplayNSFWValid isUserDisplayNSFWValid
} }
declare global {
namespace ExpressValidator {
export interface Validator {
isUserPasswordValid,
isUserRoleValid,
isUserUsernameValid,
isUserDisplayNSFWValid
}
}
}

View File

@ -9,105 +9,105 @@ import {
VIDEO_RATE_TYPES VIDEO_RATE_TYPES
} from '../../initializers' } from '../../initializers'
import { isUserUsernameValid } from './users' import { isUserUsernameValid } from './users'
import { isArray } from './misc' import { isArray, exists } from './misc'
const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
function isVideoAuthorValid (value) { function isVideoAuthorValid (value: string) {
return isUserUsernameValid(value) return isUserUsernameValid(value)
} }
function isVideoDateValid (value) { function isVideoDateValid (value: string) {
return validator.isDate(value) return exists(value) && validator.isISO8601(value)
} }
function isVideoCategoryValid (value) { function isVideoCategoryValid (value: number) {
return VIDEO_CATEGORIES[value] !== undefined return VIDEO_CATEGORIES[value] !== undefined
} }
function isVideoLicenceValid (value) { function isVideoLicenceValid (value: number) {
return VIDEO_LICENCES[value] !== undefined return VIDEO_LICENCES[value] !== undefined
} }
function isVideoLanguageValid (value) { function isVideoLanguageValid (value: number) {
return value === null || VIDEO_LANGUAGES[value] !== undefined return value === null || VIDEO_LANGUAGES[value] !== undefined
} }
function isVideoNSFWValid (value) { function isVideoNSFWValid (value: any) {
return validator.isBoolean(value) return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
} }
function isVideoDescriptionValid (value) { function isVideoDescriptionValid (value: string) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
} }
function isVideoDurationValid (value) { function isVideoDurationValid (value: string) {
return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
} }
function isVideoExtnameValid (value) { function isVideoExtnameValid (value: string) {
return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
} }
function isVideoInfoHashValid (value) { function isVideoInfoHashValid (value: string) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
} }
function isVideoNameValid (value) { function isVideoNameValid (value: string) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
} }
function isVideoTagsValid (tags) { function isVideoTagsValid (tags: string[]) {
return isArray(tags) && return isArray(tags) &&
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
tags.every(function (tag) { tags.every(function (tag) {
return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
}) })
} }
function isVideoThumbnailValid (value) { function isVideoThumbnailValid (value: string) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
} }
function isVideoThumbnailDataValid (value) { function isVideoThumbnailDataValid (value: string) {
return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
} }
function isVideoRemoteIdValid (value) { function isVideoRemoteIdValid (value: string) {
return validator.isUUID(value, 4) return exists(value) && validator.isUUID(value, 4)
} }
function isVideoAbuseReasonValid (value) { function isVideoAbuseReasonValid (value: string) {
return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
} }
function isVideoAbuseReporterUsernameValid (value) { function isVideoAbuseReporterUsernameValid (value: string) {
return isUserUsernameValid(value) return isUserUsernameValid(value)
} }
function isVideoViewsValid (value) { function isVideoViewsValid (value: string) {
return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
} }
function isVideoLikesValid (value) { function isVideoLikesValid (value: string) {
return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
} }
function isVideoDislikesValid (value) { function isVideoDislikesValid (value: string) {
return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
} }
function isVideoEventCountValid (value) { function isVideoEventCountValid (value: string) {
return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
} }
function isVideoRatingTypeValid (value) { function isVideoRatingTypeValid (value: string) {
return values(VIDEO_RATE_TYPES).indexOf(value) !== -1 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
} }
function isVideoFile (value, files) { function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
// Should have files // Should have files
if (!files) return false if (!files) return false
@ -149,3 +149,33 @@ export {
isVideoDislikesValid, isVideoDislikesValid,
isVideoEventCountValid isVideoEventCountValid
} }
declare global {
namespace ExpressValidator {
export interface Validator {
isVideoAuthorValid,
isVideoDateValid,
isVideoCategoryValid,
isVideoLicenceValid,
isVideoLanguageValid,
isVideoNSFWValid,
isVideoDescriptionValid,
isVideoDurationValid,
isVideoInfoHashValid,
isVideoNameValid,
isVideoTagsValid,
isVideoThumbnailValid,
isVideoThumbnailDataValid,
isVideoExtnameValid,
isVideoRemoteIdValid,
isVideoAbuseReasonValid,
isVideoAbuseReporterUsernameValid,
isVideoFile,
isVideoViewsValid,
isVideoLikesValid,
isVideoRatingTypeValid,
isVideoDislikesValid,
isVideoEventCountValid
}
}
}

View File

@ -1,14 +1,15 @@
import * as Sequelize from 'sequelize'
// TODO: import from ES6 when retry typing file will include errorFilter function // TODO: import from ES6 when retry typing file will include errorFilter function
import * as retry from 'async/retry' import * as retry from 'async/retry'
import { database as db } from '../initializers/database' import { database as db } from '../initializers/database'
import { logger } from './logger' import { logger } from './logger'
function commitTransaction (t, callback) { function commitTransaction (t: Sequelize.Transaction, callback: (err: Error) => void) {
return t.commit().asCallback(callback) return t.commit().asCallback(callback)
} }
function rollbackTransaction (err, t, callback) { function rollbackTransaction (err: Error, t: Sequelize.Transaction, callback: (err: Error) => void) {
// Try to rollback transaction // Try to rollback transaction
if (t) { if (t) {
// Do not catch err, report the original one // Do not catch err, report the original one
@ -20,8 +21,8 @@ function rollbackTransaction (err, t, callback) {
} }
} }
// { arguments, errorMessage } type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
function retryTransactionWrapper (functionToRetry, options, finalCallback) { function retryTransactionWrapper (functionToRetry: Function, options: RetryTransactionWrapperOptions, finalCallback: Function) {
const args = options.arguments ? options.arguments : [] const args = options.arguments ? options.arguments : []
transactionRetryer( transactionRetryer(
@ -39,7 +40,7 @@ function retryTransactionWrapper (functionToRetry, options, finalCallback) {
) )
} }
function transactionRetryer (func, callback) { function transactionRetryer (func: Function, callback: (err: Error) => void) {
retry({ retry({
times: 5, times: 5,
@ -51,7 +52,7 @@ function transactionRetryer (func, callback) {
}, func, callback) }, func, callback)
} }
function startSerializableTransaction (callback) { function startSerializableTransaction (callback: (err: Error, t: Sequelize.Transaction) => void) {
db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) { db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) {
// We force to return only two parameters // We force to return only two parameters
return callback(err, t) return callback(err, t)

View File

@ -14,7 +14,7 @@ import {
} from '../initializers' } from '../initializers'
import { logger } from './logger' import { logger } from './logger'
function checkSignature (publicKey, data, hexSignature) { function checkSignature (publicKey: string, data: string, hexSignature: string) {
const verify = crypto.createVerify(SIGNATURE_ALGORITHM) const verify = crypto.createVerify(SIGNATURE_ALGORITHM)
let dataString let dataString
@ -35,10 +35,10 @@ function checkSignature (publicKey, data, hexSignature) {
return isValid return isValid
} }
function sign (data) { function sign (data: string|Object) {
const sign = crypto.createSign(SIGNATURE_ALGORITHM) const sign = crypto.createSign(SIGNATURE_ALGORITHM)
let dataString let dataString: string
if (typeof data === 'string') { if (typeof data === 'string') {
dataString = data dataString = data
} else { } else {
@ -60,7 +60,7 @@ function sign (data) {
return signature return signature
} }
function comparePassword (plainPassword, hashPassword, callback) { function comparePassword (plainPassword: string, hashPassword: string, callback: (err: Error, match?: boolean) => void) {
bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
if (err) return callback(err) if (err) return callback(err)
@ -68,7 +68,7 @@ function comparePassword (plainPassword, hashPassword, callback) {
}) })
} }
function createCertsIfNotExist (callback) { function createCertsIfNotExist (callback: (err: Error) => void) {
certsExist(function (err, exist) { certsExist(function (err, exist) {
if (err) return callback(err) if (err) return callback(err)
@ -82,7 +82,7 @@ function createCertsIfNotExist (callback) {
}) })
} }
function cryptPassword (password, callback) { function cryptPassword (password: string, callback: (err: Error, hash?: string) => void) {
bcrypt.genSalt(BCRYPT_SALT_SIZE, function (err, salt) { bcrypt.genSalt(BCRYPT_SALT_SIZE, function (err, salt) {
if (err) return callback(err) if (err) return callback(err)
@ -92,12 +92,12 @@ function cryptPassword (password, callback) {
}) })
} }
function getMyPrivateCert (callback) { function getMyPrivateCert (callback: (err: Error, privateCert: string) => void) {
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
fs.readFile(certPath, 'utf8', callback) fs.readFile(certPath, 'utf8', callback)
} }
function getMyPublicCert (callback) { function getMyPublicCert (callback: (err: Error, publicCert: string) => void) {
const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME) const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
fs.readFile(certPath, 'utf8', callback) fs.readFile(certPath, 'utf8', callback)
} }
@ -116,7 +116,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function certsExist (callback) { function certsExist (callback: (err: Error, certsExist: boolean) => void) {
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
fs.access(certPath, function (err) { fs.access(certPath, function (err) {
// If there is an error the certificates do not exist // If there is an error the certificates do not exist
@ -125,14 +125,14 @@ function certsExist (callback) {
}) })
} }
function createCerts (callback) { function createCerts (callback: (err: Error) => void) {
certsExist(function (err, exist) { certsExist(function (err, exist) {
if (err) return callback(err) if (err) return callback(err)
if (exist === true) { if (exist === true) {
const string = 'Certs already exist.' const errorMessage = 'Certs already exist.'
logger.warning(string) logger.warning(errorMessage)
return callback(new Error(string)) return callback(new Error(errorMessage))
} }
logger.info('Generating a RSA key...') logger.info('Generating a RSA key...')

View File

@ -6,9 +6,15 @@ import {
REMOTE_SCHEME, REMOTE_SCHEME,
CONFIG CONFIG
} from '../initializers' } from '../initializers'
import { PodInstance } from '../models'
import { sign } from './peertube-crypto' import { sign } from './peertube-crypto'
function makeRetryRequest (params, callback) { type MakeRetryRequestParams = {
url: string,
method: 'GET'|'POST',
json: Object
}
function makeRetryRequest (params: MakeRetryRequestParams, callback: request.RequestCallback) {
replay( replay(
request(params, callback), request(params, callback),
{ {
@ -20,14 +26,21 @@ function makeRetryRequest (params, callback) {
) )
} }
function makeSecureRequest (params, callback) { type MakeSecureRequestParams = {
method: 'GET'|'POST'
toPod: PodInstance
path: string
sign: boolean
data?: Object
}
function makeSecureRequest (params: MakeSecureRequestParams, callback: request.RequestCallback) {
const requestParams = { const requestParams = {
url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
json: {} json: {}
} }
if (params.method !== 'POST') { if (params.method !== 'POST') {
return callback(new Error('Cannot make a secure request with a non POST method.')) return callback(new Error('Cannot make a secure request with a non POST method.'), null, null)
} }
// Add signature if it is specified in the params // Add signature if it is specified in the params

View File

@ -1,13 +1,15 @@
import * as express from 'express'
import { pseudoRandomBytes } from 'crypto' import { pseudoRandomBytes } from 'crypto'
import { join } from 'path' import { join } from 'path'
import { logger } from './logger' import { logger } from './logger'
function badRequest (req, res, next) { function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
res.type('json').status(400).end() res.type('json').status(400).end()
} }
function generateRandomString (size, callback) { function generateRandomString (size: number, callback: (err: Error, randomString?: string) => void) {
pseudoRandomBytes(size, function (err, raw) { pseudoRandomBytes(size, function (err, raw) {
if (err) return callback(err) if (err) return callback(err)
@ -15,11 +17,6 @@ function generateRandomString (size, callback) {
}) })
} }
function cleanForExit (webtorrentProcess) {
logger.info('Gracefully exiting.')
process.kill(-webtorrentProcess.pid)
}
function createEmptyCallback () { function createEmptyCallback () {
return function (err) { return function (err) {
if (err) logger.error('Error in empty callback.', { error: err }) if (err) logger.error('Error in empty callback.', { error: err })
@ -27,10 +24,10 @@ function createEmptyCallback () {
} }
function isTestInstance () { function isTestInstance () {
return (process.env.NODE_ENV === 'test') return process.env.NODE_ENV === 'test'
} }
function getFormatedObjects (objects, objectsTotal) { function getFormatedObjects (objects: any[], objectsTotal: number) {
const formatedObjects = [] const formatedObjects = []
objects.forEach(function (object) { objects.forEach(function (object) {
@ -53,7 +50,6 @@ function root () {
export { export {
badRequest, badRequest,
createEmptyCallback, createEmptyCallback,
cleanForExit,
generateRandomString, generateRandomString,
isTestInstance, isTestInstance,
getFormatedObjects, getFormatedObjects,

View File

@ -23,7 +23,7 @@ function checkMissedConfig () {
'storage.certs', 'storage.videos', 'storage.logs', 'storage.thumbnails', 'storage.previews', 'storage.certs', 'storage.videos', 'storage.logs', 'storage.thumbnails', 'storage.previews',
'admin.email', 'signup.enabled', 'transcoding.enabled', 'transcoding.threads' 'admin.email', 'signup.enabled', 'transcoding.enabled', 'transcoding.threads'
] ]
const miss = [] const miss: string[] = []
for (const key of required) { for (const key of required) {
if (!config.has(key)) { if (!config.has(key)) {
@ -35,7 +35,7 @@ function checkMissedConfig () {
} }
// Check the available codecs // Check the available codecs
function checkFFmpeg (callback) { function checkFFmpeg (callback: (err: Error) => void) {
const Ffmpeg = require('fluent-ffmpeg') const Ffmpeg = require('fluent-ffmpeg')
Ffmpeg.getAvailableCodecs(function (err, codecs) { Ffmpeg.getAvailableCodecs(function (err, codecs) {
@ -57,7 +57,7 @@ function checkFFmpeg (callback) {
}) })
} }
function clientsExist (callback) { function clientsExist (callback: (err: Error, clientsExist?: boolean) => void) {
db.OAuthClient.countTotal(function (err, totalClients) { db.OAuthClient.countTotal(function (err, totalClients) {
if (err) return callback(err) if (err) return callback(err)
@ -65,7 +65,7 @@ function clientsExist (callback) {
}) })
} }
function usersExist (callback) { function usersExist (callback: (err: Error, usersExist?: boolean) => void) {
db.User.countTotal(function (err, totalUsers) { db.User.countTotal(function (err, totalUsers) {
if (err) return callback(err) if (err) return callback(err)

View File

@ -202,7 +202,7 @@ const REQUEST_ENDPOINTS = {
VIDEOS: 'videos' VIDEOS: 'videos'
} }
const REQUEST_ENDPOINT_ACTIONS = {} const REQUEST_ENDPOINT_ACTIONS: { [ id: string ]: any } = {}
REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = { REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
ADD: 'add', ADD: 'add',
UPDATE: 'update', UPDATE: 'update',

View File

@ -59,7 +59,7 @@ const sequelize = new Sequelize(dbname, username, password, {
port: CONFIG.DATABASE.PORT, port: CONFIG.DATABASE.PORT,
benchmark: isTestInstance(), benchmark: isTestInstance(),
logging: function (message, benchmark) { logging: function (message: string, benchmark: number) {
let newMessage = message let newMessage = message
if (benchmark !== undefined) { if (benchmark !== undefined) {
newMessage += ' | ' + benchmark + 'ms' newMessage += ' | ' + benchmark + 'ms'
@ -71,7 +71,7 @@ const sequelize = new Sequelize(dbname, username, password, {
database.sequelize = sequelize database.sequelize = sequelize
database.init = function (silent, callback) { database.init = function (silent: boolean, callback: (err: Error) => void) {
const modelDirectory = join(__dirname, '..', 'models') const modelDirectory = join(__dirname, '..', 'models')
fs.readdir(modelDirectory, function (err, files) { fs.readdir(modelDirectory, function (err, files) {

View File

@ -9,7 +9,7 @@ import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
import { clientsExist, usersExist } from './checker' import { clientsExist, usersExist } from './checker'
import { logger, createCertsIfNotExist, root } from '../helpers' import { logger, createCertsIfNotExist, root } from '../helpers'
function installApplication (callback) { function installApplication (callback: (err: Error) => void) {
series([ series([
function createDatabase (callbackAsync) { function createDatabase (callbackAsync) {
db.sequelize.sync().asCallback(callbackAsync) db.sequelize.sync().asCallback(callbackAsync)
@ -42,7 +42,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function createDirectoriesIfNotExist (callback) { function createDirectoriesIfNotExist (callback: (err: Error) => void) {
const storages = config.get('storage') const storages = config.get('storage')
each(Object.keys(storages), function (key, callbackEach) { each(Object.keys(storages), function (key, callbackEach) {
@ -51,7 +51,7 @@ function createDirectoriesIfNotExist (callback) {
}, callback) }, callback)
} }
function createOAuthClientIfNotExist (callback) { function createOAuthClientIfNotExist (callback: (err: Error) => void) {
clientsExist(function (err, exist) { clientsExist(function (err, exist) {
if (err) return callback(err) if (err) return callback(err)
@ -80,7 +80,7 @@ function createOAuthClientIfNotExist (callback) {
}) })
} }
function createOAuthAdminIfNotExist (callback) { function createOAuthAdminIfNotExist (callback: (err: Error) => void) {
usersExist(function (err, exist) { usersExist(function (err, exist) {
if (err) return callback(err) if (err) return callback(err)

View File

@ -8,7 +8,7 @@ import { LAST_MIGRATION_VERSION } from './constants'
import { logger } from '../helpers' import { logger } from '../helpers'
import { ApplicationInstance } from '../models' import { ApplicationInstance } from '../models'
function migrate (finalCallback) { function migrate (finalCallback: (err: Error) => void) {
waterfall([ waterfall([
function checkApplicationTableExists (callback) { function checkApplicationTableExists (callback) {
@ -56,7 +56,7 @@ function migrate (finalCallback) {
}, },
function doMigrations (actualVersion, migrationScripts, callback) { function doMigrations (actualVersion, migrationScripts, callback) {
eachSeries(migrationScripts, function (entity, callbackEach) { eachSeries(migrationScripts, function (entity: any, callbackEach) {
executeMigration(actualVersion, entity, callbackEach) executeMigration(actualVersion, entity, callbackEach)
}, function (err) { }, function (err) {
if (err) return callback(err) if (err) return callback(err)
@ -76,7 +76,8 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getMigrationScripts (callback) { type GetMigrationScriptsCallback = (err: Error, filesToMigrate?: { version: string, script: string }[]) => void
function getMigrationScripts (callback: GetMigrationScriptsCallback) {
fs.readdir(path.join(__dirname, 'migrations'), function (err, files) { fs.readdir(path.join(__dirname, 'migrations'), function (err, files) {
if (err) return callback(err) if (err) return callback(err)
@ -95,7 +96,7 @@ function getMigrationScripts (callback) {
}) })
} }
function executeMigration (actualVersion, entity, callback) { function executeMigration (actualVersion: number, entity: { version: string, script: string }, callback: (err: Error) => void) {
const versionScript = parseInt(entity.version, 10) const versionScript = parseInt(entity.version, 10)
// Do not execute old migration scripts // Do not execute old migration scripts

View File

@ -1,5 +1,6 @@
import { each, eachLimit, eachSeries, series, waterfall } from 'async' import { each, eachLimit, eachSeries, series, waterfall } from 'async'
import * as request from 'request' import * as request from 'request'
import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../initializers/database'
import { import {
@ -19,9 +20,18 @@ import {
} from '../helpers' } from '../helpers'
import { import {
RequestScheduler, RequestScheduler,
RequestSchedulerOptions,
RequestVideoQaduScheduler, RequestVideoQaduScheduler,
RequestVideoEventScheduler RequestVideoQaduSchedulerOptions,
RequestVideoEventScheduler,
RequestVideoEventSchedulerOptions
} from './request' } from './request'
import { PodInstance, VideoInstance } from '../models'
type QaduParam = { videoId: string, type: string }
type EventParam = { videoId: string, type: string }
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
@ -35,7 +45,7 @@ function activateSchedulers () {
requestVideoEventScheduler.activate() requestVideoEventScheduler.activate()
} }
function addVideoToFriends (videoData, transaction, callback) { function addVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
const options = { const options = {
type: ENDPOINT_ACTIONS.ADD, type: ENDPOINT_ACTIONS.ADD,
endpoint: REQUEST_ENDPOINTS.VIDEOS, endpoint: REQUEST_ENDPOINTS.VIDEOS,
@ -45,7 +55,7 @@ function addVideoToFriends (videoData, transaction, callback) {
createRequest(options, callback) createRequest(options, callback)
} }
function updateVideoToFriends (videoData, transaction, callback) { function updateVideoToFriends (videoData: Object, transaction: Sequelize.Transaction, callback: (err: Error) => void) {
const options = { const options = {
type: ENDPOINT_ACTIONS.UPDATE, type: ENDPOINT_ACTIONS.UPDATE,
endpoint: REQUEST_ENDPOINTS.VIDEOS, endpoint: REQUEST_ENDPOINTS.VIDEOS,
@ -55,35 +65,37 @@ function updateVideoToFriends (videoData, transaction, callback) {
createRequest(options, callback) createRequest(options, callback)
} }
function removeVideoToFriends (videoParams) { function removeVideoToFriends (videoParams: Object) {
const options = { const options = {
type: ENDPOINT_ACTIONS.REMOVE, type: ENDPOINT_ACTIONS.REMOVE,
endpoint: REQUEST_ENDPOINTS.VIDEOS, endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: videoParams data: videoParams,
transaction: null
} }
createRequest(options) createRequest(options)
} }
function reportAbuseVideoToFriend (reportData, video) { function reportAbuseVideoToFriend (reportData: Object, video: VideoInstance) {
const options = { const options = {
type: ENDPOINT_ACTIONS.REPORT_ABUSE, type: ENDPOINT_ACTIONS.REPORT_ABUSE,
endpoint: REQUEST_ENDPOINTS.VIDEOS, endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: reportData, data: reportData,
toIds: [ video.Author.podId ] toIds: [ video.Author.podId ],
transaction: null
} }
createRequest(options) createRequest(options)
} }
function quickAndDirtyUpdateVideoToFriends (qaduParams, transaction?, callback?) { function quickAndDirtyUpdateVideoToFriends (qaduParam: QaduParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
const options = { const options = {
videoId: qaduParams.videoId, videoId: qaduParam.videoId,
type: qaduParams.type, type: qaduParam.type,
transaction transaction
} }
return createVideoQaduRequest(options, callback) return createVideoQaduRequest(options, callback)
} }
function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCallback) { function quickAndDirtyUpdatesVideoToFriends (qadusParams: QaduParam[], transaction: Sequelize.Transaction, finalCallback: (err: Error) => void) {
const tasks = [] const tasks = []
qadusParams.forEach(function (qaduParams) { qadusParams.forEach(function (qaduParams) {
@ -97,16 +109,16 @@ function quickAndDirtyUpdatesVideoToFriends (qadusParams, transaction, finalCall
series(tasks, finalCallback) series(tasks, finalCallback)
} }
function addEventToRemoteVideo (eventParams, transaction?, callback?) { function addEventToRemoteVideo (eventParam: EventParam, transaction?: Sequelize.Transaction, callback?: (err: Error) => void) {
const options = { const options = {
videoId: eventParams.videoId, videoId: eventParam.videoId,
type: eventParams.type, type: eventParam.type,
transaction transaction
} }
createVideoEventRequest(options, callback) createVideoEventRequest(options, callback)
} }
function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) { function addEventsToRemoteVideo (eventsParams: EventParam[], transaction: Sequelize.Transaction, finalCallback: (err: Error) => void) {
const tasks = [] const tasks = []
eventsParams.forEach(function (eventParams) { eventsParams.forEach(function (eventParams) {
@ -120,7 +132,7 @@ function addEventsToRemoteVideo (eventsParams, transaction, finalCallback) {
series(tasks, finalCallback) series(tasks, finalCallback)
} }
function hasFriends (callback) { function hasFriends (callback: (err: Error, hasFriends?: boolean) => void) {
db.Pod.countAll(function (err, count) { db.Pod.countAll(function (err, count) {
if (err) return callback(err) if (err) return callback(err)
@ -129,7 +141,7 @@ function hasFriends (callback) {
}) })
} }
function makeFriends (hosts, callback) { function makeFriends (hosts: string[], callback: (err: Error) => void) {
const podsScore = {} const podsScore = {}
logger.info('Make friends!') logger.info('Make friends!')
@ -141,7 +153,7 @@ function makeFriends (hosts, callback) {
eachSeries(hosts, function (host, callbackEach) { eachSeries(hosts, function (host, callbackEach) {
computeForeignPodsList(host, podsScore, callbackEach) computeForeignPodsList(host, podsScore, callbackEach)
}, function (err) { }, function (err: Error) {
if (err) return callback(err) if (err) return callback(err)
logger.debug('Pods scores computed.', { podsScore: podsScore }) logger.debug('Pods scores computed.', { podsScore: podsScore })
@ -153,7 +165,7 @@ function makeFriends (hosts, callback) {
}) })
} }
function quitFriends (callback) { function quitFriends (callback: (err: Error) => void) {
// Stop pool requests // Stop pool requests
requestScheduler.deactivate() requestScheduler.deactivate()
@ -172,7 +184,7 @@ function quitFriends (callback) {
function announceIQuitMyFriends (pods, callbackAsync) { function announceIQuitMyFriends (pods, callbackAsync) {
const requestParams = { const requestParams = {
method: 'POST', method: 'POST' as 'POST',
path: '/api/' + API_VERSION + '/remote/pods/remove', path: '/api/' + API_VERSION + '/remote/pods/remove',
sign: true, sign: true,
toPod: null toPod: null
@ -199,7 +211,7 @@ function quitFriends (callback) {
pod.destroy().asCallback(callbackEach) pod.destroy().asCallback(callbackEach)
}, callbackAsync) }, callbackAsync)
} }
], function (err) { ], function (err: Error) {
// Don't forget to re activate the scheduler, even if there was an error // Don't forget to re activate the scheduler, even if there was an error
requestScheduler.activate() requestScheduler.activate()
@ -210,7 +222,7 @@ function quitFriends (callback) {
}) })
} }
function sendOwnedVideosToPod (podId) { function sendOwnedVideosToPod (podId: number) {
db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) { db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
if (err) { if (err) {
logger.error('Cannot get the list of videos we own.') logger.error('Cannot get the list of videos we own.')
@ -229,7 +241,8 @@ function sendOwnedVideosToPod (podId) {
type: 'add', type: 'add',
endpoint: REQUEST_ENDPOINTS.VIDEOS, endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: remoteVideo, data: remoteVideo,
toIds: [ podId ] toIds: [ podId ],
transaction: null
} }
createRequest(options) createRequest(options)
}) })
@ -272,7 +285,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function computeForeignPodsList (host, podsScore, callback) { function computeForeignPodsList (host: string, podsScore: { [ host: string ]: number }, callback: (err: Error) => void) {
getForeignPodsList(host, function (err, res) { getForeignPodsList(host, function (err, res) {
if (err) return callback(err) if (err) return callback(err)
@ -288,11 +301,11 @@ function computeForeignPodsList (host, podsScore, callback) {
else podsScore[foreignPodHost] = 1 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 // Build the list of pods to add
// Only add a pod if it exists in more than a half base pods // Only add a pod if it exists in more than a half base pods
const podsList = [] const podsList = []
@ -308,7 +321,7 @@ function computeWinningPods (hosts, podsScore) {
return podsList return podsList
} }
function getForeignPodsList (host, callback) { function getForeignPodsList (host: string, callback: (err: Error, foreignPodsList?: any) => void) {
const path = '/api/' + API_VERSION + '/pods' const path = '/api/' + API_VERSION + '/pods'
request.get(REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) { request.get(REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) {
@ -323,16 +336,16 @@ function getForeignPodsList (host, callback) {
}) })
} }
function makeRequestsToWinningPods (cert, podsList, callback) { function makeRequestsToWinningPods (cert: string, podsList: PodInstance[], callback: (err: Error) => void) {
// Stop pool requests // Stop pool requests
requestScheduler.deactivate() requestScheduler.deactivate()
// Flush pool requests // Flush pool requests
requestScheduler.forceSend() requestScheduler.forceSend()
eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: { host: string }, callbackEach) { eachLimit(podsList, REQUESTS_IN_PARALLEL, function (pod: PodInstance, callbackEach) {
const params = { const params = {
url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/pods/', url: REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + API_VERSION + '/pods/',
method: 'POST', method: 'POST' as 'POST',
json: { json: {
host: CONFIG.WEBSERVER.HOST, host: CONFIG.WEBSERVER.HOST,
email: CONFIG.ADMIN.EMAIL, email: CONFIG.ADMIN.EMAIL,
@ -371,15 +384,22 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
requestScheduler.activate() requestScheduler.activate()
logger.debug('makeRequestsToWinningPods finished.') logger.debug('makeRequestsToWinningPods finished.')
return callback() return callback(null)
}) })
} }
// Wrapper that populate "toIds" argument with all our friends if it is not specified // Wrapper that populate "toIds" argument with all our friends if it is not specified
// { type, endpoint, data, toIds, transaction } type CreateRequestOptions = {
function createRequest (options, callback?) { type: string
endpoint: string
data: Object
toIds?: number[]
transaction: Sequelize.Transaction
}
function createRequest (options: CreateRequestOptions, callback?: (err: Error) => void) {
if (!callback) callback = function () { /* empty */ } 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 // If the "toIds" pods is not specified, we send the request to all our friends
db.Pod.listAllIds(options.transaction, function (err, podIds) { db.Pod.listAllIds(options.transaction, function (err, podIds) {
@ -393,18 +413,18 @@ function createRequest (options, callback?) {
}) })
} }
function createVideoQaduRequest (options, callback) { function createVideoQaduRequest (options: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
if (!callback) callback = createEmptyCallback() if (!callback) callback = createEmptyCallback()
requestVideoQaduScheduler.createRequest(options, callback) requestVideoQaduScheduler.createRequest(options, callback)
} }
function createVideoEventRequest (options, callback) { function createVideoEventRequest (options: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
if (!callback) callback = createEmptyCallback() if (!callback) callback = createEmptyCallback()
requestVideoEventScheduler.createRequest(options, callback) requestVideoEventScheduler.createRequest(options, callback)
} }
function isMe (host) { function isMe (host: string) {
return host === CONFIG.WEBSERVER.HOST return host === CONFIG.WEBSERVER.HOST
} }

View File

@ -1,6 +1,14 @@
import * as videoTranscoder from './video-transcoder' import * as videoTranscoder from './video-transcoder'
const jobHandlers = { import { VideoInstance } from '../../../models'
export interface JobHandler<T> {
process (data: object, callback: (err: Error, videoInstance?: T) => void)
onError (err: Error, jobId: number, video: T, callback: (err: Error) => void)
onSuccess (data: any, jobId: number, video: T, callback: (err: Error) => void)
}
const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = {
videoTranscoder videoTranscoder
} }

View File

@ -1,8 +1,9 @@
import { database as db } from '../../../initializers/database' import { database as db } from '../../../initializers/database'
import { logger } from '../../../helpers' import { logger } from '../../../helpers'
import { addVideoToFriends } from '../../../lib' import { addVideoToFriends } from '../../../lib'
import { VideoInstance } from '../../../models'
function process (data, callback) { function process (data: { id: string }, callback: (err: Error, videoInstance?: VideoInstance) => void) {
db.Video.loadAndPopulateAuthorAndPodAndTags(data.id, function (err, video) { db.Video.loadAndPopulateAuthorAndPodAndTags(data.id, function (err, video) {
if (err) return callback(err) if (err) return callback(err)
@ -12,12 +13,12 @@ function process (data, callback) {
}) })
} }
function onError (err, jobId, video, callback) { function onError (err: Error, jobId: number, video: VideoInstance, callback: () => void) {
logger.error('Error when transcoding video file in job %d.', jobId, { error: err }) logger.error('Error when transcoding video file in job %d.', jobId, { error: err })
return callback() return callback()
} }
function onSuccess (data, jobId, video, callback) { function onSuccess (data: any, jobId: number, video: VideoInstance, callback: (err: Error) => void) {
logger.info('Job %d is a success.', jobId) logger.info('Job %d is a success.', jobId)
video.toAddRemoteJSON(function (err, remoteVideo) { video.toAddRemoteJSON(function (err, remoteVideo) {

View File

@ -1,4 +1,5 @@
import { forever, queue } from 'async' import { forever, queue } from 'async'
import * as Sequelize from 'sequelize'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { import {
@ -7,7 +8,10 @@ import {
JOB_STATES JOB_STATES
} from '../../initializers' } from '../../initializers'
import { logger } from '../../helpers' import { logger } from '../../helpers'
import { jobHandlers } from './handlers' import { JobInstance } from '../../models'
import { JobHandler, jobHandlers } from './handlers'
type JobQueueCallback = (err: Error) => void
class JobScheduler { class JobScheduler {
@ -24,7 +28,7 @@ class JobScheduler {
logger.info('Jobs scheduler activated.') logger.info('Jobs scheduler activated.')
const jobsQueue = queue(this.processJob.bind(this)) const jobsQueue = queue<JobInstance, JobQueueCallback>(this.processJob.bind(this))
// Finish processing jobs from a previous start // Finish processing jobs from a previous start
const state = JOB_STATES.PROCESSING const state = JOB_STATES.PROCESSING
@ -58,7 +62,7 @@ class JobScheduler {
}) })
} }
createJob (transaction, handlerName: string, handlerInputData: object, callback) { createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: object, callback: (err: Error) => void) {
const createQuery = { const createQuery = {
state: JOB_STATES.PENDING, state: JOB_STATES.PENDING,
handlerName, handlerName,
@ -69,7 +73,7 @@ class JobScheduler {
db.Job.create(createQuery, options).asCallback(callback) db.Job.create(createQuery, options).asCallback(callback)
} }
private enqueueJobs (err, jobsQueue, jobs) { private enqueueJobs (err: Error, jobsQueue: AsyncQueue<JobInstance>, jobs: JobInstance[]) {
if (err) { if (err) {
logger.error('Cannot list pending jobs.', { error: err }) logger.error('Cannot list pending jobs.', { error: err })
} else { } else {
@ -79,7 +83,7 @@ class JobScheduler {
} }
} }
private processJob (job, callback) { private processJob (job: JobInstance, callback: (err: Error) => void) {
const jobHandler = jobHandlers[job.handlerName] const jobHandler = jobHandlers[job.handlerName]
logger.info('Processing job %d with handler %s.', job.id, job.handlerName) logger.info('Processing job %d with handler %s.', job.id, job.handlerName)
@ -89,8 +93,8 @@ class JobScheduler {
if (err) return this.cannotSaveJobError(err, callback) if (err) return this.cannotSaveJobError(err, callback)
if (jobHandler === undefined) { if (jobHandler === undefined) {
logger.error('Unknown job handler for job %s.', jobHandler.handlerName) logger.error('Unknown job handler for job %s.', job.handlerName)
return callback() return callback(null)
} }
return jobHandler.process(job.handlerInputData, (err, result) => { return jobHandler.process(job.handlerInputData, (err, result) => {
@ -104,7 +108,7 @@ class JobScheduler {
}) })
} }
private onJobError (jobHandler, job, jobResult, callback) { private onJobError (jobHandler: JobHandler<any>, job: JobInstance, jobResult: any, callback: (err: Error) => void) {
job.state = JOB_STATES.ERROR job.state = JOB_STATES.ERROR
job.save().asCallback(err => { job.save().asCallback(err => {
@ -114,7 +118,7 @@ class JobScheduler {
}) })
} }
private onJobSuccess (jobHandler, job, jobResult, callback) { private onJobSuccess (jobHandler: JobHandler<any>, job: JobInstance, jobResult: any, callback: (err: Error) => void) {
job.state = JOB_STATES.SUCCESS job.state = JOB_STATES.SUCCESS
job.save().asCallback(err => { job.save().asCallback(err => {
@ -124,7 +128,7 @@ class JobScheduler {
}) })
} }
private cannotSaveJobError (err, callback) { private cannotSaveJobError (err: Error, callback: (err: Error) => void) {
logger.error('Cannot save new job state.', { error: err }) logger.error('Cannot save new job state.', { error: err })
return callback(err) return callback(err)
} }

View File

@ -1,27 +1,30 @@
import { OAuthClientInstance, UserInstance } from '../models'
import { database as db } from '../initializers/database' import { database as db } from '../initializers/database'
import { logger } from '../helpers' import { logger } from '../helpers'
type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getAccessToken (bearerToken) { function getAccessToken (bearerToken: string) {
logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
return db.OAuthToken.getByTokenAndPopulateUser(bearerToken) return db.OAuthToken.getByTokenAndPopulateUser(bearerToken)
} }
function getClient (clientId, clientSecret) { function getClient (clientId: string, clientSecret: string) {
logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
return db.OAuthClient.getByIdAndSecret(clientId, clientSecret) return db.OAuthClient.getByIdAndSecret(clientId, clientSecret)
} }
function getRefreshToken (refreshToken) { function getRefreshToken (refreshToken: string) {
logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken)
} }
function getUser (username, password) { function getUser (username: string, password: string) {
logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
return db.User.getByUsername(username).then(function (user) { return db.User.getByUsername(username).then(function (user) {
@ -42,7 +45,7 @@ function getUser (username, password) {
}) })
} }
function revokeToken (token) { function revokeToken (token: TokenInfo) {
return db.OAuthToken.getByRefreshTokenAndPopulateUser(token.refreshToken).then(function (tokenDB) { return db.OAuthToken.getByRefreshTokenAndPopulateUser(token.refreshToken).then(function (tokenDB) {
if (tokenDB) tokenDB.destroy() if (tokenDB) tokenDB.destroy()
@ -60,7 +63,7 @@ function revokeToken (token) {
}) })
} }
function saveToken (token, client, user) { function saveToken (token: TokenInfo, client: OAuthClientInstance, user: UserInstance) {
logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
const tokenToCreate = { const tokenToCreate = {

View File

@ -2,6 +2,7 @@ import * as eachLimit from 'async/eachLimit'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { logger, makeSecureRequest } from '../../helpers' import { logger, makeSecureRequest } from '../../helpers'
import { PodInstance } from '../../models'
import { import {
API_VERSION, API_VERSION,
REQUESTS_IN_PARALLEL, REQUESTS_IN_PARALLEL,
@ -9,11 +10,12 @@ import {
} from '../../initializers' } from '../../initializers'
abstract class BaseRequestScheduler { abstract class BaseRequestScheduler {
requestInterval: number
limitPods: number
limitPerPod: number
protected lastRequestTimestamp: number protected lastRequestTimestamp: number
protected timer: NodeJS.Timer protected timer: NodeJS.Timer
protected requestInterval: number
protected limitPods: number
protected limitPerPod: number
protected description: string protected description: string
constructor () { constructor () {
@ -53,24 +55,24 @@ abstract class BaseRequestScheduler {
return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
} }
remainingRequestsCount (callback) { remainingRequestsCount (callback: (err: Error, total: number) => void) {
return this.getRequestModel().countTotalRequests(callback) return this.getRequestModel().countTotalRequests(callback)
} }
flush (callback) { flush (callback: (err: Error) => void) {
this.getRequestModel().removeAll(callback) this.getRequestModel().removeAll(callback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Make a requests to friends of a certain type // Make a requests to friends of a certain type
protected makeRequest (toPod, requestEndpoint, requestsToMake, callback) { protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) {
if (!callback) callback = function () { /* empty */ } if (!callback) callback = function () { /* empty */ }
const params = { const params = {
toPod: toPod, toPod: toPod,
sign: true, // Prove our identity sign: true, // Prove our identity
method: 'POST', method: 'POST' as 'POST',
path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
data: requestsToMake // Requests we need to make data: requestsToMake // Requests we need to make
} }

View File

@ -1,3 +1,4 @@
export * from './base-request-scheduler'
export * from './request-scheduler' export * from './request-scheduler'
export * from './request-video-event-scheduler' export * from './request-video-event-scheduler'
export * from './request-video-qadu-scheduler' export * from './request-video-qadu-scheduler'

View File

@ -1,3 +1,5 @@
import * as Sequelize from 'sequelize'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { BaseRequestScheduler } from './base-request-scheduler' import { BaseRequestScheduler } from './base-request-scheduler'
import { logger } from '../../helpers' import { logger } from '../../helpers'
@ -6,6 +8,14 @@ import {
REQUESTS_LIMIT_PER_POD REQUESTS_LIMIT_PER_POD
} from '../../initializers' } from '../../initializers'
export type RequestSchedulerOptions = {
type: string
endpoint: string
data: Object
toIds: number[]
transaction: Sequelize.Transaction
}
class RequestScheduler extends BaseRequestScheduler { class RequestScheduler extends BaseRequestScheduler {
constructor () { constructor () {
super() super()
@ -25,7 +35,7 @@ class RequestScheduler extends BaseRequestScheduler {
return db.RequestToPod return db.RequestToPod
} }
buildRequestObjects (requests) { buildRequestObjects (requests: { [ toPodId: number ]: any }) {
const requestsToMakeGrouped = {} const requestsToMakeGrouped = {}
Object.keys(requests).forEach(toPodId => { Object.keys(requests).forEach(toPodId => {
@ -51,14 +61,7 @@ class RequestScheduler extends BaseRequestScheduler {
return requestsToMakeGrouped return requestsToMakeGrouped
} }
// { type, endpoint, data, toIds, transaction } createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) {
createRequest (options, callback) {
const type = options.type
const endpoint = options.endpoint
const data = options.data
const toIds = options.toIds
const transaction = options.transaction
// TODO: check the setPods works // TODO: check the setPods works
const podIds = [] const podIds = []
@ -77,7 +80,7 @@ class RequestScheduler extends BaseRequestScheduler {
} }
} }
const dbRequestOptions = { const dbRequestOptions: Sequelize.CreateOptions = {
transaction transaction
} }

View File

@ -1,3 +1,5 @@
import * as Sequelize from 'sequelize'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { BaseRequestScheduler } from './base-request-scheduler' import { BaseRequestScheduler } from './base-request-scheduler'
import { import {
@ -6,6 +8,13 @@ import {
REQUEST_VIDEO_EVENT_ENDPOINT REQUEST_VIDEO_EVENT_ENDPOINT
} from '../../initializers' } from '../../initializers'
export type RequestVideoEventSchedulerOptions = {
type: string
videoId: string
count?: number
transaction?: Sequelize.Transaction
}
class RequestVideoEventScheduler extends BaseRequestScheduler { class RequestVideoEventScheduler extends BaseRequestScheduler {
constructor () { constructor () {
super() super()
@ -25,7 +34,7 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
return db.RequestVideoEvent return db.RequestVideoEvent
} }
buildRequestObjects (eventsToProcess) { buildRequestObjects (eventsToProcess: { [ toPodId: number ]: any }[]) {
const requestsToMakeGrouped = {} const requestsToMakeGrouped = {}
/* Example: /* Example:
@ -87,16 +96,10 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
return requestsToMakeGrouped return requestsToMakeGrouped
} }
// { type, videoId, count?, transaction? } createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
createRequest (options, callback) {
const type = options.type
const videoId = options.videoId
const transaction = options.transaction
let count = options.count
if (count === undefined) count = 1 if (count === undefined) count = 1
const dbRequestOptions: { transaction?: any } = {} const dbRequestOptions: Sequelize.CreateOptions = {}
if (transaction) dbRequestOptions.transaction = transaction if (transaction) dbRequestOptions.transaction = transaction
const createQuery = { const createQuery = {

View File

@ -1,3 +1,5 @@
import * as Sequelize from 'sequelize'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { BaseRequestScheduler } from './base-request-scheduler' import { BaseRequestScheduler } from './base-request-scheduler'
import { logger } from '../../helpers' import { logger } from '../../helpers'
@ -8,6 +10,12 @@ import {
REQUEST_VIDEO_QADU_TYPES REQUEST_VIDEO_QADU_TYPES
} from '../../initializers' } from '../../initializers'
export type RequestVideoQaduSchedulerOptions = {
type: string
videoId: string
transaction?: Sequelize.Transaction
}
class RequestVideoQaduScheduler extends BaseRequestScheduler { class RequestVideoQaduScheduler extends BaseRequestScheduler {
constructor () { constructor () {
super() super()
@ -27,7 +35,7 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
return db.RequestVideoQadu return db.RequestVideoQadu
} }
buildRequestObjects (requests) { buildRequestObjects (requests: { [ toPodId: number ]: any }[]) {
const requestsToMakeGrouped = {} const requestsToMakeGrouped = {}
Object.keys(requests).forEach(toPodId => { Object.keys(requests).forEach(toPodId => {
@ -96,17 +104,12 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
return requestsToMakeGrouped return requestsToMakeGrouped
} }
// { type, videoId, transaction? } createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
createRequest (options, callback) { const dbRequestOptions: Sequelize.BulkCreateOptions = {}
const type = options.type
const videoId = options.videoId
const transaction = options.transaction
const dbRequestOptions: { transaction?: any } = {}
if (transaction) dbRequestOptions.transaction = transaction if (transaction) dbRequestOptions.transaction = transaction
// Send the update to all our friends // Send the update to all our friends
db.Pod.listAllIds(options.transaction, function (err, podIds) { db.Pod.listAllIds(transaction, function (err, podIds) {
if (err) return callback(err) if (err) return callback(err)
const queries = [] const queries = []

View File

@ -1,6 +1,9 @@
import 'express-validator'
import * as express from 'express'
import { logger } from '../helpers' import { logger } from '../helpers'
function ensureIsAdmin (req, res, next) { function ensureIsAdmin (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = res.locals.oauth.token.user const user = res.locals.oauth.token.user
if (user.isAdmin() === false) { if (user.isAdmin() === false) {
logger.info('A non admin user is trying to access to an admin content.') logger.info('A non admin user is trying to access to an admin content.')

View File

@ -1,8 +1,8 @@
export * from './validators'; export * from './validators'
export * from './admin'; export * from './admin'
export * from './oauth'; export * from './oauth'
export * from './pagination'; export * from './pagination'
export * from './pods'; export * from './pods'
export * from './search'; export * from './search'
export * from './secure'; export * from './secure'
export * from './sort'; export * from './sort'

View File

@ -1,3 +1,5 @@
import 'express-validator'
import * as express from 'express'
import * as OAuthServer from 'express-oauth-server' import * as OAuthServer from 'express-oauth-server'
import { OAUTH_LIFETIME } from '../initializers' import { OAUTH_LIFETIME } from '../initializers'
@ -9,7 +11,7 @@ const oAuthServer = new OAuthServer({
model: require('../lib/oauth-model') model: require('../lib/oauth-model')
}) })
function authenticate (req, res, next) { function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
oAuthServer.authenticate()(req, res, function (err) { oAuthServer.authenticate()(req, res, function (err) {
if (err) { if (err) {
logger.error('Cannot authenticate.', { error: err }) logger.error('Cannot authenticate.', { error: err })
@ -22,7 +24,7 @@ function authenticate (req, res, next) {
}) })
} }
function token (req, res, next) { function token (req: express.Request, res: express.Response, next: express.NextFunction) {
return oAuthServer.token()(req, res, next) return oAuthServer.token()(req, res, next)
} }

View File

@ -1,6 +1,9 @@
import 'express-validator'
import * as express from 'express'
import { PAGINATION_COUNT_DEFAULT } from '../initializers' import { PAGINATION_COUNT_DEFAULT } from '../initializers'
function setPagination (req, res, next) { function setPagination (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.query.start) req.query.start = 0 if (!req.query.start) req.query.start = 0
else req.query.start = parseInt(req.query.start, 10) else req.query.start = parseInt(req.query.start, 10)

View File

@ -1,6 +1,9 @@
import 'express-validator'
import * as express from 'express'
import { REMOTE_SCHEME } from '../initializers' import { REMOTE_SCHEME } from '../initializers'
function setBodyHostsPort (req, res, next) { function setBodyHostsPort (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.body.hosts) return next() if (!req.body.hosts) return next()
for (let i = 0; i < req.body.hosts.length; i++) { for (let i = 0; i < req.body.hosts.length; i++) {
@ -17,7 +20,7 @@ function setBodyHostsPort (req, res, next) {
return next() return next()
} }
function setBodyHostPort (req, res, next) { function setBodyHostPort (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.body.host) return next() if (!req.body.host) return next()
const hostWithPort = getHostWithPort(req.body.host) const hostWithPort = getHostWithPort(req.body.host)
@ -41,7 +44,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getHostWithPort (host) { function getHostWithPort (host: string) {
const splitted = host.split(':') const splitted = host.split(':')
// The port was not specified // The port was not specified

View File

@ -1,4 +1,7 @@
function setVideosSearch (req, res, next) { import 'express-validator'
import * as express from 'express'
function setVideosSearch (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.query.field) req.query.field = 'name' if (!req.query.field) req.query.field = 'name'
return next() return next()

View File

@ -1,10 +1,13 @@
import 'express-validator'
import * as express from 'express'
import { database as db } from '../initializers' import { database as db } from '../initializers'
import { import {
logger, logger,
checkSignature as peertubeCryptoCheckSignature checkSignature as peertubeCryptoCheckSignature
} from '../helpers' } from '../helpers'
function checkSignature (req, res, next) { function checkSignature (req: express.Request, res: express.Response, next: express.NextFunction) {
const host = req.body.signature.host const host = req.body.signature.host
db.Pod.loadByHost(host, function (err, pod) { db.Pod.loadByHost(host, function (err, pod) {
if (err) { if (err) {

View File

@ -1,16 +1,19 @@
function setUsersSort (req, res, next) { import 'express-validator'
import * as express from 'express'
function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.query.sort) req.query.sort = '-createdAt' if (!req.query.sort) req.query.sort = '-createdAt'
return next() return next()
} }
function setVideoAbusesSort (req, res, next) { function setVideoAbusesSort (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.query.sort) req.query.sort = '-createdAt' if (!req.query.sort) req.query.sort = '-createdAt'
return next() return next()
} }
function setVideosSort (req, res, next) { function setVideosSort (req: express.Request, res: express.Response, next: express.NextFunction) {
if (!req.query.sort) req.query.sort = '-createdAt' if (!req.query.sort) req.query.sort = '-createdAt'
return next() return next()

View File

@ -1,7 +1,10 @@
import 'express-validator'
import * as express from 'express'
import { checkErrors } from './utils' import { checkErrors } from './utils'
import { logger } from '../../helpers' import { logger } from '../../helpers'
function paginationValidator (req, res, next) { function paginationValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkQuery('start', 'Should have a number start').optional().isInt() req.checkQuery('start', 'Should have a number start').optional().isInt()
req.checkQuery('count', 'Should have a number count').optional().isInt() req.checkQuery('count', 'Should have a number count').optional().isInt()

View File

@ -1,3 +1,6 @@
import 'express-validator'
import * as express from 'express'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { checkErrors } from './utils' import { checkErrors } from './utils'
import { logger } from '../../helpers' import { logger } from '../../helpers'
@ -5,7 +8,7 @@ import { CONFIG } from '../../initializers'
import { hasFriends } from '../../lib' import { hasFriends } from '../../lib'
import { isTestInstance } from '../../helpers' import { isTestInstance } from '../../helpers'
function makeFriendsValidator (req, res, next) { function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
// Force https if the administrator wants to make friends // Force https if the administrator wants to make friends
if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
return res.status(400).send('Cannot make friends with a non HTTPS webserver.') return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
@ -32,7 +35,7 @@ function makeFriendsValidator (req, res, next) {
}) })
} }
function podsAddValidator (req, res, next) { function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('host', 'Should have a host').isHostValid() req.checkBody('host', 'Should have a host').isHostValid()
req.checkBody('email', 'Should have an email').isEmail() req.checkBody('email', 'Should have an email').isEmail()
req.checkBody('publicKey', 'Should have a public key').notEmpty() req.checkBody('publicKey', 'Should have a public key').notEmpty()

View File

@ -1,7 +1,10 @@
import 'express-validator'
import * as express from 'express'
import { logger } from '../../../helpers' import { logger } from '../../../helpers'
import { checkErrors } from '../utils' import { checkErrors } from '../utils'
function signatureValidator (req, res, next) { function signatureValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('signature.host', 'Should have a signature host').isURL() req.checkBody('signature.host', 'Should have a signature host').isURL()
req.checkBody('signature.signature', 'Should have a signature').notEmpty() req.checkBody('signature.signature', 'Should have a signature').notEmpty()

View File

@ -1,7 +1,10 @@
import 'express-validator'
import * as express from 'express'
import { logger } from '../../../helpers' import { logger } from '../../../helpers'
import { checkErrors } from '../utils' import { checkErrors } from '../utils'
function remoteVideosValidator (req, res, next) { function remoteVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('data').isEachRemoteRequestVideosValid() req.checkBody('data').isEachRemoteRequestVideosValid()
logger.debug('Checking remoteVideos parameters', { parameters: req.body }) logger.debug('Checking remoteVideos parameters', { parameters: req.body })
@ -9,7 +12,7 @@ function remoteVideosValidator (req, res, next) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function remoteQaduVideosValidator (req, res, next) { function remoteQaduVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('data').isEachRemoteRequestVideosQaduValid() req.checkBody('data').isEachRemoteRequestVideosQaduValid()
logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body })
@ -17,7 +20,7 @@ function remoteQaduVideosValidator (req, res, next) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function remoteEventsVideosValidator (req, res, next) { function remoteEventsVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('data').isEachRemoteRequestVideosEventsValid() req.checkBody('data').isEachRemoteRequestVideosEventsValid()
logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body })

View File

@ -1,3 +1,6 @@
import 'express-validator'
import * as express from 'express'
import { checkErrors } from './utils' import { checkErrors } from './utils'
import { logger } from '../../helpers' import { logger } from '../../helpers'
import { SORTABLE_COLUMNS } from '../../initializers' import { SORTABLE_COLUMNS } from '../../initializers'
@ -7,15 +10,15 @@ const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
function usersSortValidator (req, res, next) { function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
checkSort(req, res, next, SORTABLE_USERS_COLUMNS) checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
} }
function videoAbusesSortValidator (req, res, next) { function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
} }
function videosSortValidator (req, res, next) { function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
} }
@ -29,7 +32,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function checkSort (req, res, next, sortableColumns) { function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) {
req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
logger.debug('Checking sort parameters', { parameters: req.query }) logger.debug('Checking sort parameters', { parameters: req.query })
@ -37,7 +40,7 @@ function checkSort (req, res, next, sortableColumns) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function createSortableColumns (sortableColumns) { function createSortableColumns (sortableColumns: string[]) {
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc) return sortableColumns.concat(sortableColumnDesc)

View File

@ -1,8 +1,11 @@
import 'express-validator'
import * as express from 'express'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { checkErrors } from './utils' import { checkErrors } from './utils'
import { logger } from '../../helpers' import { logger } from '../../helpers'
function usersAddValidator (req, res, next) { function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('username', 'Should have a valid username').isUserUsernameValid() req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
req.checkBody('password', 'Should have a valid password').isUserPasswordValid() req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
req.checkBody('email', 'Should have a valid email').isEmail() req.checkBody('email', 'Should have a valid email').isEmail()
@ -23,7 +26,7 @@ function usersAddValidator (req, res, next) {
}) })
} }
function usersRemoveValidator (req, res, next) { function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isInt() req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
logger.debug('Checking usersRemove parameters', { parameters: req.params }) logger.debug('Checking usersRemove parameters', { parameters: req.params })
@ -44,7 +47,7 @@ function usersRemoveValidator (req, res, next) {
}) })
} }
function usersUpdateValidator (req, res, next) { function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isInt() req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
// Add old password verification // Add old password verification
req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
@ -55,7 +58,7 @@ function usersUpdateValidator (req, res, next) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function usersVideoRatingValidator (req, res, next) { function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) logger.debug('Checking usersVideoRating parameters', { parameters: req.params })

View File

@ -1,9 +1,10 @@
import 'express-validator'
import * as express from 'express'
import { inspect } from 'util' import { inspect } from 'util'
import { logger } from '../../helpers' import { logger } from '../../helpers'
function checkErrors (req, res, next, statusCode?) { function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) {
if (statusCode === undefined) statusCode = 400
const errors = req.validationErrors() const errors = req.validationErrors()
if (errors) { if (errors) {

View File

@ -1,9 +1,13 @@
import 'express-validator'
import * as multer from 'multer'
import * as express from 'express'
import { database as db } from '../../initializers/database' import { database as db } from '../../initializers/database'
import { checkErrors } from './utils' import { checkErrors } from './utils'
import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
import { logger, isVideoDurationValid } from '../../helpers' import { logger, isVideoDurationValid } from '../../helpers'
function videosAddValidator (req, res, next) { function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
req.checkBody('name', 'Should have a valid name').isVideoNameValid() req.checkBody('name', 'Should have a valid name').isVideoNameValid()
req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
@ -27,13 +31,13 @@ function videosAddValidator (req, res, next) {
return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
} }
videoFile.duration = duration videoFile['duration'] = duration
next() next()
}) })
}) })
} }
function videosUpdateValidator (req, res, next) { function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
@ -61,7 +65,7 @@ function videosUpdateValidator (req, res, next) {
}) })
} }
function videosGetValidator (req, res, next) { function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
logger.debug('Checking videosGet parameters', { parameters: req.params }) logger.debug('Checking videosGet parameters', { parameters: req.params })
@ -71,7 +75,7 @@ function videosGetValidator (req, res, next) {
}) })
} }
function videosRemoveValidator (req, res, next) { function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
logger.debug('Checking videosRemove parameters', { parameters: req.params }) logger.debug('Checking videosRemove parameters', { parameters: req.params })
@ -88,7 +92,7 @@ function videosRemoveValidator (req, res, next) {
}) })
} }
function videosSearchValidator (req, res, next) { function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
req.checkParams('value', 'Should have a valid search').notEmpty() req.checkParams('value', 'Should have a valid search').notEmpty()
req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
@ -98,7 +102,7 @@ function videosSearchValidator (req, res, next) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function videoAbuseReportValidator (req, res, next) { function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
@ -109,7 +113,7 @@ function videoAbuseReportValidator (req, res, next) {
}) })
} }
function videoRateValidator (req, res, next) { function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
@ -120,7 +124,7 @@ function videoRateValidator (req, res, next) {
}) })
} }
function videosBlacklistValidator (req, res, next) { function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
@ -150,7 +154,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function checkVideoExists (id, res, callback) { function checkVideoExists (id: string, res: express.Response, callback: () => void) {
db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
if (err) { if (err) {
logger.error('Error in video request validator.', { error: err }) logger.error('Error in video request validator.', { error: err })
@ -164,7 +168,7 @@ function checkVideoExists (id, res, callback) {
}) })
} }
function checkUserCanDeleteVideo (userId, res, callback) { function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
// Retrieve the user who did the request // Retrieve the user who did the request
db.User.loadById(userId, function (err, user) { db.User.loadById(userId, function (err, user) {
if (err) { if (err) {
@ -190,7 +194,7 @@ function checkUserCanDeleteVideo (userId, res, callback) {
}) })
} }
function checkVideoIsBlacklistable (req, res, callback) { function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
if (res.locals.video.isOwned() === true) { if (res.locals.video.isOwned() === true) {
return res.status(403).send('Cannot blacklist a local video') return res.status(403).send('Cannot blacklist a local video')
} }

View File

@ -1,8 +1,11 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace ApplicationMethods { export namespace ApplicationMethods {
export type LoadMigrationVersion = (callback: (err: Error, version: number) => void) => void export type LoadMigrationVersionCallback = (err: Error, version: number) => void
export type UpdateMigrationVersion = (newVersion: number, transaction: any, callback: any) => void export type LoadMigrationVersion = (callback: LoadMigrationVersionCallback) => void
export type UpdateMigrationVersionCallback = (err: Error, applicationInstance: ApplicationAttributes) => void
export type UpdateMigrationVersion = (newVersion: number, transaction: Sequelize.Transaction, callback: UpdateMigrationVersionCallback) => void
} }
export interface ApplicationClass { export interface ApplicationClass {

View File

@ -35,7 +35,7 @@ export default function defineApplication (sequelize: Sequelize.Sequelize, DataT
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
loadMigrationVersion = function (callback: (err: Error, version: number) => void) { loadMigrationVersion = function (callback: ApplicationMethods.LoadMigrationVersionCallback) {
const query = { const query = {
attributes: [ 'migrationVersion' ] attributes: [ 'migrationVersion' ]
} }
@ -47,15 +47,10 @@ loadMigrationVersion = function (callback: (err: Error, version: number) => void
}) })
} }
updateMigrationVersion = function (newVersion: number, transaction: any, callback: any) { updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction, callback: ApplicationMethods.UpdateMigrationVersionCallback) {
const options: Sequelize.UpdateOptions = { const options: Sequelize.UpdateOptions = {
where: {} where: {},
} transaction: transaction
if (!callback) {
transaction = callback
} else {
options.transaction = transaction
} }
return Application.update({ migrationVersion: newVersion }, options).asCallback(callback) return Application.update({ migrationVersion: newVersion }, options).asCallback(callback)

View File

@ -1,7 +1,10 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { PodInstance } from './pod-interface'
export namespace AuthorMethods { export namespace AuthorMethods {
export type FindOrCreateAuthor = (name, podId, userId, transaction, callback) => void export type FindOrCreateAuthorCallback = (err: Error, authorInstance?: AuthorInstance) => void
export type FindOrCreateAuthor = (name: string, podId: number, userId: number, transaction: Sequelize.Transaction, callback: FindOrCreateAuthorCallback) => void
} }
export interface AuthorClass { export interface AuthorClass {
@ -16,6 +19,9 @@ export interface AuthorInstance extends AuthorClass, AuthorAttributes, Sequelize
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
podId: number
Pod: PodInstance
} }
export interface AuthorModel extends AuthorClass, Sequelize.Model<AuthorInstance, AuthorAttributes> {} export interface AuthorModel extends AuthorClass, Sequelize.Model<AuthorInstance, AuthorAttributes> {}

View File

@ -74,12 +74,13 @@ function associate (models) {
}) })
} }
findOrCreateAuthor = function (name, podId, userId, transaction, callback) { findOrCreateAuthor = function (
if (!callback) { name: string,
callback = transaction podId: number,
transaction = null userId: number,
} transaction: Sequelize.Transaction,
callback: AuthorMethods.FindOrCreateAuthorCallback
) {
const author = { const author = {
name, name,
podId, podId,
@ -91,7 +92,7 @@ findOrCreateAuthor = function (name, podId, userId, transaction, callback) {
defaults: author defaults: author
} }
if (transaction) query.transaction = transaction if (transaction !== null) query.transaction = transaction
Author.findOrCreate(query).asCallback(function (err, result) { Author.findOrCreate(query).asCallback(function (err, result) {
if (err) return callback(err) if (err) return callback(err)

View File

@ -1,7 +1,8 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace JobMethods { export namespace JobMethods {
export type ListWithLimit = (limit, state, callback) => void export type ListWithLimitCallback = (err: Error, jobInstances: JobInstance[]) => void
export type ListWithLimit = (limit: number, state: string, callback: ListWithLimitCallback) => void
} }
export interface JobClass { export interface JobClass {

View File

@ -48,7 +48,7 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
listWithLimit = function (limit, state, callback) { listWithLimit = function (limit: number, state: string, callback: JobMethods.ListWithLimitCallback) {
const query = { const query = {
order: [ order: [
[ 'id', 'ASC' ] [ 'id', 'ASC' ]

View File

@ -1,8 +1,12 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace OAuthClientMethods { export namespace OAuthClientMethods {
export type CountTotal = (callback) => void export type CountTotalCallback = (err: Error, total: number) => void
export type LoadFirstClient = (callback) => void export type CountTotal = (callback: CountTotalCallback) => void
export type LoadFirstClientCallback = (err: Error, client: OAuthClientInstance) => void
export type LoadFirstClient = (callback: LoadFirstClientCallback) => void
export type GetByIdAndSecret = (clientId, clientSecret) => void export type GetByIdAndSecret = (clientId, clientSecret) => void
} }

View File

@ -67,15 +67,15 @@ function associate (models) {
}) })
} }
countTotal = function (callback) { countTotal = function (callback: OAuthClientMethods.CountTotalCallback) {
return OAuthClient.count().asCallback(callback) return OAuthClient.count().asCallback(callback)
} }
loadFirstClient = function (callback) { loadFirstClient = function (callback: OAuthClientMethods.LoadFirstClientCallback) {
return OAuthClient.findOne().asCallback(callback) return OAuthClient.findOne().asCallback(callback)
} }
getByIdAndSecret = function (clientId, clientSecret) { getByIdAndSecret = function (clientId: string, clientSecret: string) {
const query = { const query = {
where: { where: {
clientId: clientId, clientId: clientId,

View File

@ -1,11 +1,25 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Bluebird from 'bluebird'
import { UserModel } from './user-interface' import { UserModel } from './user-interface'
export type OAuthTokenInfo = {
refreshToken: string
refreshTokenExpiresAt: Date,
client: {
id: number
},
user: {
id: number
}
}
export namespace OAuthTokenMethods { export namespace OAuthTokenMethods {
export type GetByRefreshTokenAndPopulateClient = (refreshToken) => void export type GetByRefreshTokenAndPopulateClient = (refreshToken: string) => Bluebird<OAuthTokenInfo>
export type GetByTokenAndPopulateUser = (bearerToken) => void export type GetByTokenAndPopulateUser = (bearerToken: string) => Bluebird<OAuthTokenInstance>
export type GetByRefreshTokenAndPopulateUser = (refreshToken) => any export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Bluebird<OAuthTokenInstance>
export type RemoveByUserIdCallback = (err: Error) => void
export type RemoveByUserId = (userId, callback) => void export type RemoveByUserId = (userId, callback) => void
} }

View File

@ -8,7 +8,8 @@ import {
OAuthTokenInstance, OAuthTokenInstance,
OAuthTokenAttributes, OAuthTokenAttributes,
OAuthTokenMethods OAuthTokenMethods,
OAuthTokenInfo
} from './oauth-token-interface' } from './oauth-token-interface'
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes> let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
@ -90,7 +91,7 @@ function associate (models) {
}) })
} }
getByRefreshTokenAndPopulateClient = function (refreshToken) { getByRefreshTokenAndPopulateClient = function (refreshToken: string) {
const query = { const query = {
where: { where: {
refreshToken: refreshToken refreshToken: refreshToken
@ -99,9 +100,9 @@ getByRefreshTokenAndPopulateClient = function (refreshToken) {
} }
return OAuthToken.findOne(query).then(function (token) { return OAuthToken.findOne(query).then(function (token) {
if (!token) return token if (!token) return null
const tokenInfos = { const tokenInfos: OAuthTokenInfo = {
refreshToken: token.refreshToken, refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt, refreshTokenExpiresAt: token.refreshTokenExpiresAt,
client: { client: {
@ -118,7 +119,7 @@ getByRefreshTokenAndPopulateClient = function (refreshToken) {
}) })
} }
getByTokenAndPopulateUser = function (bearerToken) { getByTokenAndPopulateUser = function (bearerToken: string) {
const query = { const query = {
where: { where: {
accessToken: bearerToken accessToken: bearerToken
@ -133,7 +134,7 @@ getByTokenAndPopulateUser = function (bearerToken) {
}) })
} }
getByRefreshTokenAndPopulateUser = function (refreshToken) { getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
const query = { const query = {
where: { where: {
refreshToken: refreshToken refreshToken: refreshToken

View File

@ -1,18 +1,39 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace PodMethods { // Don't use barrel, import just what we need
export type ToFormatedJSON = () => void import { Pod as FormatedPod } from '../../shared/models/pod.model'
export namespace PodMethods {
export type ToFormatedJSON = () => FormatedPod
export type CountAllCallback = (err: Error, total: number) => void
export type CountAll = (callback) => void export type CountAll = (callback) => void
export type IncrementScores = (ids, value, callback) => void
export type List = (callback) => void export type IncrementScoresCallback = (err: Error) => void
export type ListAllIds = (transaction, callback) => void export type IncrementScores = (ids: number[], value: number, callback?: IncrementScoresCallback) => void
export type ListRandomPodIdsWithRequest = (limit, tableWithPods, tableWithPodsJoins, callback) => void
export type ListBadPods = (callback) => void export type ListCallback = (err: Error, podInstances?: PodInstance[]) => void
export type Load = (id, callback) => void export type List = (callback: ListCallback) => void
export type LoadByHost = (host, callback) => void
export type RemoveAll = (callback) => void export type ListAllIdsCallback = (err: Error, ids?: number[]) => void
export type UpdatePodsScore = (goodPods, badPods) => void export type ListAllIds = (transaction: Sequelize.Transaction, callback: ListAllIdsCallback) => void
export type ListRandomPodIdsWithRequestCallback = (err: Error, podInstanceIds?: number[]) => void
export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: ListRandomPodIdsWithRequestCallback) => void
export type ListBadPodsCallback = (err: Error, podInstances?: PodInstance[]) => void
export type ListBadPods = (callback: ListBadPodsCallback) => void
export type LoadCallback = (err: Error, podInstance: PodInstance) => void
export type Load = (id: number, callback: LoadCallback) => void
export type LoadByHostCallback = (err: Error, podInstance: PodInstance) => void
export type LoadByHost = (host: string, callback: LoadByHostCallback) => void
export type RemoveAllCallback = (err: Error) => void
export type RemoveAll = (callback: RemoveAllCallback) => void
export type UpdatePodsScore = (goodPods: number[], badPods: number[]) => void
} }
export interface PodClass { export interface PodClass {

View File

@ -118,11 +118,11 @@ function associate (models) {
}) })
} }
countAll = function (callback) { countAll = function (callback: PodMethods.CountAllCallback) {
return Pod.count().asCallback(callback) return Pod.count().asCallback(callback)
} }
incrementScores = function (ids, value, callback) { incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) {
if (!callback) callback = function () { /* empty */ } if (!callback) callback = function () { /* empty */ }
const update = { const update = {
@ -142,35 +142,25 @@ incrementScores = function (ids, value, callback) {
return Pod.update(update, options).asCallback(callback) return Pod.update(update, options).asCallback(callback)
} }
list = function (callback) { list = function (callback: PodMethods.ListCallback) {
return Pod.findAll().asCallback(callback) return Pod.findAll().asCallback(callback)
} }
listAllIds = function (transaction, callback) { listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) {
if (!callback) {
callback = transaction
transaction = null
}
const query: any = { const query: any = {
attributes: [ 'id' ] attributes: [ 'id' ]
} }
if (transaction) query.transaction = transaction if (transaction !== null) query.transaction = transaction
return Pod.findAll(query).asCallback(function (err, pods) { return Pod.findAll(query).asCallback(function (err: Error, pods) {
if (err) return callback(err) if (err) return callback(err)
return callback(null, map(pods, 'id')) return callback(null, map(pods, 'id'))
}) })
} }
listRandomPodIdsWithRequest = function (limit, tableWithPods, tableWithPodsJoins, callback) { listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) {
if (!callback) {
callback = tableWithPodsJoins
tableWithPodsJoins = ''
}
Pod.count().asCallback(function (err, count) { Pod.count().asCallback(function (err, count) {
if (err) return callback(err) if (err) return callback(err)
@ -204,7 +194,7 @@ listRandomPodIdsWithRequest = function (limit, tableWithPods, tableWithPodsJoins
}) })
} }
listBadPods = function (callback) { listBadPods = function (callback: PodMethods.ListBadPodsCallback) {
const query = { const query = {
where: { where: {
score: { $lte: 0 } score: { $lte: 0 }
@ -214,11 +204,11 @@ listBadPods = function (callback) {
return Pod.findAll(query).asCallback(callback) return Pod.findAll(query).asCallback(callback)
} }
load = function (id, callback) { load = function (id: number, callback: PodMethods.LoadCallback) {
return Pod.findById(id).asCallback(callback) return Pod.findById(id).asCallback(callback)
} }
loadByHost = function (host, callback) { loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) {
const query = { const query = {
where: { where: {
host: host host: host
@ -228,11 +218,11 @@ loadByHost = function (host, callback) {
return Pod.findOne(query).asCallback(callback) return Pod.findOne(query).asCallback(callback)
} }
removeAll = function (callback) { removeAll = function (callback: PodMethods.RemoveAllCallback) {
return Pod.destroy().asCallback(callback) return Pod.destroy().asCallback(callback)
} }
updatePodsScore = function (goodPods, badPods) { updatePodsScore = function (goodPods: number[], badPods: number[]) {
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
if (goodPods.length !== 0) { if (goodPods.length !== 0) {

View File

@ -1,12 +1,26 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { PodAttributes } from './pod-interface' import { PodInstance, PodAttributes } from './pod-interface'
export type RequestsGrouped = {
[ podId: number ]: {
request: RequestInstance,
pod: PodInstance
}[]
}
export namespace RequestMethods { export namespace RequestMethods {
export type CountTotalRequests = (callback) => void export type CountTotalRequestsCallback = (err: Error, total: number) => void
export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
export type RemoveWithEmptyTo = (callback) => void
export type RemoveAll = (callback) => void export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsGrouped) => void
export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback: ListWithLimitAndRandomCallback) => void
export type RemoveWithEmptyToCallback = (err: Error) => void
export type RemoveWithEmptyTo = (callback: RemoveWithEmptyToCallback) => void
export type RemoveAllCallback = (err: Error) => void
export type RemoveAll = (callback: RemoveAllCallback) => void
} }
export interface RequestClass { export interface RequestClass {
@ -21,12 +35,13 @@ export interface RequestAttributes {
endpoint: string endpoint: string
} }
export interface RequestInstance extends Sequelize.Instance<RequestAttributes> { export interface RequestInstance extends RequestClass, RequestAttributes, Sequelize.Instance<RequestAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
setPods: Sequelize.HasManySetAssociationsMixin<PodAttributes, number> setPods: Sequelize.HasManySetAssociationsMixin<PodAttributes, number>
Pods: PodInstance[]
} }
export interface RequestModel extends RequestClass, Sequelize.Model<RequestInstance, RequestAttributes> {} export interface RequestModel extends RequestClass, Sequelize.Model<RequestInstance, RequestAttributes> {}

View File

@ -1,7 +1,8 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace RequestToPodMethods { export namespace RequestToPodMethods {
export type RemoveByRequestIdsAndPod = (requestsIds, podId, callback) => void export type RemoveByRequestIdsAndPodCallback = (err: Error) => void
export type RemoveByRequestIdsAndPod = (requestsIds: number[], podId: number, callback?: RemoveByRequestIdsAndPodCallback) => void
} }
export interface RequestToPodClass { export interface RequestToPodClass {
@ -11,7 +12,7 @@ export interface RequestToPodClass {
export interface RequestToPodAttributes { export interface RequestToPodAttributes {
} }
export interface RequestToPodInstance extends Sequelize.Instance<RequestToPodAttributes> { export interface RequestToPodInstance extends RequestToPodClass, RequestToPodAttributes, Sequelize.Instance<RequestToPodAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date

View File

@ -38,7 +38,7 @@ export default function (sequelize, DataTypes) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
removeByRequestIdsAndPod = function (requestsIds, podId, callback) { removeByRequestIdsAndPod = function (requestsIds: number[], podId: number, callback?: RequestToPodMethods.RemoveByRequestIdsAndPodCallback) {
if (!callback) callback = function () { /* empty */ } if (!callback) callback = function () { /* empty */ }
const query = { const query = {

View File

@ -1,10 +1,30 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VideoInstance } from './video-interface'
import { PodInstance } from './pod-interface'
export type RequestsVideoEventGrouped = {
[ podId: number ]: {
id: number
type: string
count: number
video: VideoInstance
pod: PodInstance
}[]
}
export namespace RequestVideoEventMethods { export namespace RequestVideoEventMethods {
export type CountTotalRequests = (callback) => void export type CountTotalRequestsCallback = (err: Error, total: number) => void
export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
export type RemoveByRequestIdsAndPod = (ids, podId, callback) => void
export type RemoveAll = (callback) => void export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsVideoEventGrouped) => void
export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number, callback: ListWithLimitAndRandomCallback) => void
export type RemoveByRequestIdsAndPodCallback = () => void
export type RemoveByRequestIdsAndPod = (ids: number[], podId: number, callback: RemoveByRequestIdsAndPodCallback) => void
export type RemoveAllCallback = () => void
export type RemoveAll = (callback: RemoveAllCallback) => void
} }
export interface RequestVideoEventClass { export interface RequestVideoEventClass {
@ -19,8 +39,10 @@ export interface RequestVideoEventAttributes {
count: number count: number
} }
export interface RequestVideoEventInstance extends Sequelize.Instance<RequestVideoEventAttributes> { export interface RequestVideoEventInstance extends RequestVideoEventClass, RequestVideoEventAttributes, Sequelize.Instance<RequestVideoEventAttributes> {
id: number id: number
Video: VideoInstance
} }
export interface RequestVideoEventModel extends RequestVideoEventClass, Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> {} export interface RequestVideoEventModel extends RequestVideoEventClass, Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> {}

View File

@ -5,16 +5,17 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database'
import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers'
import { isVideoEventCountValid } from '../helpers' import { isVideoEventCountValid } from '../helpers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from './utils'
import { import {
RequestVideoEventClass, RequestVideoEventClass,
RequestVideoEventInstance, RequestVideoEventInstance,
RequestVideoEventAttributes, RequestVideoEventAttributes,
RequestVideoEventMethods RequestVideoEventMethods,
RequestsVideoEventGrouped
} from './request-video-event-interface' } from './request-video-event-interface'
let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
@ -76,13 +77,13 @@ function associate (models) {
}) })
} }
countTotalRequests = function (callback) { countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) {
const query = {} const query = {}
return RequestVideoEvent.count(query).asCallback(callback) return RequestVideoEvent.count(query).asCallback(callback)
} }
listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) {
const Pod = RequestVideoEvent['sequelize'].models.Pod const Pod = db.Pod
// We make a join between videos and authors to find the podId of our video event requests // We make a join between videos and authors to find the podId of our video event requests
const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
@ -129,7 +130,7 @@ listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) {
}) })
} }
removeByRequestIdsAndPod = function (ids, podId, callback) { removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) {
const query = { const query = {
where: { where: {
id: { id: {
@ -154,15 +155,15 @@ removeByRequestIdsAndPod = function (ids, podId, callback) {
RequestVideoEvent.destroy(query).asCallback(callback) RequestVideoEvent.destroy(query).asCallback(callback)
} }
removeAll = function (callback) { removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) {
// Delete all requests // Delete all requests
RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) RequestVideoEvent.truncate({ cascade: true }).asCallback(callback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function groupAndTruncateRequests (events, limitRequestsPerPod) { function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
const eventsGrouped = {} const eventsGrouped: RequestsVideoEventGrouped = {}
events.forEach(function (event) { events.forEach(function (event) {
const pod = event.Video.Author.Pod const pod = event.Video.Author.Pod

View File

@ -1,10 +1,28 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VideoInstance } from './video-interface'
import { PodInstance } from './pod-interface'
export type RequestsVideoQaduGrouped = {
[ podId: number ]: {
request: RequestVideoQaduInstance
video: VideoInstance
pod: PodInstance
}
}
export namespace RequestVideoQaduMethods { export namespace RequestVideoQaduMethods {
export type CountTotalRequests = (callback) => void export type CountTotalRequestsCallback = (err: Error, total: number) => void
export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
export type RemoveByRequestIdsAndPod = (ids, podId, callback) => void
export type RemoveAll = (callback) => void export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsVideoQaduGrouped) => void
export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number, callback: ListWithLimitAndRandomCallback) => void
export type RemoveByRequestIdsAndPodCallback = () => void
export type RemoveByRequestIdsAndPod = (ids: number[], podId: number, callback: RemoveByRequestIdsAndPodCallback) => void
export type RemoveAllCallback = () => void
export type RemoveAll = (callback: RemoveAllCallback) => void
} }
export interface RequestVideoQaduClass { export interface RequestVideoQaduClass {
@ -18,8 +36,11 @@ export interface RequestVideoQaduAttributes {
type: string type: string
} }
export interface RequestVideoQaduInstance extends Sequelize.Instance<RequestVideoQaduAttributes> { export interface RequestVideoQaduInstance extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance<RequestVideoQaduAttributes> {
id: number id: number
Pod: PodInstance
Video: VideoInstance
} }
export interface RequestVideoQaduModel extends RequestVideoQaduClass, Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> {} export interface RequestVideoQaduModel extends RequestVideoQaduClass, Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> {}

View File

@ -12,8 +12,8 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database'
import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' import { REQUEST_VIDEO_QADU_TYPES } from '../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from './utils'
import { import {
RequestVideoQaduClass, RequestVideoQaduClass,
@ -83,15 +83,16 @@ function associate (models) {
}) })
} }
countTotalRequests = function (callback) { countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) {
const query = {} const query = {}
return RequestVideoQadu.count(query).asCallback(callback) return RequestVideoQadu.count(query).asCallback(callback)
} }
listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoQaduMethods.ListWithLimitAndRandomCallback) {
const Pod = RequestVideoQadu['sequelize'].models.Pod const Pod = db.Pod
const tableJoin = ''
Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) { Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin, function (err, podIds) {
if (err) return callback(err) if (err) return callback(err)
// We don't have friends that have requests // We don't have friends that have requests
@ -122,7 +123,7 @@ listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) {
}) })
} }
removeByRequestIdsAndPod = function (ids, podId, callback) { removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoQaduMethods.RemoveByRequestIdsAndPodCallback) {
const query = { const query = {
where: { where: {
id: { id: {
@ -135,14 +136,14 @@ removeByRequestIdsAndPod = function (ids, podId, callback) {
RequestVideoQadu.destroy(query).asCallback(callback) RequestVideoQadu.destroy(query).asCallback(callback)
} }
removeAll = function (callback) { removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) {
// Delete all requests // Delete all requests
RequestVideoQadu.truncate({ cascade: true }).asCallback(callback) RequestVideoQadu.truncate({ cascade: true }).asCallback(callback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function groupAndTruncateRequests (requests, limitRequestsPerPod) { function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) {
const requestsGrouped = {} const requestsGrouped = {}
requests.forEach(function (request) { requests.forEach(function (request) {

View File

@ -1,15 +1,16 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database'
import { REQUEST_ENDPOINTS } from '../initializers' import { REQUEST_ENDPOINTS } from '../initializers'
import { addMethodsToModel } from './utils' import { addMethodsToModel } from './utils'
import { import {
RequestClass, RequestClass,
RequestInstance, RequestInstance,
RequestAttributes, RequestAttributes,
RequestMethods RequestMethods,
RequestsGrouped
} from './request-interface' } from './request-interface'
let Request: Sequelize.Model<RequestInstance, RequestAttributes> let Request: Sequelize.Model<RequestInstance, RequestAttributes>
@ -59,7 +60,7 @@ function associate (models) {
}) })
} }
countTotalRequests = function (callback) { countTotalRequests = function (callback: RequestMethods.CountTotalRequestsCallback) {
// We need to include Pod because there are no cascade delete when a pod is removed // We need to include Pod because there are no cascade delete when a pod is removed
// So we could count requests that do not have existing pod anymore // So we could count requests that do not have existing pod anymore
const query = { const query = {
@ -69,10 +70,11 @@ countTotalRequests = function (callback) {
return Request.count(query).asCallback(callback) return Request.count(query).asCallback(callback)
} }
listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) {
const Pod = Request['sequelize'].models.Pod const Pod = db.Pod
const tableJoin = ''
Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', '', function (err, podIds) {
if (err) return callback(err) if (err) return callback(err)
// We don't have friends that have requests // We don't have friends that have requests
@ -105,12 +107,12 @@ listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) {
}) })
} }
removeAll = function (callback) { removeAll = function (callback: RequestMethods.RemoveAllCallback) {
// Delete all requests // Delete all requests
Request.truncate({ cascade: true }).asCallback(callback) Request.truncate({ cascade: true }).asCallback(callback)
} }
removeWithEmptyTo = function (callback) { removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) {
if (!callback) callback = function () { /* empty */ } if (!callback) callback = function () { /* empty */ }
const query = { const query = {
@ -128,8 +130,8 @@ removeWithEmptyTo = function (callback) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function groupAndTruncateRequests (requests, limitRequestsPerPod) { function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
const requestsGrouped = {} const requestsGrouped: RequestsGrouped = {}
requests.forEach(function (request) { requests.forEach(function (request) {
request.Pods.forEach(function (pod) { request.Pods.forEach(function (pod) {

View File

@ -1,7 +1,8 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace TagMethods { export namespace TagMethods {
export type FindOrCreateTags = (tags, transaction, callback) => void export type FindOrCreateTagsCallback = (err: Error, tagInstances: TagInstance[]) => void
export type FindOrCreateTags = (tags: string[], transaction: Sequelize.Transaction, callback: FindOrCreateTagsCallback) => void
} }
export interface TagClass { export interface TagClass {

View File

@ -52,15 +52,10 @@ function associate (models) {
}) })
} }
findOrCreateTags = function (tags, transaction, callback) { findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, callback: TagMethods.FindOrCreateTagsCallback) {
if (!callback) {
callback = transaction
transaction = null
}
const tagInstances = [] const tagInstances = []
each(tags, function (tag, callbackEach) { each<string, Error>(tags, function (tag, callbackEach) {
const query: any = { const query: any = {
where: { where: {
name: tag name: tag

View File

@ -1,17 +1,35 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Bluebird from 'bluebird'
// Don't use barrel, import just what we need
import { User as FormatedUser } from '../../shared/models/user.model'
export namespace UserMethods { export namespace UserMethods {
export type IsPasswordMatch = (password, callback) => void export type IsPasswordMatchCallback = (err: Error, same: boolean) => void
export type ToFormatedJSON = () => void export type IsPasswordMatch = (password: string, callback: IsPasswordMatchCallback) => void
export type ToFormatedJSON = () => FormatedUser
export type IsAdmin = () => boolean export type IsAdmin = () => boolean
export type CountTotal = (callback) => void export type CountTotalCallback = (err: Error, total: number) => void
export type GetByUsername = (username) => any export type CountTotal = (callback: CountTotalCallback) => void
export type List = (callback) => void
export type ListForApi = (start, count, sort, callback) => void export type GetByUsername = (username: string) => Bluebird<UserInstance>
export type LoadById = (id, callback) => void
export type LoadByUsername = (username, callback) => void export type ListCallback = (err: Error, userInstances: UserInstance[]) => void
export type LoadByUsernameOrEmail = (username, email, callback) => void export type List = (callback: ListCallback) => void
export type ListForApiCallback = (err: Error, userInstances?: UserInstance[], total?: number) => void
export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void
export type LoadByIdCallback = (err: Error, userInstance: UserInstance) => void
export type LoadById = (id: number, callback: LoadByIdCallback) => void
export type LoadByUsernameCallback = (err: Error, userInstance: UserInstance) => void
export type LoadByUsername = (username: string, callback: LoadByUsernameCallback) => void
export type LoadByUsernameOrEmailCallback = (err: Error, userInstance: UserInstance) => void
export type LoadByUsernameOrEmail = (username: string, email: string, callback: LoadByUsernameOrEmailCallback) => void
} }
export interface UserClass { export interface UserClass {

View File

@ -1,6 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace UserVideoRateMethods { export namespace UserVideoRateMethods {
export type LoadCallback = (err: Error, userVideoRateInstance: UserVideoRateInstance) => void
export type Load = (userId, videoId, transaction, callback) => void export type Load = (userId, videoId, transaction, callback) => void
} }
@ -12,7 +13,7 @@ export interface UserVideoRateAttributes {
type: string type: string
} }
export interface UserVideoRateInstance extends Sequelize.Instance<UserVideoRateAttributes> { export interface UserVideoRateInstance extends UserVideoRateClass, UserVideoRateAttributes, Sequelize.Instance<UserVideoRateAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date

View File

@ -67,7 +67,7 @@ function associate (models) {
}) })
} }
load = function (userId, videoId, transaction, callback) { load = function (userId: number, videoId: number, transaction: Sequelize.Transaction, callback: UserVideoRateMethods.LoadCallback) {
const options: Sequelize.FindOptions = { const options: Sequelize.FindOptions = {
where: { where: {
userId, userId,

View File

@ -117,7 +117,7 @@ export default function (sequelize, DataTypes) {
return User return User
} }
function beforeCreateOrUpdate (user, options) { function beforeCreateOrUpdate (user: UserInstance) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
cryptPassword(user.password, function (err, hash) { cryptPassword(user.password, function (err, hash) {
if (err) return reject(err) if (err) return reject(err)
@ -131,7 +131,7 @@ function beforeCreateOrUpdate (user, options) {
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------
isPasswordMatch = function (password, callback) { isPasswordMatch = function (password: string, callback: UserMethods.IsPasswordMatchCallback) {
return comparePassword(password, this.password, callback) return comparePassword(password, this.password, callback)
} }
@ -164,11 +164,11 @@ function associate (models) {
}) })
} }
countTotal = function (callback) { countTotal = function (callback: UserMethods.CountTotalCallback) {
return this.count().asCallback(callback) return this.count().asCallback(callback)
} }
getByUsername = function (username) { getByUsername = function (username: string) {
const query = { const query = {
where: { where: {
username: username username: username
@ -178,11 +178,11 @@ getByUsername = function (username) {
return User.findOne(query) return User.findOne(query)
} }
list = function (callback) { list = function (callback: UserMethods.ListCallback) {
return User.find().asCallback(callback) return User.find().asCallback(callback)
} }
listForApi = function (start, count, sort, callback) { listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) {
const query = { const query = {
offset: start, offset: start,
limit: count, limit: count,
@ -196,11 +196,11 @@ listForApi = function (start, count, sort, callback) {
}) })
} }
loadById = function (id, callback) { loadById = function (id: number, callback: UserMethods.LoadByIdCallback) {
return User.findById(id).asCallback(callback) return User.findById(id).asCallback(callback)
} }
loadByUsername = function (username, callback) { loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) {
const query = { const query = {
where: { where: {
username: username username: username
@ -210,7 +210,7 @@ loadByUsername = function (username, callback) {
return User.findOne(query).asCallback(callback) return User.findOne(query).asCallback(callback)
} }
loadByUsernameOrEmail = function (username, email, callback) { loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) {
const query = { const query = {
where: { where: {
$or: [ { username }, { email } ] $or: [ { username }, { email } ]

View File

@ -1,7 +1,7 @@
// Translate for example "-name" to [ 'name', 'DESC' ] // Translate for example "-name" to [ 'name', 'DESC' ]
function getSort (value) { function getSort (value: string) {
let field let field: string
let direction let direction: 'ASC' | 'DESC'
if (value.substring(0, 1) === '-') { if (value.substring(0, 1) === '-') {
direction = 'DESC' direction = 'DESC'

View File

@ -1,9 +1,13 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace VideoAbuseMethods { // Don't use barrel, import just what we need
export type toFormatedJSON = () => void import { VideoAbuse as FormatedVideoAbuse } from '../../shared/models/video-abuse.model'
export type ListForApi = (start, count, sort, callback) => void export namespace VideoAbuseMethods {
export type toFormatedJSON = () => FormatedVideoAbuse
export type ListForApiCallback = (err: Error, videoAbuseInstances?: VideoAbuseInstance[], total?: number) => void
export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void
} }
export interface VideoAbuseClass { export interface VideoAbuseClass {
@ -15,7 +19,7 @@ export interface VideoAbuseAttributes {
reason: string reason: string
} }
export interface VideoAbuseInstance extends Sequelize.Instance<VideoAbuseAttributes> { export interface VideoAbuseInstance extends VideoAbuseClass, VideoAbuseAttributes, Sequelize.Instance<VideoAbuseAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date

View File

@ -1,13 +1,25 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace BlacklistedVideoMethods { // Don't use barrel, import just what we need
export type ToFormatedJSON = () => void import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../shared/models/video-blacklist.model'
export type CountTotal = (callback) => void export namespace BlacklistedVideoMethods {
export type List = (callback) => void export type ToFormatedJSON = () => FormatedBlacklistedVideo
export type ListForApi = (start, count, sort, callback) => void
export type LoadById = (id, callback) => void export type CountTotalCallback = (err: Error, total: number) => void
export type LoadByVideoId = (id, callback) => void export type CountTotal = (callback: CountTotalCallback) => void
export type ListCallback = (err: Error, backlistedVideoInstances: BlacklistedVideoInstance[]) => void
export type List = (callback: ListCallback) => void
export type ListForApiCallback = (err: Error, blacklistedVIdeoInstances?: BlacklistedVideoInstance[], total?: number) => void
export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void
export type LoadByIdCallback = (err: Error, blacklistedVideoInstance: BlacklistedVideoInstance) => void
export type LoadById = (id: number, callback: LoadByIdCallback) => void
export type LoadByVideoIdCallback = (err: Error, blacklistedVideoInstance: BlacklistedVideoInstance) => void
export type LoadByVideoId = (id: string, callback: LoadByVideoIdCallback) => void
} }
export interface BlacklistedVideoClass { export interface BlacklistedVideoClass {

View File

@ -66,15 +66,15 @@ function associate (models) {
}) })
} }
countTotal = function (callback) { countTotal = function (callback: BlacklistedVideoMethods.CountTotalCallback) {
return BlacklistedVideo.count().asCallback(callback) return BlacklistedVideo.count().asCallback(callback)
} }
list = function (callback) { list = function (callback: BlacklistedVideoMethods.ListCallback) {
return BlacklistedVideo.findAll().asCallback(callback) return BlacklistedVideo.findAll().asCallback(callback)
} }
listForApi = function (start, count, sort, callback) { listForApi = function (start: number, count: number, sort: string, callback: BlacklistedVideoMethods.ListForApiCallback) {
const query = { const query = {
offset: start, offset: start,
limit: count, limit: count,
@ -88,11 +88,11 @@ listForApi = function (start, count, sort, callback) {
}) })
} }
loadById = function (id, callback) { loadById = function (id: number, callback: BlacklistedVideoMethods.LoadByIdCallback) {
return BlacklistedVideo.findById(id).asCallback(callback) return BlacklistedVideo.findById(id).asCallback(callback)
} }
loadByVideoId = function (id, callback) { loadByVideoId = function (id: string, callback: BlacklistedVideoMethods.LoadByIdCallback) {
const query = { const query = {
where: { where: {
videoId: id videoId: id

View File

@ -1,28 +1,101 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
export namespace VideoMethods { import { AuthorInstance } from './author-interface'
export type GenerateMagnetUri = () => void import { VideoTagInstance } from './video-tag-interface'
export type GetVideoFilename = () => void
export type GetThumbnailName = () => void
export type GetPreviewName = () => void
export type GetTorrentName = () => void
export type IsOwned = () => void
export type ToFormatedJSON = () => void
export type ToAddRemoteJSON = (callback) => void
export type ToUpdateRemoteJSON = (callback) => void
export type TranscodeVideofile = (callback) => void
export type GenerateThumbnailFromData = (video, thumbnailData, callback) => void // Don't use barrel, import just what we need
import { Video as FormatedVideo } from '../../shared/models/video.model'
export type FormatedAddRemoteVideo = {
name: string
category: number
licence: number
language: number
nsfw: boolean
description: string
infoHash: string
remoteId: string
author: string
duration: number
thumbnailData: string
tags: string[]
createdAt: Date
updatedAt: Date
extname: string
views: number
likes: number
dislikes: number
}
export type FormatedUpdateRemoteVideo = {
name: string
category: number
licence: number
language: number
nsfw: boolean
description: string
infoHash: string
remoteId: string
author: string
duration: number
tags: string[]
createdAt: Date
updatedAt: Date
extname: string
views: number
likes: number
dislikes: number
}
export namespace VideoMethods {
export type GenerateMagnetUri = () => string
export type GetVideoFilename = () => string
export type GetThumbnailName = () => string
export type GetPreviewName = () => string
export type GetTorrentName = () => string
export type IsOwned = () => boolean
export type ToFormatedJSON = () => FormatedVideo
export type ToAddRemoteJSONCallback = (err: Error, videoFormated?: FormatedAddRemoteVideo) => void
export type ToAddRemoteJSON = (callback: ToAddRemoteJSONCallback) => void
export type ToUpdateRemoteJSON = () => FormatedUpdateRemoteVideo
export type TranscodeVideofileCallback = (err: Error) => void
export type TranscodeVideofile = (callback: TranscodeVideofileCallback) => void
export type GenerateThumbnailFromDataCallback = (err: Error, thumbnailName?: string) => void
export type GenerateThumbnailFromData = (video: VideoInstance, thumbnailData: string, callback: GenerateThumbnailFromDataCallback) => void
export type GetDurationFromFileCallback = (err: Error, duration?: number) => void
export type GetDurationFromFile = (videoPath, callback) => void export type GetDurationFromFile = (videoPath, callback) => void
export type List = (callback) => void
export type ListForApi = (start, count, sort, callback) => void export type ListCallback = () => void
export type LoadByHostAndRemoteId = (fromHost, remoteId, callback) => void export type List = (callback: ListCallback) => void
export type ListOwnedAndPopulateAuthorAndTags = (callback) => void
export type ListOwnedByAuthor = (author, callback) => void export type ListForApiCallback = (err: Error, videoInstances?: VideoInstance[], total?: number) => void
export type Load = (id, callback) => void export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void
export type LoadAndPopulateAuthor = (id, callback) => void
export type LoadAndPopulateAuthorAndPodAndTags = (id, callback) => void export type LoadByHostAndRemoteIdCallback = (err: Error, videoInstance: VideoInstance) => void
export type SearchAndPopulateAuthorAndPodAndTags = (value, field, start, count, sort, callback) => void export type LoadByHostAndRemoteId = (fromHost: string, remoteId: string, callback: LoadByHostAndRemoteIdCallback) => void
export type ListOwnedAndPopulateAuthorAndTagsCallback = (err: Error, videoInstances: VideoInstance[]) => void
export type ListOwnedAndPopulateAuthorAndTags = (callback: ListOwnedAndPopulateAuthorAndTagsCallback) => void
export type ListOwnedByAuthorCallback = (err: Error, videoInstances: VideoInstance[]) => void
export type ListOwnedByAuthor = (author: string, callback: ListOwnedByAuthorCallback) => void
export type LoadCallback = (err: Error, videoInstance: VideoInstance) => void
export type Load = (id: string, callback: LoadCallback) => void
export type LoadAndPopulateAuthorCallback = (err: Error, videoInstance: VideoInstance) => void
export type LoadAndPopulateAuthor = (id: string, callback: LoadAndPopulateAuthorCallback) => void
export type LoadAndPopulateAuthorAndPodAndTagsCallback = (err: Error, videoInstance: VideoInstance) => void
export type LoadAndPopulateAuthorAndPodAndTags = (id: string, callback: LoadAndPopulateAuthorAndPodAndTagsCallback) => void
export type SearchAndPopulateAuthorAndPodAndTagsCallback = (err: Error, videoInstances?: VideoInstance[], total?: number) => void
export type SearchAndPopulateAuthorAndPodAndTags = (value: string, field: string, start: number, count: number, sort: string, callback: SearchAndPopulateAuthorAndPodAndTagsCallback) => void
} }
export interface VideoClass { export interface VideoClass {
@ -64,6 +137,9 @@ export interface VideoAttributes {
views?: number views?: number
likes?: number likes?: number
dislikes?: number dislikes?: number
Author?: AuthorInstance
Tags?: VideoTagInstance[]
} }
export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.Instance<VideoAttributes> { export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.Instance<VideoAttributes> {

View File

@ -9,7 +9,7 @@ export interface VideoTagClass {
export interface VideoTagAttributes { export interface VideoTagAttributes {
} }
export interface VideoTagInstance extends Sequelize.Instance<VideoTagAttributes> { export interface VideoTagInstance extends VideoTagClass, VideoTagAttributes, Sequelize.Instance<VideoTagAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date

View File

@ -11,6 +11,7 @@ import { join } from 'path'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { database as db } from '../initializers/database' import { database as db } from '../initializers/database'
import { VideoTagInstance } from './video-tag-interface'
import { import {
logger, logger,
isVideoNameValid, isVideoNameValid,
@ -266,7 +267,7 @@ export default function (sequelize, DataTypes) {
return Video return Video
} }
function beforeValidate (video, options) { function beforeValidate (video: VideoInstance) {
// Put a fake infoHash if it does not exists yet // Put a fake infoHash if it does not exists yet
if (video.isOwned() && !video.infoHash) { if (video.isOwned() && !video.infoHash) {
// 40 hexa length // 40 hexa length
@ -274,7 +275,7 @@ function beforeValidate (video, options) {
} }
} }
function beforeCreate (video, options) { function beforeCreate (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
const tasks = [] const tasks = []
@ -318,7 +319,7 @@ function beforeCreate (video, options) {
}) })
} }
function afterDestroy (video, options) { function afterDestroy (video: VideoInstance) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
const tasks = [] const tasks = []
@ -401,7 +402,7 @@ generateMagnetUri = function () {
} }
const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName() const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName()
const announce = baseUrlWs + '/tracker/socket' const announce = [ baseUrlWs + '/tracker/socket' ]
const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ] const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
const magnetHash = { const magnetHash = {
@ -496,7 +497,7 @@ toFormatedJSON = function () {
return json return json
} }
toAddRemoteJSON = function (callback) { toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) {
// Get thumbnail data to send to the other pod // Get thumbnail data to send to the other pod
const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
fs.readFile(thumbnailPath, (err, thumbnailData) => { fs.readFile(thumbnailPath, (err, thumbnailData) => {
@ -517,7 +518,7 @@ toAddRemoteJSON = function (callback) {
author: this.Author.name, author: this.Author.name,
duration: this.duration, duration: this.duration,
thumbnailData: thumbnailData.toString('binary'), thumbnailData: thumbnailData.toString('binary'),
tags: map(this.Tags, 'name'), tags: map<VideoTagInstance, string>(this.Tags, 'name'),
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
extname: this.extname, extname: this.extname,
@ -530,7 +531,7 @@ toAddRemoteJSON = function (callback) {
}) })
} }
toUpdateRemoteJSON = function (callback) { toUpdateRemoteJSON = function () {
const json = { const json = {
name: this.name, name: this.name,
category: this.category, category: this.category,
@ -542,7 +543,7 @@ toUpdateRemoteJSON = function (callback) {
remoteId: this.id, remoteId: this.id,
author: this.Author.name, author: this.Author.name,
duration: this.duration, duration: this.duration,
tags: map(this.Tags, 'name'), tags: map<VideoTagInstance, string>(this.Tags, 'name'),
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
extname: this.extname, extname: this.extname,
@ -554,7 +555,7 @@ toUpdateRemoteJSON = function (callback) {
return json return json
} }
transcodeVideofile = function (finalCallback) { transcodeVideofile = function (finalCallback: VideoMethods.TranscodeVideofileCallback) {
const video = this const video = this
const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
@ -591,9 +592,9 @@ transcodeVideofile = function (finalCallback) {
video.save().asCallback(callback) video.save().asCallback(callback)
} }
], function (err) { ], function (err: Error) {
if (err) { if (err) {
// Autodescruction... // Autodesctruction...
video.destroy().asCallback(function (err) { video.destroy().asCallback(function (err) {
if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err }) if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err })
}) })
@ -609,7 +610,7 @@ transcodeVideofile = function (finalCallback) {
// ------------------------------ STATICS ------------------------------ // ------------------------------ STATICS ------------------------------
generateThumbnailFromData = function (video, thumbnailData, callback) { generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string, callback: VideoMethods.GenerateThumbnailFromDataCallback) {
// Creating the thumbnail for a remote video // Creating the thumbnail for a remote video
const thumbnailName = video.getThumbnailName() const thumbnailName = video.getThumbnailName()
@ -621,7 +622,7 @@ generateThumbnailFromData = function (video, thumbnailData, callback) {
}) })
} }
getDurationFromFile = function (videoPath, callback) { getDurationFromFile = function (videoPath: string, callback: VideoMethods.GetDurationFromFileCallback) {
ffmpeg.ffprobe(videoPath, function (err, metadata) { ffmpeg.ffprobe(videoPath, function (err, metadata) {
if (err) return callback(err) if (err) return callback(err)
@ -629,11 +630,11 @@ getDurationFromFile = function (videoPath, callback) {
}) })
} }
list = function (callback) { list = function (callback: VideoMethods.ListCallback) {
return Video.findAll().asCallback(callback) return Video.findAll().asCallback(callback)
} }
listForApi = function (start, count, sort, callback) { listForApi = function (start: number, count: number, sort: string, callback: VideoMethods.ListForApiCallback) {
// Exclude Blakclisted videos from the list // Exclude Blakclisted videos from the list
const query = { const query = {
distinct: true, distinct: true,
@ -658,7 +659,7 @@ listForApi = function (start, count, sort, callback) {
}) })
} }
loadByHostAndRemoteId = function (fromHost, remoteId, callback) { loadByHostAndRemoteId = function (fromHost: string, remoteId: string, callback: VideoMethods.LoadByHostAndRemoteIdCallback) {
const query = { const query = {
where: { where: {
remoteId: remoteId remoteId: remoteId
@ -682,7 +683,7 @@ loadByHostAndRemoteId = function (fromHost, remoteId, callback) {
return Video.findOne(query).asCallback(callback) return Video.findOne(query).asCallback(callback)
} }
listOwnedAndPopulateAuthorAndTags = function (callback) { listOwnedAndPopulateAuthorAndTags = function (callback: VideoMethods.ListOwnedAndPopulateAuthorAndTagsCallback) {
// If remoteId is null this is *our* video // If remoteId is null this is *our* video
const query = { const query = {
where: { where: {
@ -694,7 +695,7 @@ listOwnedAndPopulateAuthorAndTags = function (callback) {
return Video.findAll(query).asCallback(callback) return Video.findAll(query).asCallback(callback)
} }
listOwnedByAuthor = function (author, callback) { listOwnedByAuthor = function (author: string, callback: VideoMethods.ListOwnedByAuthorCallback) {
const query = { const query = {
where: { where: {
remoteId: null remoteId: null
@ -712,11 +713,11 @@ listOwnedByAuthor = function (author, callback) {
return Video.findAll(query).asCallback(callback) return Video.findAll(query).asCallback(callback)
} }
load = function (id, callback) { load = function (id: string, callback: VideoMethods.LoadCallback) {
return Video.findById(id).asCallback(callback) return Video.findById(id).asCallback(callback)
} }
loadAndPopulateAuthor = function (id, callback) { loadAndPopulateAuthor = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorCallback) {
const options = { const options = {
include: [ Video['sequelize'].models.Author ] include: [ Video['sequelize'].models.Author ]
} }
@ -724,7 +725,7 @@ loadAndPopulateAuthor = function (id, callback) {
return Video.findById(id, options).asCallback(callback) return Video.findById(id, options).asCallback(callback)
} }
loadAndPopulateAuthorAndPodAndTags = function (id, callback) { loadAndPopulateAuthorAndPodAndTags = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorAndPodAndTagsCallback) {
const options = { const options = {
include: [ include: [
{ {
@ -738,7 +739,14 @@ loadAndPopulateAuthorAndPodAndTags = function (id, callback) {
return Video.findById(id, options).asCallback(callback) return Video.findById(id, options).asCallback(callback)
} }
searchAndPopulateAuthorAndPodAndTags = function (value, field, start, count, sort, callback) { searchAndPopulateAuthorAndPodAndTags = function (
value: string,
field: string,
start: number,
count: number,
sort: string,
callback: VideoMethods.SearchAndPopulateAuthorAndPodAndTagsCallback
) {
const podInclude: any = { const podInclude: any = {
model: Video['sequelize'].models.Pod, model: Video['sequelize'].models.Pod,
required: false required: false
@ -821,27 +829,27 @@ function createBaseVideosWhere () {
} }
} }
function removeThumbnail (video, callback) { function removeThumbnail (video: VideoInstance, callback: (err: Error) => void) {
const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
fs.unlink(thumbnailPath, callback) fs.unlink(thumbnailPath, callback)
} }
function removeFile (video, callback) { function removeFile (video: VideoInstance, callback: (err: Error) => void) {
const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
fs.unlink(filePath, callback) fs.unlink(filePath, callback)
} }
function removeTorrent (video, callback) { function removeTorrent (video: VideoInstance, callback: (err: Error) => void) {
const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
fs.unlink(torrenPath, callback) fs.unlink(torrenPath, callback)
} }
function removePreview (video, callback) { function removePreview (video: VideoInstance, callback: (err: Error) => void) {
// Same name than video thumnail // Same name than video thumnail
fs.unlink(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) fs.unlink(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
} }
function createTorrentFromVideo (video, videoPath, callback) { function createTorrentFromVideo (video: VideoInstance, videoPath: string, callback: (err: Error) => void) {
const options = { const options = {
announceList: [ announceList: [
[ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ] [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
@ -865,24 +873,23 @@ function createTorrentFromVideo (video, videoPath, callback) {
}) })
} }
function createPreview (video, videoPath, callback) { function createPreview (video: VideoInstance, videoPath: string, callback: (err: Error) => void) {
generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), null, callback)
} }
function createThumbnail (video, videoPath, callback) { function createThumbnail (video: VideoInstance, videoPath: string, callback: (err: Error) => void) {
generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE, callback) generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE, callback)
} }
function generateImage (video, videoPath, folder, imageName, size, callback?) { type GenerateImageCallback = (err: Error, imageName: string) => void
function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string, callback?: GenerateImageCallback) {
const options: any = { const options: any = {
filename: imageName, filename: imageName,
count: 1, count: 1,
folder folder
} }
if (!callback) { if (size) {
callback = size
} else {
options.size = size options.size = size
} }
@ -894,7 +901,7 @@ function generateImage (video, videoPath, folder, imageName, size, callback?) {
.thumbnail(options) .thumbnail(options)
} }
function removeFromBlacklist (video, callback) { function removeFromBlacklist (video: VideoInstance, callback: (err: Error) => void) {
// Find the blacklisted video // Find the blacklisted video
db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) { db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) {
// If an error occured, stop here // If an error occured, stop here
@ -908,7 +915,7 @@ function removeFromBlacklist (video, callback) {
video.destroy().asCallback(callback) video.destroy().asCallback(callback)
} else { } else {
// If haven't found it, simply ignore it and do nothing // If haven't found it, simply ignore it and do nothing
return callback() return callback(null)
} }
}) })
} }

1
shared/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './models'

5
shared/models/index.ts Normal file
View File

@ -0,0 +1,5 @@
export * from './pod.model'
export * from './user.model'
export * from './video-abuse.model'
export * from './video-blacklist.model'
export * from './video.model'

View File

@ -0,0 +1,7 @@
export interface Pod {
id: number,
host: string,
email: string,
score: number,
createdAt: Date
}

View File

@ -0,0 +1,8 @@
export interface User {
id: number
username: string
email: string
displayNSFW: boolean
role: string[]
createdAt: Date
}

View File

@ -0,0 +1,8 @@
export interface VideoAbuse {
id: number
reporterPodHost: string
reason: string
reporterUsername: string
videoId: number
createdAt: Date
}

View File

@ -0,0 +1,5 @@
export interface BlacklistedVideo {
id: number
videoId: number
createdAt: Date
}

View File

@ -0,0 +1,3 @@
export interface Video {
}

View File

@ -52,6 +52,12 @@
version "4.14.64" version "4.14.64"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.64.tgz#979cf3a3d4a368670840bf9b3e448dc33ffe84ee" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.64.tgz#979cf3a3d4a368670840bf9b3e448dc33ffe84ee"
"@types/magnet-uri@^5.1.1":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@types/magnet-uri/-/magnet-uri-5.1.1.tgz#861aaf64c92a3137dd848fefc55cd352a8ea851a"
dependencies:
"@types/node" "*"
"@types/mime@*": "@types/mime@*":
version "0.0.29" version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-0.0.29.tgz#fbcfd330573b912ef59eeee14602bface630754b" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-0.0.29.tgz#fbcfd330573b912ef59eeee14602bface630754b"
@ -66,6 +72,12 @@
dependencies: dependencies:
"@types/express" "*" "@types/express" "*"
"@types/multer@^0.0.34":
version "0.0.34"
resolved "https://registry.yarnpkg.com/@types/multer/-/multer-0.0.34.tgz#4b542b380dcf59bced8b66294654dc67a7fab383"
dependencies:
"@types/express" "*"
"@types/node@*", "@types/node@^7.0.18": "@types/node@*", "@types/node@^7.0.18":
version "7.0.22" version "7.0.22"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.22.tgz#4593f4d828bdd612929478ea40c67b4f403ca255" resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.22.tgz#4593f4d828bdd612929478ea40c67b4f403ca255"