Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Create types for model enums

Chocobozzzee9e7b61f59a
diff --git a/server/controllers/api/requests.ts b/server/controllers/api/requests.ts
index d3f0d958c2..5718b59b7a 100644
--- a/server/controllers/api/requests.ts
+++ b/server/controllers/api/requests.ts
@@ -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,
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index 7792ffd74e..750563adab 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -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) {
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 8904975c0d..72d226e81e 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -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[] }) {
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index b476ef9282..bf99f4df69 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -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'
}
diff --git a/server/lib/friends.ts b/server/lib/friends.ts
index e097f92548..d07433a5dd 100644
--- a/server/lib/friends.ts
+++ b/server/lib/friends.ts
@@ -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
diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts
index 38c0cca3dd..575e0227c8 100644
--- a/server/lib/request/request-scheduler.ts
+++ b/server/lib/request/request-scheduler.ts
@@ -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
diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts
index c9d1651177..4bb76f4c93 100644
--- a/server/lib/request/request-video-event-scheduler.ts
+++ b/server/lib/request/request-video-event-scheduler.ts
@@ -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
diff --git a/server/lib/request/request-video-qadu-scheduler.ts b/server/lib/request/request-video-qadu-scheduler.ts
index 49970a2e6a..d7169cc815 100644
--- a/server/lib/request/request-video-qadu-scheduler.ts
+++ b/server/lib/request/request-video-qadu-scheduler.ts
@@ -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
}
diff --git a/server/models/job/job-interface.ts b/server/models/job/job-interface.ts
index ab66782571..31b3773670 100644
--- a/server/models/job/job-interface.ts
+++ b/server/models/job/job-interface.ts
@@ -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
}
diff --git a/server/models/job/job.ts b/server/models/job/job.ts
index 60a6c551b4..38e4e8f30a 100644
--- a/server/models/job/job.ts
+++ b/server/models/job/job.ts
@@ -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' ]
diff --git a/server/models/request/request-interface.ts b/server/models/request/request-interface.ts
index 70fd734e1e..4838506338 100644
--- a/server/models/request/request-interface.ts
+++ b/server/models/request/request-interface.ts
@@ -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> {
diff --git a/server/models/request/request-video-event-interface.ts b/server/models/request/request-video-event-interface.ts
index 219d8edc09..3ed03339a4 100644
--- a/server/models/request/request-video-event-interface.ts
+++ b/server/models/request/request-video-event-interface.ts
@@ -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
}
diff --git a/server/models/request/request-video-qadu-interface.ts b/server/models/request/request-video-qadu-interface.ts
index 625b063dcc..805771cdad 100644
--- a/server/models/request/request-video-qadu-interface.ts
+++ b/server/models/request/request-video-qadu-interface.ts
@@ -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> {
diff --git a/server/models/user/user-interface.ts b/server/models/user/user-interface.ts
index 6726e8ab5f..fd98a042e0 100644
--- a/server/models/user/user-interface.ts
+++ b/server/models/user/user-interface.ts
@@ -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> {
diff --git a/server/models/user/user-video-rate-interface.ts b/server/models/user/user-video-rate-interface.ts
index e48869fd22..a726639b1e 100644
--- a/server/models/user/user-video-rate-interface.ts
+++ b/server/models/user/user-video-rate-interface.ts
@@ -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> {
diff --git a/server/models/user/user-video-rate.ts b/server/models/user/user-video-rate.ts
index 68be62fc22..1094eb281e 100644
--- a/server/models/user/user-video-rate.ts
+++ b/server/models/user/user-video-rate.ts
@@ -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,
diff --git a/shared/models/index.ts b/shared/models/index.ts
index b498d620a6..1ddc8545f8 100644
--- a/shared/models/index.ts
+++ b/shared/models/index.ts
@@ -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
index 0000000000..411c91482b
--- /dev/null
+++ b/shared/models/job.model.ts
@@ -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
index 0000000000..f94ccfcabd
--- /dev/null
+++ b/shared/models/request-scheduler.model.ts
@@ -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
index 0000000000..d48774a4b4
--- /dev/null
+++ b/shared/models/user-video-rate.model.ts
@@ -0,0 +1 @@
+export type VideoRateType = 'like' | 'dislike'
diff --git a/shared/models/user.model.ts b/shared/models/user.model.ts
index a6be359d32..92dc73ec2a 100644
--- a/shared/models/user.model.ts
+++ b/shared/models/user.model.ts
@@ -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
}