Create types for model enums

pull/71/head
Chocobozzz 2017-06-16 10:36:18 +02:00
parent 70c065d64c
commit ee9e7b61f5
21 changed files with 89 additions and 29 deletions

View File

@ -8,6 +8,7 @@ import {
getRequestVideoEventScheduler
} from '../../lib'
import { authenticate, ensureIsAdmin } from '../../middlewares'
import { RequestSchedulerAttributes } from '../../../shared'
const requestsRouter = express.Router()
@ -44,7 +45,7 @@ function buildRequestSchedulerFunction (requestScheduler: AbstractRequestSchedul
requestScheduler.remainingRequestsCount(function (err, count) {
if (err) return callback(err)
const result = {
const result: RequestSchedulerAttributes = {
totalRequests: count,
requestsLimitPods: requestScheduler.limitPods,
requestsLimitPerPod: requestScheduler.limitPerPod,

View File

@ -3,6 +3,8 @@ import * as validator from 'validator'
import { exists } from './misc'
import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
import { UserRole } from '../../../shared'
const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
function isUserPasswordValid (value: string) {
@ -10,7 +12,7 @@ function isUserPasswordValid (value: string) {
}
function isUserRoleValid (value: string) {
return values(USER_ROLES).indexOf(value) !== -1
return values(USER_ROLES).indexOf(value as UserRole) !== -1
}
function isUserUsernameValid (value: string) {

View File

@ -11,6 +11,7 @@ import {
} from '../../initializers'
import { isUserUsernameValid } from './users'
import { isArray, exists } from './misc'
import { VideoRateType } from '../../../shared'
const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
@ -105,7 +106,7 @@ function isVideoEventCountValid (value: string) {
}
function isVideoRatingTypeValid (value: string) {
return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
}
function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {

View File

@ -4,6 +4,15 @@ import { join } from 'path'
// Do not use barrels, remain constants as independent as possible
import { root, isTestInstance } from '../helpers/core-utils'
import {
UserRole,
VideoRateType,
RequestEndpoint,
RequestVideoEventType,
RequestVideoQaduType,
JobState
} from '../../shared/models'
// ---------------------------------------------------------------------------
const LAST_MIGRATION_VERSION = 50
@ -105,7 +114,7 @@ const CONSTRAINTS_FIELDS = {
}
}
const VIDEO_RATE_TYPES = {
const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
LIKE: 'like',
DISLIKE: 'dislike'
}
@ -198,7 +207,7 @@ const REQUESTS_VIDEO_EVENT_LIMIT_PER_POD = 50
// Number of requests to retry for replay requests module
const RETRY_REQUESTS = 5
const REQUEST_ENDPOINTS = {
const REQUEST_ENDPOINTS: { [ id: string ]: RequestEndpoint } = {
VIDEOS: 'videos'
}
@ -213,13 +222,13 @@ REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
const REQUEST_VIDEO_QADU_ENDPOINT = 'videos/qadu'
const REQUEST_VIDEO_EVENT_ENDPOINT = 'videos/events'
const REQUEST_VIDEO_QADU_TYPES = {
const REQUEST_VIDEO_QADU_TYPES: { [ id: string ]: RequestVideoQaduType } = {
LIKES: 'likes',
DISLIKES: 'dislikes',
VIEWS: 'views'
}
const REQUEST_VIDEO_EVENT_TYPES = {
const REQUEST_VIDEO_EVENT_TYPES: { [ id: string ]: RequestVideoEventType } = {
LIKES: 'likes',
DISLIKES: 'dislikes',
VIEWS: 'views'
@ -230,7 +239,7 @@ const REMOTE_SCHEME = {
WS: 'wss'
}
const JOB_STATES = {
const JOB_STATES: { [ id: string ]: JobState } = {
PENDING: 'pending',
PROCESSING: 'processing',
ERROR: 'error',
@ -271,7 +280,7 @@ const PREVIEWS_SIZE = '640x480'
// ---------------------------------------------------------------------------
const USER_ROLES = {
const USER_ROLES: { [ id: string ]: UserRole } = {
ADMIN: 'admin',
USER: 'user'
}

View File

@ -28,10 +28,18 @@ import {
RequestVideoEventScheduler,
RequestVideoEventSchedulerOptions
} from './request'
import { PodInstance, VideoInstance } from '../models'
import {
PodInstance,
VideoInstance
} from '../models'
import {
RequestEndpoint,
RequestVideoEventType,
RequestVideoQaduType
} from '../../shared'
type QaduParam = { videoId: string, type: string }
type EventParam = { videoId: string, type: string }
type QaduParam = { videoId: string, type: RequestVideoQaduType }
type EventParam = { videoId: string, type: RequestVideoEventType }
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
@ -391,7 +399,7 @@ function makeRequestsToWinningPods (cert: string, podsList: PodInstance[], callb
// Wrapper that populate "toIds" argument with all our friends if it is not specified
type CreateRequestOptions = {
type: string
endpoint: string
endpoint: RequestEndpoint
data: Object
toIds?: number[]
transaction: Sequelize.Transaction

View File

@ -7,10 +7,11 @@ import {
REQUESTS_LIMIT_PODS,
REQUESTS_LIMIT_PER_POD
} from '../../initializers'
import { RequestEndpoint } from '../../../shared'
export type RequestSchedulerOptions = {
type: string
endpoint: string
endpoint: RequestEndpoint
data: Object
toIds: number[]
transaction: Sequelize.Transaction

View File

@ -7,9 +7,10 @@ import {
REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
REQUEST_VIDEO_EVENT_ENDPOINT
} from '../../initializers'
import { RequestVideoEventType } from '../../../shared'
export type RequestVideoEventSchedulerOptions = {
type: string
type: RequestVideoEventType
videoId: string
count?: number
transaction?: Sequelize.Transaction

View File

@ -9,9 +9,10 @@ import {
REQUEST_VIDEO_QADU_ENDPOINT,
REQUEST_VIDEO_QADU_TYPES
} from '../../initializers'
import { RequestVideoQaduType } from '../../../shared'
export type RequestVideoQaduSchedulerOptions = {
type: string
type: RequestVideoQaduType
videoId: string
transaction?: Sequelize.Transaction
}

View File

@ -1,8 +1,10 @@
import * as Sequelize from 'sequelize'
import { JobState } from '../../../shared/models/job.model'
export namespace JobMethods {
export type ListWithLimitCallback = (err: Error, jobInstances: JobInstance[]) => void
export type ListWithLimit = (limit: number, state: string, callback: ListWithLimitCallback) => void
export type ListWithLimit = (limit: number, state: JobState, callback: ListWithLimitCallback) => void
}
export interface JobClass {
@ -10,7 +12,7 @@ export interface JobClass {
}
export interface JobAttributes {
state: string
state: JobState
handlerName: string
handlerInputData: object
}

View File

@ -11,6 +11,7 @@ import {
JobMethods
} from './job-interface'
import { JobState } from '../../../shared/models/job.model'
let Job: Sequelize.Model<JobInstance, JobAttributes>
let listWithLimit: JobMethods.ListWithLimit
@ -48,7 +49,7 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se
// ---------------------------------------------------------------------------
listWithLimit = function (limit: number, state: string, callback: JobMethods.ListWithLimitCallback) {
listWithLimit = function (limit: number, state: JobState, callback: JobMethods.ListWithLimitCallback) {
const query = {
order: [
[ 'id', 'ASC' ]

View File

@ -1,6 +1,7 @@
import * as Sequelize from 'sequelize'
import { PodInstance, PodAttributes } from '../pod'
import { RequestEndpoint } from '../../../shared/models/request-scheduler.model'
export type RequestsGrouped = {
[ podId: number ]: {
@ -32,7 +33,7 @@ export interface RequestClass {
export interface RequestAttributes {
request: object
endpoint: string
endpoint: RequestEndpoint
}
export interface RequestInstance extends RequestClass, RequestAttributes, Sequelize.Instance<RequestAttributes> {

View File

@ -3,10 +3,12 @@ import * as Sequelize from 'sequelize'
import { VideoInstance } from '../video'
import { PodInstance } from '../pod'
import { RequestVideoEventType } from '../../../shared/models/request-scheduler.model'
export type RequestsVideoEventGrouped = {
[ podId: number ]: {
id: number
type: string
type: RequestVideoEventType
count: number
video: VideoInstance
pod: PodInstance
@ -35,7 +37,7 @@ export interface RequestVideoEventClass {
}
export interface RequestVideoEventAttributes {
type: string
type: RequestVideoEventType
count: number
}

View File

@ -3,6 +3,8 @@ import * as Sequelize from 'sequelize'
import { VideoInstance } from '../video'
import { PodInstance } from '../pod'
import { RequestVideoQaduType } from '../../../shared/models/request-scheduler.model'
export type RequestsVideoQaduGrouped = {
[ podId: number ]: {
request: RequestVideoQaduInstance
@ -33,7 +35,7 @@ export interface RequestVideoQaduClass {
}
export interface RequestVideoQaduAttributes {
type: string
type: RequestVideoQaduType
}
export interface RequestVideoQaduInstance extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance<RequestVideoQaduAttributes> {

View File

@ -2,7 +2,7 @@ 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'
import { UserRole, User as FormatedUser } from '../../../shared/models/user.model'
export namespace UserMethods {
export type IsPasswordMatchCallback = (err: Error, same: boolean) => void
@ -51,7 +51,7 @@ export interface UserAttributes {
username: string
email: string
displayNSFW?: boolean
role: string
role: UserRole
}
export interface UserInstance extends UserClass, UserAttributes, Sequelize.Instance<UserAttributes> {

View File

@ -1,8 +1,10 @@
import * as Sequelize from 'sequelize'
import { VideoRateType } from '../../../shared/models/user-video-rate.model'
export namespace UserVideoRateMethods {
export type LoadCallback = (err: Error, userVideoRateInstance: UserVideoRateInstance) => void
export type Load = (userId, videoId, transaction, callback) => void
export type Load = (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: LoadCallback) => void
}
export interface UserVideoRateClass {
@ -10,7 +12,7 @@ export interface UserVideoRateClass {
}
export interface UserVideoRateAttributes {
type: string
type: VideoRateType
}
export interface UserVideoRateInstance extends UserVideoRateClass, UserVideoRateAttributes, Sequelize.Instance<UserVideoRateAttributes> {

View File

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

View File

@ -1,4 +1,7 @@
export * from './job.model'
export * from './pod.model'
export * from './request-scheduler.model'
export * from './user-video-rate.model'
export * from './user.model'
export * from './video-abuse.model'
export * from './video-blacklist.model'

View File

@ -0,0 +1 @@
export type JobState = 'pending' | 'processing' | 'error' | 'success'

View File

@ -0,0 +1,19 @@
export type RequestEndpoint = 'videos'
export type RequestVideoQaduType = 'likes' | 'dislikes' | 'views'
export type RequestVideoEventType = 'likes' | 'dislikes' | 'views'
export type RequestSchedulerAttributes = {
totalRequests: number
requestsLimitPods: number
requestsLimitPerPod: number
remainingMilliSeconds: number
milliSecondsInterval: number
}
export interface RequestScheduler {
requestScheduler: RequestSchedulerAttributes
requestVideoQaduScheduler: RequestSchedulerAttributes
requestVideoEventScheduler: RequestSchedulerAttributes
}

View File

@ -0,0 +1 @@
export type VideoRateType = 'like' | 'dislike'

View File

@ -1,8 +1,10 @@
export type UserRole = 'admin' | 'user'
export interface User {
id: number
username: string
email: string
displayNSFW: boolean
role: string
role: UserRole
createdAt: Date
}