1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Better typescript typing for a better world

This commit is contained in:
Chocobozzz 2017-07-10 19:43:21 +02:00
parent 7a214f746b
commit 4771e0008d
59 changed files with 400 additions and 166 deletions

View file

@ -2,6 +2,7 @@ import * as express from 'express'
import { database as db } from '../../../initializers/database'
import { checkSignature, signatureValidator } from '../../../middlewares'
import { PodSignature } from '../../../../shared'
const remotePodsRouter = express.Router()
@ -21,12 +22,11 @@ export {
// ---------------------------------------------------------------------------
function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
const host = req.body.signature.host
const signature: PodSignature = req.body.signature
const host = signature.host
db.Pod.loadByHost(host)
.then(pod => {
return pod.destroy()
})
.then(pod => pod.destroy())
.then(() => res.type('json').status(204).end())
.catch(err => next(err))
}

View file

@ -18,6 +18,17 @@ import {
import { logger, retryTransactionWrapper } from '../../../helpers'
import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib'
import { PodInstance, VideoInstance } from '../../../models'
import {
RemoteVideoRequest,
RemoteVideoCreateData,
RemoteVideoUpdateData,
RemoteVideoRemoveData,
RemoteVideoReportAbuseData,
RemoteQaduVideoRequest,
RemoteQaduVideoData,
RemoteVideoEventRequest,
RemoteVideoEventData
} from '../../../../shared'
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
@ -60,11 +71,11 @@ export {
// ---------------------------------------------------------------------------
function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data
const requests: RemoteVideoRequest[] = req.body.data
const fromPod = res.locals.secure.pod
// We need to process in the same order to keep consistency
Promise.each(requests, (request: any) => {
Promise.each(requests, request => {
const data = request.data
// Get the function we need to call in order to process the request
@ -83,10 +94,10 @@ function remoteVideos (req: express.Request, res: express.Response, next: expres
}
function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data
const requests: RemoteQaduVideoRequest[] = req.body.data
const fromPod = res.locals.secure.pod
Promise.each(requests, (request: any) => {
Promise.each(requests, request => {
const videoData = request.data
return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod)
@ -97,10 +108,10 @@ function remoteVideosQadu (req: express.Request, res: express.Response, next: ex
}
function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) {
const requests = req.body.data
const requests: RemoteVideoEventRequest[] = req.body.data
const fromPod = res.locals.secure.pod
Promise.each(requests, (request: any) => {
Promise.each(requests, request => {
const eventData = request.data
return processVideosEventsRetryWrapper(eventData, fromPod)
@ -110,7 +121,7 @@ function remoteVideosEvents (req: express.Request, res: express.Response, next:
return res.type('json').status(204).end()
}
function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance) {
function processVideosEventsRetryWrapper (eventData: RemoteVideoEventData, fromPod: PodInstance) {
const options = {
arguments: [ eventData, fromPod ],
errorMessage: 'Cannot process videos events with many retries.'
@ -119,7 +130,7 @@ function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance)
return retryTransactionWrapper(processVideosEvents, options)
}
function processVideosEvents (eventData: any, fromPod: PodInstance) {
function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInstance) {
return db.sequelize.transaction(t => {
return fetchOwnedVideo(eventData.remoteId)
@ -172,7 +183,7 @@ function processVideosEvents (eventData: any, fromPod: PodInstance) {
})
}
function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, fromPod: PodInstance) {
function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
const options = {
arguments: [ videoData, fromPod ],
errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
@ -181,7 +192,7 @@ function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, fromPod: PodInsta
return retryTransactionWrapper(quickAndDirtyUpdateVideo, options)
}
function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance) {
function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
let videoName
return db.sequelize.transaction(t => {
@ -211,7 +222,7 @@ function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance) {
}
// Handle retries on fail
function addRemoteVideoRetryWrapper (videoToCreateData: any, fromPod: PodInstance) {
function addRemoteVideoRetryWrapper (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) {
const options = {
arguments: [ videoToCreateData, fromPod ],
errorMessage: 'Cannot insert the remote video with many retries.'
@ -220,7 +231,7 @@ function addRemoteVideoRetryWrapper (videoToCreateData: any, fromPod: PodInstanc
return retryTransactionWrapper(addRemoteVideo, options)
}
function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance) {
function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) {
logger.debug('Adding remote video "%s".', videoToCreateData.remoteId)
return db.sequelize.transaction(t => {
@ -293,7 +304,7 @@ function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance) {
}
// Handle retries on fail
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, fromPod: PodInstance) {
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) {
const options = {
arguments: [ videoAttributesToUpdate, fromPod ],
errorMessage: 'Cannot update the remote video with many retries'
@ -302,7 +313,7 @@ function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, fromPod: P
return retryTransactionWrapper(updateRemoteVideo, options)
}
function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance) {
function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) {
logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId)
return db.sequelize.transaction(t => {
@ -346,7 +357,7 @@ function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance)
})
}
function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance) {
function removeRemoteVideo (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) {
// We need the instance because we have to remove some other stuffs (thumbnail etc)
return fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId)
.then(video => {
@ -358,7 +369,7 @@ function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance) {
})
}
function reportAbuseRemoteVideo (reportData: any, fromPod: PodInstance) {
function reportAbuseRemoteVideo (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
return fetchOwnedVideo(reportData.videoRemoteId)
.then(video => {
logger.debug('Reporting remote abuse for video %s.', video.id)

View file

@ -17,7 +17,7 @@ import {
setUsersSort,
token
} from '../../middlewares'
import { UserVideoRate as FormatedUserVideoRate } from '../../../shared'
import { UserVideoRate as FormatedUserVideoRate, UserCreate, UserUpdate } from '../../../shared'
const usersRouter = express.Router()
@ -78,10 +78,12 @@ export {
// ---------------------------------------------------------------------------
function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const body: UserCreate = req.body
const user = db.User.build({
username: req.body.username,
password: req.body.password,
email: req.body.email,
username: body.username,
password: body.password,
email: body.email,
displayNSFW: false,
role: USER_ROLES.USER
})
@ -132,10 +134,12 @@ function removeUser (req: express.Request, res: express.Response, next: express.
}
function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const body: UserUpdate = req.body
db.User.loadByUsername(res.locals.oauth.token.user.username)
.then(user => {
if (req.body.password) user.password = req.body.password
if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
if (body.password) user.password = body.password
if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
return user.save()
})

View file

@ -17,6 +17,7 @@ import {
setPagination
} from '../../../middlewares'
import { VideoInstance } from '../../../models'
import { VideoAbuseCreate } from '../../../../shared'
const abuseVideoRouter = express.Router()
@ -63,10 +64,11 @@ function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Respon
function reportVideoAbuse (req: express.Request, res: express.Response) {
const videoInstance = res.locals.video
const reporterUsername = res.locals.oauth.token.User.username
const body: VideoAbuseCreate = req.body
const abuse = {
reporterUsername,
reason: req.body.reason,
reason: body.reason,
videoId: videoInstance.id,
reporterPodId: null // This is our pod that reported this abuse
}

View file

@ -39,6 +39,7 @@ import {
renamePromise
} from '../../../helpers'
import { TagInstance } from '../../../models'
import { VideoCreate, VideoUpdate } from '../../../../shared'
import { abuseVideoRouter } from './abuse'
import { blacklistRouter } from './blacklist'
@ -155,7 +156,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next
}
function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) {
const videoInfos = req.body
const videoInfos: VideoCreate = req.body
return db.sequelize.transaction(t => {
const user = res.locals.oauth.token.User
@ -257,7 +258,7 @@ function updateVideoRetryWrapper (req: express.Request, res: express.Response, n
function updateVideo (req: express.Request, res: express.Response) {
const videoInstance = res.locals.video
const videoFieldsSave = videoInstance.toJSON()
const videoInfosToUpdate = req.body
const videoInfosToUpdate: VideoUpdate = req.body
return db.sequelize.transaction(t => {
let tagsPromise: Promise<TagInstance[]>

View file

@ -1,4 +1,5 @@
import * as express from 'express'
import * as Promise from 'bluebird'
import { database as db } from '../../../initializers/database'
import {
@ -18,6 +19,7 @@ import {
authenticate,
videoRateValidator
} from '../../../middlewares'
import { UserVideoRateUpdate, VideoRateType } from '../../../../shared'
const rateVideoRouter = express.Router()
@ -47,7 +49,8 @@ function rateVideoRetryWrapper (req: express.Request, res: express.Response, nex
}
function rateVideo (req: express.Request, res: express.Response) {
const rateType = req.body.rating
const body: UserVideoRateUpdate = req.body
const rateType = body.rating
const videoInstance = res.locals.video
const userInstance = res.locals.oauth.token.User
@ -62,24 +65,34 @@ function rateVideo (req: express.Request, res: express.Response) {
if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
let promise: Promise<any>
// There was a previous rate, update it
if (previousRate) {
// We will remove the previous rate, so we will need to remove it from the video attribute
if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
previousRate.type = rateType
if (rateType === 'none') { // Destroy previous rate
promise = previousRate.destroy()
} else { // Update previous rate
previousRate.type = rateType as VideoRateType
return previousRate.save(options).then(() => ({ t, likesToIncrement, dislikesToIncrement }))
} else { // There was not a previous rate, insert a new one
promise = previousRate.save()
}
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
const query = {
userId: userInstance.id,
videoId: videoInstance.id,
type: rateType
}
return db.UserVideoRate.create(query, options).then(() => ({ likesToIncrement, dislikesToIncrement }))
promise = db.UserVideoRate.create(query, options)
} else {
promise = Promise.resolve()
}
return promise.then(() => ({ likesToIncrement, dislikesToIncrement }))
})
.then(({ likesToIncrement, dislikesToIncrement }) => {
const options = { transaction: t }