From 69818c9394366b954b6ba3bd697bd9d2b09f2a16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 10 Jun 2017 22:15:25 +0200 Subject: Type functions --- server/models/application-interface.ts | 7 +- server/models/application.ts | 13 +-- server/models/author-interface.ts | 8 +- server/models/author.ts | 15 ++-- server/models/job-interface.ts | 3 +- server/models/job.ts | 2 +- server/models/oauth-client-interface.ts | 8 +- server/models/oauth-client.ts | 6 +- server/models/oauth-token-interface.ts | 20 ++++- server/models/oauth-token.ts | 13 +-- server/models/pod-interface.ts | 41 ++++++--- server/models/pod.ts | 34 +++---- server/models/request-interface.ts | 27 ++++-- server/models/request-to-pod-interface.ts | 5 +- server/models/request-to-pod.ts | 2 +- server/models/request-video-event-interface.ts | 32 +++++-- server/models/request-video-event.ts | 19 ++-- server/models/request-video-qadu-interface.ts | 31 +++++-- server/models/request-video-qadu.ts | 17 ++-- server/models/request.ts | 22 ++--- server/models/tag-interface.ts | 3 +- server/models/tag.ts | 9 +- server/models/user-interface.ts | 36 ++++++-- server/models/user-video-rate-interface.ts | 3 +- server/models/user-video-rate.ts | 2 +- server/models/user.ts | 18 ++-- server/models/utils.ts | 6 +- server/models/video-abuse-interface.ts | 10 ++- server/models/video-blacklist-interface.ts | 24 +++-- server/models/video-blacklist.ts | 10 +-- server/models/video-interface.ts | 118 ++++++++++++++++++++----- server/models/video-tag-interface.ts | 2 +- server/models/video.ts | 79 +++++++++-------- 33 files changed, 429 insertions(+), 216 deletions(-) (limited to 'server/models') diff --git a/server/models/application-interface.ts b/server/models/application-interface.ts index 826d25df0..c03513db1 100644 --- a/server/models/application-interface.ts +++ b/server/models/application-interface.ts @@ -1,8 +1,11 @@ import * as Sequelize from 'sequelize' export namespace ApplicationMethods { - export type LoadMigrationVersion = (callback: (err: Error, version: number) => void) => void - export type UpdateMigrationVersion = (newVersion: number, transaction: any, callback: any) => void + export type LoadMigrationVersionCallback = (err: Error, version: number) => 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 { diff --git a/server/models/application.ts b/server/models/application.ts index acd0dfbf2..14b87777a 100644 --- a/server/models/application.ts +++ b/server/models/application.ts @@ -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 = { 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 = { - where: {} - } - - if (!callback) { - transaction = callback - } else { - options.transaction = transaction + where: {}, + transaction: transaction } return Application.update({ migrationVersion: newVersion }, options).asCallback(callback) diff --git a/server/models/author-interface.ts b/server/models/author-interface.ts index d2475c3bd..b57ce2a6b 100644 --- a/server/models/author-interface.ts +++ b/server/models/author-interface.ts @@ -1,7 +1,10 @@ import * as Sequelize from 'sequelize' +import { PodInstance } from './pod-interface' + 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 { @@ -16,6 +19,9 @@ export interface AuthorInstance extends AuthorClass, AuthorAttributes, Sequelize id: number createdAt: Date updatedAt: Date + + podId: number + Pod: PodInstance } export interface AuthorModel extends AuthorClass, Sequelize.Model {} diff --git a/server/models/author.ts b/server/models/author.ts index b543d17a0..3264d3a88 100644 --- a/server/models/author.ts +++ b/server/models/author.ts @@ -74,12 +74,13 @@ function associate (models) { }) } -findOrCreateAuthor = function (name, podId, userId, transaction, callback) { - if (!callback) { - callback = transaction - transaction = null - } - +findOrCreateAuthor = function ( + name: string, + podId: number, + userId: number, + transaction: Sequelize.Transaction, + callback: AuthorMethods.FindOrCreateAuthorCallback +) { const author = { name, podId, @@ -91,7 +92,7 @@ findOrCreateAuthor = function (name, podId, userId, transaction, callback) { defaults: author } - if (transaction) query.transaction = transaction + if (transaction !== null) query.transaction = transaction Author.findOrCreate(query).asCallback(function (err, result) { if (err) return callback(err) diff --git a/server/models/job-interface.ts b/server/models/job-interface.ts index ad4e2d2b0..ab6678257 100644 --- a/server/models/job-interface.ts +++ b/server/models/job-interface.ts @@ -1,7 +1,8 @@ import * as Sequelize from 'sequelize' 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 { diff --git a/server/models/job.ts b/server/models/job.ts index 982b51499..1afae8f08 100644 --- a/server/models/job.ts +++ b/server/models/job.ts @@ -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 = { order: [ [ 'id', 'ASC' ] diff --git a/server/models/oauth-client-interface.ts b/server/models/oauth-client-interface.ts index 4efd6212a..3b4325740 100644 --- a/server/models/oauth-client-interface.ts +++ b/server/models/oauth-client-interface.ts @@ -1,8 +1,12 @@ import * as Sequelize from 'sequelize' export namespace OAuthClientMethods { - export type CountTotal = (callback) => void - export type LoadFirstClient = (callback) => void + export type CountTotalCallback = (err: Error, total: number) => 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 } diff --git a/server/models/oauth-client.ts b/server/models/oauth-client.ts index 2cefb5cb9..22fae2842 100644 --- a/server/models/oauth-client.ts +++ b/server/models/oauth-client.ts @@ -67,15 +67,15 @@ function associate (models) { }) } -countTotal = function (callback) { +countTotal = function (callback: OAuthClientMethods.CountTotalCallback) { return OAuthClient.count().asCallback(callback) } -loadFirstClient = function (callback) { +loadFirstClient = function (callback: OAuthClientMethods.LoadFirstClientCallback) { return OAuthClient.findOne().asCallback(callback) } -getByIdAndSecret = function (clientId, clientSecret) { +getByIdAndSecret = function (clientId: string, clientSecret: string) { const query = { where: { clientId: clientId, diff --git a/server/models/oauth-token-interface.ts b/server/models/oauth-token-interface.ts index a0cd1ffe7..88526697e 100644 --- a/server/models/oauth-token-interface.ts +++ b/server/models/oauth-token-interface.ts @@ -1,11 +1,25 @@ import * as Sequelize from 'sequelize' +import * as Bluebird from 'bluebird' import { UserModel } from './user-interface' +export type OAuthTokenInfo = { + refreshToken: string + refreshTokenExpiresAt: Date, + client: { + id: number + }, + user: { + id: number + } +} + export namespace OAuthTokenMethods { - export type GetByRefreshTokenAndPopulateClient = (refreshToken) => void - export type GetByTokenAndPopulateUser = (bearerToken) => void - export type GetByRefreshTokenAndPopulateUser = (refreshToken) => any + export type GetByRefreshTokenAndPopulateClient = (refreshToken: string) => Bluebird + export type GetByTokenAndPopulateUser = (bearerToken: string) => Bluebird + export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Bluebird + + export type RemoveByUserIdCallback = (err: Error) => void export type RemoveByUserId = (userId, callback) => void } diff --git a/server/models/oauth-token.ts b/server/models/oauth-token.ts index 567df1c12..d70bd2bce 100644 --- a/server/models/oauth-token.ts +++ b/server/models/oauth-token.ts @@ -8,7 +8,8 @@ import { OAuthTokenInstance, OAuthTokenAttributes, - OAuthTokenMethods + OAuthTokenMethods, + OAuthTokenInfo } from './oauth-token-interface' let OAuthToken: Sequelize.Model @@ -90,7 +91,7 @@ function associate (models) { }) } -getByRefreshTokenAndPopulateClient = function (refreshToken) { +getByRefreshTokenAndPopulateClient = function (refreshToken: string) { const query = { where: { refreshToken: refreshToken @@ -99,9 +100,9 @@ getByRefreshTokenAndPopulateClient = function (refreshToken) { } return OAuthToken.findOne(query).then(function (token) { - if (!token) return token + if (!token) return null - const tokenInfos = { + const tokenInfos: OAuthTokenInfo = { refreshToken: token.refreshToken, refreshTokenExpiresAt: token.refreshTokenExpiresAt, client: { @@ -118,7 +119,7 @@ getByRefreshTokenAndPopulateClient = function (refreshToken) { }) } -getByTokenAndPopulateUser = function (bearerToken) { +getByTokenAndPopulateUser = function (bearerToken: string) { const query = { where: { accessToken: bearerToken @@ -133,7 +134,7 @@ getByTokenAndPopulateUser = function (bearerToken) { }) } -getByRefreshTokenAndPopulateUser = function (refreshToken) { +getByRefreshTokenAndPopulateUser = function (refreshToken: string) { const query = { where: { refreshToken: refreshToken diff --git a/server/models/pod-interface.ts b/server/models/pod-interface.ts index 14c88bec6..8f362bd5c 100644 --- a/server/models/pod-interface.ts +++ b/server/models/pod-interface.ts @@ -1,18 +1,39 @@ import * as Sequelize from 'sequelize' +// Don't use barrel, import just what we need +import { Pod as FormatedPod } from '../../shared/models/pod.model' + export namespace PodMethods { - export type ToFormatedJSON = () => void + export type ToFormatedJSON = () => FormatedPod + export type CountAllCallback = (err: Error, total: number) => void export type CountAll = (callback) => void - export type IncrementScores = (ids, value, callback) => void - export type List = (callback) => void - export type ListAllIds = (transaction, callback) => void - export type ListRandomPodIdsWithRequest = (limit, tableWithPods, tableWithPodsJoins, callback) => void - export type ListBadPods = (callback) => void - export type Load = (id, callback) => void - export type LoadByHost = (host, callback) => void - export type RemoveAll = (callback) => void - export type UpdatePodsScore = (goodPods, badPods) => void + + export type IncrementScoresCallback = (err: Error) => void + export type IncrementScores = (ids: number[], value: number, callback?: IncrementScoresCallback) => void + + export type ListCallback = (err: Error, podInstances?: PodInstance[]) => void + export type List = (callback: ListCallback) => void + + export type ListAllIdsCallback = (err: Error, ids?: number[]) => 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 { diff --git a/server/models/pod.ts b/server/models/pod.ts index 2df32e4a4..107744c43 100644 --- a/server/models/pod.ts +++ b/server/models/pod.ts @@ -118,11 +118,11 @@ function associate (models) { }) } -countAll = function (callback) { +countAll = function (callback: PodMethods.CountAllCallback) { return Pod.count().asCallback(callback) } -incrementScores = function (ids, value, callback) { +incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) { if (!callback) callback = function () { /* empty */ } const update = { @@ -142,35 +142,25 @@ incrementScores = function (ids, value, callback) { return Pod.update(update, options).asCallback(callback) } -list = function (callback) { +list = function (callback: PodMethods.ListCallback) { return Pod.findAll().asCallback(callback) } -listAllIds = function (transaction, callback) { - if (!callback) { - callback = transaction - transaction = null - } - +listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) { const query: any = { 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) return callback(null, map(pods, 'id')) }) } -listRandomPodIdsWithRequest = function (limit, tableWithPods, tableWithPodsJoins, callback) { - if (!callback) { - callback = tableWithPodsJoins - tableWithPodsJoins = '' - } - +listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) { Pod.count().asCallback(function (err, count) { if (err) return callback(err) @@ -204,7 +194,7 @@ listRandomPodIdsWithRequest = function (limit, tableWithPods, tableWithPodsJoins }) } -listBadPods = function (callback) { +listBadPods = function (callback: PodMethods.ListBadPodsCallback) { const query = { where: { score: { $lte: 0 } @@ -214,11 +204,11 @@ listBadPods = function (callback) { return Pod.findAll(query).asCallback(callback) } -load = function (id, callback) { +load = function (id: number, callback: PodMethods.LoadCallback) { return Pod.findById(id).asCallback(callback) } -loadByHost = function (host, callback) { +loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) { const query = { where: { host: host @@ -228,11 +218,11 @@ loadByHost = function (host, callback) { return Pod.findOne(query).asCallback(callback) } -removeAll = function (callback) { +removeAll = function (callback: PodMethods.RemoveAllCallback) { 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) if (goodPods.length !== 0) { diff --git a/server/models/request-interface.ts b/server/models/request-interface.ts index 2bba8ce7f..4bbd79966 100644 --- a/server/models/request-interface.ts +++ b/server/models/request-interface.ts @@ -1,12 +1,26 @@ 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 type CountTotalRequests = (callback) => void - export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void - export type RemoveWithEmptyTo = (callback) => void - export type RemoveAll = (callback) => void + export type CountTotalRequestsCallback = (err: Error, total: number) => void + export type CountTotalRequests = (callback: CountTotalRequestsCallback) => 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 { @@ -21,12 +35,13 @@ export interface RequestAttributes { endpoint: string } -export interface RequestInstance extends Sequelize.Instance { +export interface RequestInstance extends RequestClass, RequestAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date setPods: Sequelize.HasManySetAssociationsMixin + Pods: PodInstance[] } export interface RequestModel extends RequestClass, Sequelize.Model {} diff --git a/server/models/request-to-pod-interface.ts b/server/models/request-to-pod-interface.ts index 52116d6c4..6d75ca6e5 100644 --- a/server/models/request-to-pod-interface.ts +++ b/server/models/request-to-pod-interface.ts @@ -1,7 +1,8 @@ import * as Sequelize from 'sequelize' 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 { @@ -11,7 +12,7 @@ export interface RequestToPodClass { export interface RequestToPodAttributes { } -export interface RequestToPodInstance extends Sequelize.Instance { +export interface RequestToPodInstance extends RequestToPodClass, RequestToPodAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date diff --git a/server/models/request-to-pod.ts b/server/models/request-to-pod.ts index 681f808b7..3562069cc 100644 --- a/server/models/request-to-pod.ts +++ b/server/models/request-to-pod.ts @@ -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 */ } const query = { diff --git a/server/models/request-video-event-interface.ts b/server/models/request-video-event-interface.ts index a31c7108f..ad576a2b1 100644 --- a/server/models/request-video-event-interface.ts +++ b/server/models/request-video-event-interface.ts @@ -1,10 +1,30 @@ 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 type CountTotalRequests = (callback) => void - export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void - export type RemoveByRequestIdsAndPod = (ids, podId, callback) => void - export type RemoveAll = (callback) => void + export type CountTotalRequestsCallback = (err: Error, total: number) => void + export type CountTotalRequests = (callback: CountTotalRequestsCallback) => 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 { @@ -19,8 +39,10 @@ export interface RequestVideoEventAttributes { count: number } -export interface RequestVideoEventInstance extends Sequelize.Instance { +export interface RequestVideoEventInstance extends RequestVideoEventClass, RequestVideoEventAttributes, Sequelize.Instance { id: number + + Video: VideoInstance } export interface RequestVideoEventModel extends RequestVideoEventClass, Sequelize.Model {} diff --git a/server/models/request-video-event.ts b/server/models/request-video-event.ts index 234e2a8a9..e422649af 100644 --- a/server/models/request-video-event.ts +++ b/server/models/request-video-event.ts @@ -5,16 +5,17 @@ import { values } from 'lodash' import * as Sequelize from 'sequelize' +import { database as db } from '../initializers/database' import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' import { isVideoEventCountValid } from '../helpers' - import { addMethodsToModel } from './utils' import { RequestVideoEventClass, RequestVideoEventInstance, RequestVideoEventAttributes, - RequestVideoEventMethods + RequestVideoEventMethods, + RequestsVideoEventGrouped } from './request-video-event-interface' let RequestVideoEvent: Sequelize.Model @@ -76,13 +77,13 @@ function associate (models) { }) } -countTotalRequests = function (callback) { +countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) { const query = {} return RequestVideoEvent.count(query).asCallback(callback) } -listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { - const Pod = RequestVideoEvent['sequelize'].models.Pod +listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) { + const Pod = db.Pod // 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" ' + @@ -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 = { where: { id: { @@ -154,15 +155,15 @@ removeByRequestIdsAndPod = function (ids, podId, callback) { RequestVideoEvent.destroy(query).asCallback(callback) } -removeAll = function (callback) { +removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) { // Delete all requests RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) } // --------------------------------------------------------------------------- -function groupAndTruncateRequests (events, limitRequestsPerPod) { - const eventsGrouped = {} +function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) { + const eventsGrouped: RequestsVideoEventGrouped = {} events.forEach(function (event) { const pod = event.Video.Author.Pod diff --git a/server/models/request-video-qadu-interface.ts b/server/models/request-video-qadu-interface.ts index 6fe34ee91..04de7f159 100644 --- a/server/models/request-video-qadu-interface.ts +++ b/server/models/request-video-qadu-interface.ts @@ -1,10 +1,28 @@ 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 type CountTotalRequests = (callback) => void - export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback) => void - export type RemoveByRequestIdsAndPod = (ids, podId, callback) => void - export type RemoveAll = (callback) => void + export type CountTotalRequestsCallback = (err: Error, total: number) => void + export type CountTotalRequests = (callback: CountTotalRequestsCallback) => 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 { @@ -18,8 +36,11 @@ export interface RequestVideoQaduAttributes { type: string } -export interface RequestVideoQaduInstance extends Sequelize.Instance { +export interface RequestVideoQaduInstance extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance { id: number + + Pod: PodInstance + Video: VideoInstance } export interface RequestVideoQaduModel extends RequestVideoQaduClass, Sequelize.Model {} diff --git a/server/models/request-video-qadu.ts b/server/models/request-video-qadu.ts index e914e06cd..38627ad55 100644 --- a/server/models/request-video-qadu.ts +++ b/server/models/request-video-qadu.ts @@ -12,8 +12,8 @@ import { values } from 'lodash' import * as Sequelize from 'sequelize' +import { database as db } from '../initializers/database' import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' - import { addMethodsToModel } from './utils' import { RequestVideoQaduClass, @@ -83,15 +83,16 @@ function associate (models) { }) } -countTotalRequests = function (callback) { +countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) { const query = {} return RequestVideoQadu.count(query).asCallback(callback) } -listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { - const Pod = RequestVideoQadu['sequelize'].models.Pod +listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoQaduMethods.ListWithLimitAndRandomCallback) { + 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) // 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 = { where: { id: { @@ -135,14 +136,14 @@ removeByRequestIdsAndPod = function (ids, podId, callback) { RequestVideoQadu.destroy(query).asCallback(callback) } -removeAll = function (callback) { +removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) { // Delete all requests RequestVideoQadu.truncate({ cascade: true }).asCallback(callback) } // --------------------------------------------------------------------------- -function groupAndTruncateRequests (requests, limitRequestsPerPod) { +function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) { const requestsGrouped = {} requests.forEach(function (request) { diff --git a/server/models/request.ts b/server/models/request.ts index 18fa291fa..71f81ae66 100644 --- a/server/models/request.ts +++ b/server/models/request.ts @@ -1,15 +1,16 @@ import { values } from 'lodash' import * as Sequelize from 'sequelize' +import { database as db } from '../initializers/database' import { REQUEST_ENDPOINTS } from '../initializers' - import { addMethodsToModel } from './utils' import { RequestClass, RequestInstance, RequestAttributes, - RequestMethods + RequestMethods, + RequestsGrouped } from './request-interface' let Request: Sequelize.Model @@ -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 // So we could count requests that do not have existing pod anymore const query = { @@ -69,10 +70,11 @@ countTotalRequests = function (callback) { return Request.count(query).asCallback(callback) } -listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { - const Pod = Request['sequelize'].models.Pod +listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) { + 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) // 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 Request.truncate({ cascade: true }).asCallback(callback) } -removeWithEmptyTo = function (callback) { +removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) { if (!callback) callback = function () { /* empty */ } const query = { @@ -128,8 +130,8 @@ removeWithEmptyTo = function (callback) { // --------------------------------------------------------------------------- -function groupAndTruncateRequests (requests, limitRequestsPerPod) { - const requestsGrouped = {} +function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) { + const requestsGrouped: RequestsGrouped = {} requests.forEach(function (request) { request.Pods.forEach(function (pod) { diff --git a/server/models/tag-interface.ts b/server/models/tag-interface.ts index f96e1e9c5..e045e7ca5 100644 --- a/server/models/tag-interface.ts +++ b/server/models/tag-interface.ts @@ -1,7 +1,8 @@ import * as Sequelize from 'sequelize' 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 { diff --git a/server/models/tag.ts b/server/models/tag.ts index b2a9c9f81..c4402e83c 100644 --- a/server/models/tag.ts +++ b/server/models/tag.ts @@ -52,15 +52,10 @@ function associate (models) { }) } -findOrCreateTags = function (tags, transaction, callback) { - if (!callback) { - callback = transaction - transaction = null - } - +findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, callback: TagMethods.FindOrCreateTagsCallback) { const tagInstances = [] - each(tags, function (tag, callbackEach) { + each(tags, function (tag, callbackEach) { const query: any = { where: { name: tag diff --git a/server/models/user-interface.ts b/server/models/user-interface.ts index a504f42a1..98963b743 100644 --- a/server/models/user-interface.ts +++ b/server/models/user-interface.ts @@ -1,17 +1,35 @@ 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 type IsPasswordMatch = (password, callback) => void - export type ToFormatedJSON = () => void + export type IsPasswordMatchCallback = (err: Error, same: boolean) => void + export type IsPasswordMatch = (password: string, callback: IsPasswordMatchCallback) => void + + export type ToFormatedJSON = () => FormatedUser export type IsAdmin = () => boolean - export type CountTotal = (callback) => void - export type GetByUsername = (username) => any - export type List = (callback) => void - export type ListForApi = (start, count, sort, callback) => void - export type LoadById = (id, callback) => void - export type LoadByUsername = (username, callback) => void - export type LoadByUsernameOrEmail = (username, email, callback) => void + export type CountTotalCallback = (err: Error, total: number) => void + export type CountTotal = (callback: CountTotalCallback) => void + + export type GetByUsername = (username: string) => Bluebird + + export type ListCallback = (err: Error, userInstances: UserInstance[]) => 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 { diff --git a/server/models/user-video-rate-interface.ts b/server/models/user-video-rate-interface.ts index 57d2e2b91..e48869fd2 100644 --- a/server/models/user-video-rate-interface.ts +++ b/server/models/user-video-rate-interface.ts @@ -1,6 +1,7 @@ import * as Sequelize from 'sequelize' export namespace UserVideoRateMethods { + export type LoadCallback = (err: Error, userVideoRateInstance: UserVideoRateInstance) => void export type Load = (userId, videoId, transaction, callback) => void } @@ -12,7 +13,7 @@ export interface UserVideoRateAttributes { type: string } -export interface UserVideoRateInstance extends Sequelize.Instance { +export interface UserVideoRateInstance extends UserVideoRateClass, UserVideoRateAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date diff --git a/server/models/user-video-rate.ts b/server/models/user-video-rate.ts index 87886d8d0..6b71e8412 100644 --- a/server/models/user-video-rate.ts +++ b/server/models/user-video-rate.ts @@ -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 = { where: { userId, diff --git a/server/models/user.ts b/server/models/user.ts index 12ddaaeb7..0fbfdda50 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -117,7 +117,7 @@ export default function (sequelize, DataTypes) { return User } -function beforeCreateOrUpdate (user, options) { +function beforeCreateOrUpdate (user: UserInstance) { return new Promise(function (resolve, reject) { cryptPassword(user.password, function (err, hash) { if (err) return reject(err) @@ -131,7 +131,7 @@ function beforeCreateOrUpdate (user, options) { // ------------------------------ METHODS ------------------------------ -isPasswordMatch = function (password, callback) { +isPasswordMatch = function (password: string, callback: UserMethods.IsPasswordMatchCallback) { 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) } -getByUsername = function (username) { +getByUsername = function (username: string) { const query = { where: { username: username @@ -178,11 +178,11 @@ getByUsername = function (username) { return User.findOne(query) } -list = function (callback) { +list = function (callback: UserMethods.ListCallback) { return User.find().asCallback(callback) } -listForApi = function (start, count, sort, callback) { +listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) { const query = { offset: start, 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) } -loadByUsername = function (username, callback) { +loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) { const query = { where: { username: username @@ -210,7 +210,7 @@ loadByUsername = function (username, callback) { return User.findOne(query).asCallback(callback) } -loadByUsernameOrEmail = function (username, email, callback) { +loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) { const query = { where: { $or: [ { username }, { email } ] diff --git a/server/models/utils.ts b/server/models/utils.ts index fd84a9239..7ba96815e 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,7 +1,7 @@ // Translate for example "-name" to [ 'name', 'DESC' ] -function getSort (value) { - let field - let direction +function getSort (value: string) { + let field: string + let direction: 'ASC' | 'DESC' if (value.substring(0, 1) === '-') { direction = 'DESC' diff --git a/server/models/video-abuse-interface.ts b/server/models/video-abuse-interface.ts index 9b77fc6f5..d9cb93b42 100644 --- a/server/models/video-abuse-interface.ts +++ b/server/models/video-abuse-interface.ts @@ -1,9 +1,13 @@ import * as Sequelize from 'sequelize' +// Don't use barrel, import just what we need +import { VideoAbuse as FormatedVideoAbuse } from '../../shared/models/video-abuse.model' + export namespace VideoAbuseMethods { - export type toFormatedJSON = () => void + export type toFormatedJSON = () => FormatedVideoAbuse - export type ListForApi = (start, count, sort, callback) => void + 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 { @@ -15,7 +19,7 @@ export interface VideoAbuseAttributes { reason: string } -export interface VideoAbuseInstance extends Sequelize.Instance { +export interface VideoAbuseInstance extends VideoAbuseClass, VideoAbuseAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date diff --git a/server/models/video-blacklist-interface.ts b/server/models/video-blacklist-interface.ts index ae2cd6748..974718192 100644 --- a/server/models/video-blacklist-interface.ts +++ b/server/models/video-blacklist-interface.ts @@ -1,13 +1,25 @@ import * as Sequelize from 'sequelize' +// Don't use barrel, import just what we need +import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../shared/models/video-blacklist.model' + export namespace BlacklistedVideoMethods { - export type ToFormatedJSON = () => void + export type ToFormatedJSON = () => FormatedBlacklistedVideo + + export type CountTotalCallback = (err: Error, total: number) => 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 CountTotal = (callback) => void - export type List = (callback) => void - export type ListForApi = (start, count, sort, callback) => void - export type LoadById = (id, callback) => void - export type LoadByVideoId = (id, callback) => void + export type LoadByVideoIdCallback = (err: Error, blacklistedVideoInstance: BlacklistedVideoInstance) => void + export type LoadByVideoId = (id: string, callback: LoadByVideoIdCallback) => void } export interface BlacklistedVideoClass { diff --git a/server/models/video-blacklist.ts b/server/models/video-blacklist.ts index fe72d5d46..f36756085 100644 --- a/server/models/video-blacklist.ts +++ b/server/models/video-blacklist.ts @@ -66,15 +66,15 @@ function associate (models) { }) } -countTotal = function (callback) { +countTotal = function (callback: BlacklistedVideoMethods.CountTotalCallback) { return BlacklistedVideo.count().asCallback(callback) } -list = function (callback) { +list = function (callback: BlacklistedVideoMethods.ListCallback) { return BlacklistedVideo.findAll().asCallback(callback) } -listForApi = function (start, count, sort, callback) { +listForApi = function (start: number, count: number, sort: string, callback: BlacklistedVideoMethods.ListForApiCallback) { const query = { offset: start, 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) } -loadByVideoId = function (id, callback) { +loadByVideoId = function (id: string, callback: BlacklistedVideoMethods.LoadByIdCallback) { const query = { where: { videoId: id diff --git a/server/models/video-interface.ts b/server/models/video-interface.ts index b8dbeea35..7005f213c 100644 --- a/server/models/video-interface.ts +++ b/server/models/video-interface.ts @@ -1,28 +1,101 @@ import * as Sequelize from 'sequelize' +import { AuthorInstance } from './author-interface' +import { VideoTagInstance } from './video-tag-interface' + +// 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 = () => void - 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 + 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 List = (callback) => void - export type ListForApi = (start, count, sort, callback) => void - export type LoadByHostAndRemoteId = (fromHost, remoteId, callback) => void - export type ListOwnedAndPopulateAuthorAndTags = (callback) => void - export type ListOwnedByAuthor = (author, callback) => void - export type Load = (id, callback) => void - export type LoadAndPopulateAuthor = (id, callback) => void - export type LoadAndPopulateAuthorAndPodAndTags = (id, callback) => void - export type SearchAndPopulateAuthorAndPodAndTags = (value, field, start, count, sort, callback) => void + + export type ListCallback = () => void + export type List = (callback: ListCallback) => void + + export type ListForApiCallback = (err: Error, videoInstances?: VideoInstance[], total?: number) => void + export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void + + export type LoadByHostAndRemoteIdCallback = (err: Error, videoInstance: VideoInstance) => 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 { @@ -64,6 +137,9 @@ export interface VideoAttributes { views?: number likes?: number dislikes?: number + + Author?: AuthorInstance + Tags?: VideoTagInstance[] } export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.Instance { diff --git a/server/models/video-tag-interface.ts b/server/models/video-tag-interface.ts index 468827b8c..f928cecff 100644 --- a/server/models/video-tag-interface.ts +++ b/server/models/video-tag-interface.ts @@ -9,7 +9,7 @@ export interface VideoTagClass { export interface VideoTagAttributes { } -export interface VideoTagInstance extends Sequelize.Instance { +export interface VideoTagInstance extends VideoTagClass, VideoTagAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date diff --git a/server/models/video.ts b/server/models/video.ts index 5558a7c3b..3f808b811 100644 --- a/server/models/video.ts +++ b/server/models/video.ts @@ -11,6 +11,7 @@ import { join } from 'path' import * as Sequelize from 'sequelize' import { database as db } from '../initializers/database' +import { VideoTagInstance } from './video-tag-interface' import { logger, isVideoNameValid, @@ -266,7 +267,7 @@ export default function (sequelize, DataTypes) { return Video } -function beforeValidate (video, options) { +function beforeValidate (video: VideoInstance) { // Put a fake infoHash if it does not exists yet if (video.isOwned() && !video.infoHash) { // 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) { const tasks = [] @@ -318,7 +319,7 @@ function beforeCreate (video, options) { }) } -function afterDestroy (video, options) { +function afterDestroy (video: VideoInstance) { return new Promise(function (resolve, reject) { const tasks = [] @@ -401,7 +402,7 @@ generateMagnetUri = function () { } 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 magnetHash = { @@ -496,7 +497,7 @@ toFormatedJSON = function () { return json } -toAddRemoteJSON = function (callback) { +toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) { // Get thumbnail data to send to the other pod const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) fs.readFile(thumbnailPath, (err, thumbnailData) => { @@ -517,7 +518,7 @@ toAddRemoteJSON = function (callback) { author: this.Author.name, duration: this.duration, thumbnailData: thumbnailData.toString('binary'), - tags: map(this.Tags, 'name'), + tags: map(this.Tags, 'name'), createdAt: this.createdAt, updatedAt: this.updatedAt, extname: this.extname, @@ -530,7 +531,7 @@ toAddRemoteJSON = function (callback) { }) } -toUpdateRemoteJSON = function (callback) { +toUpdateRemoteJSON = function () { const json = { name: this.name, category: this.category, @@ -542,7 +543,7 @@ toUpdateRemoteJSON = function (callback) { remoteId: this.id, author: this.Author.name, duration: this.duration, - tags: map(this.Tags, 'name'), + tags: map(this.Tags, 'name'), createdAt: this.createdAt, updatedAt: this.updatedAt, extname: this.extname, @@ -554,7 +555,7 @@ toUpdateRemoteJSON = function (callback) { return json } -transcodeVideofile = function (finalCallback) { +transcodeVideofile = function (finalCallback: VideoMethods.TranscodeVideofileCallback) { const video = this const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR @@ -591,9 +592,9 @@ transcodeVideofile = function (finalCallback) { video.save().asCallback(callback) } - ], function (err) { + ], function (err: Error) { if (err) { - // Autodescruction... + // Autodesctruction... video.destroy().asCallback(function (err) { if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err }) }) @@ -609,7 +610,7 @@ transcodeVideofile = function (finalCallback) { // ------------------------------ STATICS ------------------------------ -generateThumbnailFromData = function (video, thumbnailData, callback) { +generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string, callback: VideoMethods.GenerateThumbnailFromDataCallback) { // Creating the thumbnail for a remote video 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) { 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) } -listForApi = function (start, count, sort, callback) { +listForApi = function (start: number, count: number, sort: string, callback: VideoMethods.ListForApiCallback) { // Exclude Blakclisted videos from the list const query = { 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 = { where: { remoteId: remoteId @@ -682,7 +683,7 @@ loadByHostAndRemoteId = function (fromHost, remoteId, callback) { return Video.findOne(query).asCallback(callback) } -listOwnedAndPopulateAuthorAndTags = function (callback) { +listOwnedAndPopulateAuthorAndTags = function (callback: VideoMethods.ListOwnedAndPopulateAuthorAndTagsCallback) { // If remoteId is null this is *our* video const query = { where: { @@ -694,7 +695,7 @@ listOwnedAndPopulateAuthorAndTags = function (callback) { return Video.findAll(query).asCallback(callback) } -listOwnedByAuthor = function (author, callback) { +listOwnedByAuthor = function (author: string, callback: VideoMethods.ListOwnedByAuthorCallback) { const query = { where: { remoteId: null @@ -712,11 +713,11 @@ listOwnedByAuthor = function (author, callback) { return Video.findAll(query).asCallback(callback) } -load = function (id, callback) { +load = function (id: string, callback: VideoMethods.LoadCallback) { return Video.findById(id).asCallback(callback) } -loadAndPopulateAuthor = function (id, callback) { +loadAndPopulateAuthor = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorCallback) { const options = { include: [ Video['sequelize'].models.Author ] } @@ -724,7 +725,7 @@ loadAndPopulateAuthor = function (id, callback) { return Video.findById(id, options).asCallback(callback) } -loadAndPopulateAuthorAndPodAndTags = function (id, callback) { +loadAndPopulateAuthorAndPodAndTags = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorAndPodAndTagsCallback) { const options = { include: [ { @@ -738,7 +739,14 @@ loadAndPopulateAuthorAndPodAndTags = function (id, 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 = { model: Video['sequelize'].models.Pod, 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()) 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()) 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()) fs.unlink(torrenPath, callback) } -function removePreview (video, callback) { +function removePreview (video: VideoInstance, callback: (err: Error) => void) { // Same name than video thumnail 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 = { announceList: [ [ 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) { - generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) +function createPreview (video: VideoInstance, videoPath: string, callback: (err: Error) => void) { + 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) } -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 = { filename: imageName, count: 1, folder } - if (!callback) { - callback = size - } else { + if (size) { options.size = size } @@ -894,7 +901,7 @@ function generateImage (video, videoPath, folder, imageName, size, callback?) { .thumbnail(options) } -function removeFromBlacklist (video, callback) { +function removeFromBlacklist (video: VideoInstance, callback: (err: Error) => void) { // Find the blacklisted video db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) { // If an error occured, stop here @@ -908,7 +915,7 @@ function removeFromBlacklist (video, callback) { video.destroy().asCallback(callback) } else { // If haven't found it, simply ignore it and do nothing - return callback() + return callback(null) } }) } -- cgit v1.2.3