]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Create types for model enums
authorChocobozzz <florian.bigard@gmail.com>
Fri, 16 Jun 2017 08:36:18 +0000 (10:36 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Fri, 16 Jun 2017 08:36:18 +0000 (10:36 +0200)
21 files changed:
server/controllers/api/requests.ts
server/helpers/custom-validators/users.ts
server/helpers/custom-validators/videos.ts
server/initializers/constants.ts
server/lib/friends.ts
server/lib/request/request-scheduler.ts
server/lib/request/request-video-event-scheduler.ts
server/lib/request/request-video-qadu-scheduler.ts
server/models/job/job-interface.ts
server/models/job/job.ts
server/models/request/request-interface.ts
server/models/request/request-video-event-interface.ts
server/models/request/request-video-qadu-interface.ts
server/models/user/user-interface.ts
server/models/user/user-video-rate-interface.ts
server/models/user/user-video-rate.ts
shared/models/index.ts
shared/models/job.model.ts [new file with mode: 0644]
shared/models/request-scheduler.model.ts [new file with mode: 0644]
shared/models/user-video-rate.model.ts [new file with mode: 0644]
shared/models/user.model.ts

index d3f0d958c290f75a0036c00af302c30de66a5352..5718b59b7a351fc68e30ca1d03216dac65cf643b 100644 (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,
index 7792ffd74eb82865f65dede8f37a36a6373b5dc7..750563adab77f769d8da779b35f9b7693c4fdc0e 100644 (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) {
index 8904975c0dc4c11b9f49331942cefebb446fc2a2..72d226e81e6813802b369e197cddb4ed9d01877e 100644 (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[] }) {
index b476ef9282b03d3a515212929eef0ab50f06d19b..bf99f4df6925425c7de5a02e8af2df65cb8e5620 100644 (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'
 }
index e097f925488c8c108f19b24c69a7a8da1fdb7014..d07433a5dd72b97445d3777f60f1bd232b9a350d 100644 (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
index 38c0cca3dd4b8e559d4bd009dc2da02a21cc537e..575e0227c8e397cb60c31703c159d660bb7a008d 100644 (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
index c9d1651177a89ce7bec12577da74bc521077e65a..4bb76f4c93537141280f8c263c9ad1ff25ee917c 100644 (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
index 49970a2e6a8c8b390a5f41af59d027d962985ba6..d7169cc815f46c73b3e69fb8018ef824b447d3c9 100644 (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
 }
index ab66782571e2c34cdc33467f2a2c1fd4fa79b46a..31b3773670d0eab841efee7c2c4c30d13202f105 100644 (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
 }
index 60a6c551b4cf4c8695a3f0532902cc1503bb9d95..38e4e8f30aac5b71acd2b5071dfd821153073738 100644 (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' ]
index 70fd734e1eb4ebe17e0f8c8711f3a51acd18056f..48385063389a0819f7549058580520635fe38408 100644 (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> {
index 219d8edc0981a2c5cba2ec2924a815348d28758e..3ed03339a4ff192997e40ba076ba021831c05e38 100644 (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
 }
 
index 625b063dcc60709a98deb69de738a83f30f1fc28..805771cdadcfb071e7278c870ca9f24f47cc9630 100644 (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> {
index 6726e8ab5f41d6940306c5a747702af48a255414..fd98a042e06e75c66fda005df6b700a0d6022a35 100644 (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> {
index e48869fd22a676d6d6a928579fb6b44f08cc3ffd..a726639b1e7578b347cb13c68bcd2db6cc1f4d90 100644 (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> {
index 68be62fc22c9e84cdaed362229f132c176b861dc..1094eb281e68207982ce1509b34b8bede17022bb 100644 (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,
index b498d620a662ac83ba5b23b1f02ae99227a87367..1ddc8545f8303f9a424e7dcc8c271cdf70dfa0aa 100644 (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'
diff --git a/shared/models/job.model.ts b/shared/models/job.model.ts
new file mode 100644 (file)
index 0000000..411c914
--- /dev/null
@@ -0,0 +1 @@
+export type JobState = 'pending' | 'processing' | 'error' | 'success'
diff --git a/shared/models/request-scheduler.model.ts b/shared/models/request-scheduler.model.ts
new file mode 100644 (file)
index 0000000..f94ccfc
--- /dev/null
@@ -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
+}
diff --git a/shared/models/user-video-rate.model.ts b/shared/models/user-video-rate.model.ts
new file mode 100644 (file)
index 0000000..d48774a
--- /dev/null
@@ -0,0 +1 @@
+export type VideoRateType = 'like' | 'dislike'
index a6be359d3209803a127411d72871a63d44955c5e..92dc73ec2a2fb35f5c3f38e43013aefbdf6fdfcd 100644 (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
 }