From 453e83ea5d81d203ba34bc43cd5c2c750ba40568 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Aug 2019 11:53:26 +0200 Subject: Stronger model typings --- server/models/account/account-blocklist.ts | 6 +- server/models/account/account-video-rate.ts | 21 +++-- server/models/account/account.ts | 17 ++-- server/models/account/user-notification.ts | 10 ++- server/models/account/user-video-history.ts | 7 +- server/models/account/user.ts | 30 ++++--- server/models/activitypub/actor-follow.ts | 25 ++++-- server/models/activitypub/actor.ts | 20 +++-- server/models/oauth/oauth-token.ts | 19 ++-- server/models/redundancy/video-redundancy.ts | 9 +- server/models/server/plugin.ts | 10 ++- server/models/server/server-blocklist.ts | 6 +- server/models/server/server.ts | 5 +- server/models/video/video-abuse.ts | 8 +- server/models/video/video-blacklist.ts | 6 +- server/models/video/video-caption.ts | 8 +- server/models/video/video-change-ownership.ts | 6 +- server/models/video/video-channel.ts | 35 +++++--- server/models/video/video-comment.ts | 83 ++++++------------ server/models/video/video-file.ts | 3 +- server/models/video/video-format-utils.ts | 15 ++-- server/models/video/video-import.ts | 12 ++- server/models/video/video-playlist-element.ts | 13 +-- server/models/video/video-playlist.ts | 18 ++-- server/models/video/video-share.ts | 14 +-- server/models/video/video-streaming-playlist.ts | 12 +-- server/models/video/video.ts | 111 +++++++++++++----------- 27 files changed, 292 insertions(+), 237 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index d5746ad76..bb5371395 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -3,6 +3,8 @@ import { AccountModel } from './account' import { getSort } from '../utils' import { AccountBlock } from '../../../shared/models/blocklist' import { Op } from 'sequelize' +import * as Bluebird from 'bluebird' +import { MAccountBlocklist, MAccountBlocklistAccounts } from '@server/typings/models' enum ScopeNames { WITH_ACCOUNTS = 'WITH_ACCOUNTS' @@ -103,7 +105,7 @@ export class AccountBlocklistModel extends Model { }) } - static loadByAccountAndTarget (accountId: number, targetAccountId: number) { + static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird { const query = { where: { accountId, @@ -126,7 +128,7 @@ export class AccountBlocklistModel extends Model { return AccountBlocklistModel .scope([ ScopeNames.WITH_ACCOUNTS ]) - .findAndCountAll(query) + .findAndCountAll(query) .then(({ rows, count }) => { return { total: count, data: rows } }) diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 4bd8114cf..8b62dd05f 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -10,6 +10,8 @@ import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { AccountVideoRate } from '../../../shared' import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' +import * as Bluebird from 'bluebird' +import { MAccountVideoRate, MAccountVideoRateAccountUrl, MAccountVideoRateAccountVideo } from '@server/typings/models/video/video-rate' /* Account rates per video. @@ -77,7 +79,7 @@ export class AccountVideoRateModel extends Model { }) Account: AccountModel - static load (accountId: number, videoId: number, transaction?: Transaction) { + static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird { const options: FindOptions = { where: { accountId, @@ -89,7 +91,7 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } - static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) { + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird { const options: FindOptions = { where: { [ Op.or]: [ @@ -103,7 +105,7 @@ export class AccountVideoRateModel extends Model { ] } } - if (transaction) options.transaction = transaction + if (t) options.transaction = t return AccountVideoRateModel.findOne(options) } @@ -140,7 +142,12 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findAndCountAll(query) } - static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) { + static loadLocalAndPopulateVideo ( + rateType: VideoRateType, + accountName: string, + videoId: number, + t?: Transaction + ): Bluebird { const options: FindOptions = { where: { videoId, @@ -152,7 +159,7 @@ export class AccountVideoRateModel extends Model { required: true, include: [ { - attributes: [ 'id', 'url', 'preferredUsername' ], + attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ], model: ActorModel.unscoped(), required: true, where: { @@ -167,7 +174,7 @@ export class AccountVideoRateModel extends Model { } ] } - if (transaction) options.transaction = transaction + if (t) options.transaction = t return AccountVideoRateModel.findOne(options) } @@ -208,7 +215,7 @@ export class AccountVideoRateModel extends Model { ] } - return AccountVideoRateModel.findAndCountAll(query) + return AccountVideoRateModel.findAndCountAll(query) } static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 4dc412301..4cc731075 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -3,7 +3,8 @@ import { BeforeDestroy, BelongsTo, Column, - CreatedAt, DataType, + CreatedAt, + DataType, Default, DefaultScope, ForeignKey, @@ -31,6 +32,8 @@ import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequ import { AccountBlocklistModel } from './account-blocklist' import { ServerBlocklistModel } from '../server/server-blocklist' import { ActorFollowModel } from '../activitypub/actor-follow' +import { MAccountActor, MAccountDefault } from '../../typings/models' +import * as Bluebird from 'bluebird' export enum ScopeNames { SUMMARY = 'SUMMARY' @@ -229,11 +232,11 @@ export class AccountModel extends Model { return undefined } - static load (id: number, transaction?: Transaction) { + static load (id: number, transaction?: Transaction): Bluebird { return AccountModel.findByPk(id, { transaction }) } - static loadByNameWithHost (nameWithHost: string) { + static loadByNameWithHost (nameWithHost: string): Bluebird { const [ accountName, host ] = nameWithHost.split('@') if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) @@ -241,7 +244,7 @@ export class AccountModel extends Model { return AccountModel.loadByNameAndHost(accountName, host) } - static loadLocalByName (name: string) { + static loadLocalByName (name: string): Bluebird { const query = { where: { [ Op.or ]: [ @@ -271,7 +274,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByNameAndHost (name: string, host: string) { + static loadByNameAndHost (name: string, host: string): Bluebird { const query = { include: [ { @@ -296,7 +299,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByUrl (url: string, transaction?: Transaction) { + static loadByUrl (url: string, transaction?: Transaction): Bluebird { const query = { include: [ { @@ -329,7 +332,7 @@ export class AccountModel extends Model { }) } - static listLocalsForSitemap (sort: string) { + static listLocalsForSitemap (sort: string): Bluebird { const query = { attributes: [ ], offset: 0, diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index f38cd7e78..9b13a8376 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -16,6 +16,7 @@ import { ActorModel } from '../activitypub/actor' import { ActorFollowModel } from '../activitypub/actor-follow' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' +import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/typings/models/user' enum ScopeNames { WITH_ALL = 'WITH_ALL' @@ -371,7 +372,7 @@ export class UserNotificationModel extends Model { return UserNotificationModel.update({ read: true }, query) } - toFormattedJSON (): UserNotification { + toFormattedJSON (this: UserNotificationModelForApi): UserNotification { const video = this.Video ? Object.assign(this.formatVideo(this.Video),{ channel: this.formatActor(this.Video.VideoChannel) }) : undefined @@ -436,7 +437,7 @@ export class UserNotificationModel extends Model { } } - private formatVideo (video: VideoModel) { + formatVideo (this: UserNotificationModelForApi, video: UserNotificationIncludes.VideoInclude) { return { id: video.id, uuid: video.uuid, @@ -444,7 +445,10 @@ export class UserNotificationModel extends Model { } } - private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { + formatActor ( + this: UserNotificationModelForApi, + accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor + ) { const avatar = accountOrChannel.Actor.Avatar ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } : undefined diff --git a/server/models/account/user-video-history.ts b/server/models/account/user-video-history.ts index a862fc45f..3fe4c8db1 100644 --- a/server/models/account/user-video-history.ts +++ b/server/models/account/user-video-history.ts @@ -1,7 +1,8 @@ import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript' import { VideoModel } from '../video/video' import { UserModel } from './user' -import { Transaction, Op, DestroyOptions } from 'sequelize' +import { DestroyOptions, Op, Transaction } from 'sequelize' +import { MUserAccountId, MUserId } from '@server/typings/models' @Table({ tableName: 'userVideoHistory', @@ -54,7 +55,7 @@ export class UserVideoHistoryModel extends Model { }) User: UserModel - static listForApi (user: UserModel, start: number, count: number) { + static listForApi (user: MUserAccountId, start: number, count: number) { return VideoModel.listForApi({ start, count, @@ -67,7 +68,7 @@ export class UserVideoHistoryModel extends Model { }) } - static removeUserHistoryBefore (user: UserModel, beforeDate: string, t: Transaction) { + static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) { const query: DestroyOptions = { where: { userId: user.id diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 0041bf577..24b1626e7 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -54,6 +54,8 @@ import { VideoImportModel } from '../video/video-import' import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { getThemeOrDefault } from '../../lib/plugins/theme-utils' +import * as Bluebird from 'bluebird' +import { MUserChannel, MUserDefault, MUserId, MUserWithNotificationSetting } from '@server/typings/models' enum ScopeNames { WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' @@ -303,7 +305,7 @@ export class UserModel extends Model { }) } - static listWithRight (right: UserRight) { + static listWithRight (right: UserRight): Bluebird { const roles = Object.keys(USER_ROLE_LABELS) .map(k => parseInt(k, 10) as UserRole) .filter(role => hasUserRight(role, right)) @@ -319,7 +321,7 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static listUserSubscribersOf (actorId: number) { + static listUserSubscribersOf (actorId: number): Bluebird { const query = { include: [ { @@ -358,7 +360,7 @@ export class UserModel extends Model { return UserModel.unscoped().findAll(query) } - static listByUsernames (usernames: string[]) { + static listByUsernames (usernames: string[]): Bluebird { const query = { where: { username: usernames @@ -368,11 +370,11 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static loadById (id: number) { + static loadById (id: number): Bluebird { return UserModel.findByPk(id) } - static loadByUsername (username: string) { + static loadByUsername (username: string): Bluebird { const query = { where: { username: { [ Op.iLike ]: username } @@ -382,7 +384,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByUsernameAndPopulateChannels (username: string) { + static loadByUsernameAndPopulateChannels (username: string): Bluebird { const query = { where: { username: { [ Op.iLike ]: username } @@ -392,7 +394,7 @@ export class UserModel extends Model { return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) } - static loadByEmail (email: string) { + static loadByEmail (email: string): Bluebird { const query = { where: { email @@ -402,7 +404,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByUsernameOrEmail (username: string, email?: string) { + static loadByUsernameOrEmail (username: string, email?: string): Bluebird { if (!email) email = username const query = { @@ -414,7 +416,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoId (videoId: number) { + static loadByVideoId (videoId: number): Bluebird { const query = { include: [ { @@ -445,7 +447,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoImportId (videoImportId: number) { + static loadByVideoImportId (videoImportId: number): Bluebird { const query = { include: [ { @@ -462,7 +464,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByChannelActorId (videoChannelActorId: number) { + static loadByChannelActorId (videoChannelActorId: number): Bluebird { const query = { include: [ { @@ -486,7 +488,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByAccountActorId (accountActorId: number) { + static loadByAccountActorId (accountActorId: number): Bluebird { const query = { include: [ { @@ -503,7 +505,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static getOriginalVideoFileTotalFromUser (user: UserModel) { + static getOriginalVideoFileTotalFromUser (user: MUserId) { // Don't use sequelize because we need to use a sub query const query = UserModel.generateUserQuotaBaseSQL() @@ -511,7 +513,7 @@ export class UserModel extends Model { } // Returns cumulative size of all video files uploaded in the last 24 hours. - static getOriginalVideoFileTotalDailyFromUser (user: UserModel) { + static getOriginalVideoFileTotalDailyFromUser (user: MUserId) { // Don't use sequelize because we need to use a sub query const query = UserModel.generateUserQuotaBaseSQL('"video"."createdAt" > now() - interval \'24 hours\'') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 51b09e09b..8ef770cd4 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -27,7 +27,13 @@ import { createSafeIn, getSort } from '../utils' import { ActorModel, unusedActorAttributesForAPI } from './actor' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from '../account/account' -import { IncludeOptions, Op, Transaction, QueryTypes } from 'sequelize' +import { IncludeOptions, Op, QueryTypes, Transaction } from 'sequelize' +import { + MActorFollowActorsDefault, + MActorFollowActorsDefaultSubscription, + MActorFollowFollowingHost, + MActorFollowSubscriptions +} from '@server/typings/models' @Table({ tableName: 'actorFollow', @@ -143,7 +149,7 @@ export class ActorFollowModel extends Model { if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved) } - static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction) { + static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird { const query = { where: { actorId, @@ -167,7 +173,12 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findOne(query) } - static loadByActorAndTargetNameAndHostForAPI (actorId: number, targetName: string, targetHost: string, t?: Transaction) { + static loadByActorAndTargetNameAndHostForAPI ( + actorId: number, + targetName: string, + targetHost: string, + t?: Transaction + ): Bluebird { const actorFollowingPartInclude: IncludeOptions = { model: ActorModel, required: true, @@ -220,7 +231,7 @@ export class ActorFollowModel extends Model { }) } - static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]) { + static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird { const whereTab = targets .map(t => { if (t.host) { @@ -314,7 +325,7 @@ export class ActorFollowModel extends Model { ] } - return ActorFollowModel.findAndCountAll(query) + return ActorFollowModel.findAndCountAll(query) .then(({ rows, count }) => { return { data: rows, @@ -357,7 +368,7 @@ export class ActorFollowModel extends Model { ] } - return ActorFollowModel.findAndCountAll(query) + return ActorFollowModel.findAndCountAll(query) .then(({ rows, count }) => { return { data: rows, @@ -414,7 +425,7 @@ export class ActorFollowModel extends Model { ] } - return ActorFollowModel.findAndCountAll(query) + return ActorFollowModel.findAndCountAll(query) .then(({ rows, count }) => { return { data: rows.map(r => r.ActorFollowing.VideoChannel), diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 9cc53f78a..2312127b4 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -36,6 +36,8 @@ import { isOutdated, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { ActorFollowModel } from './actor-follow' import { VideoModel } from '../video/video' +import { MActor, MActorAccountChannelId, MActorFull } from '../../typings/models' +import * as Bluebird from 'bluebird' enum ScopeNames { FULL = 'FULL' @@ -252,11 +254,15 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel - static load (id: number) { + static load (id: number): Bluebird { return ActorModel.unscoped().findByPk(id) } - static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) { + static loadFull (id: number): Bluebird { + return ActorModel.scope(ScopeNames.FULL).findByPk(id) + } + + static loadFromAccountByVideoId (videoId: number, transaction: Sequelize.Transaction): Bluebird { const query = { include: [ { @@ -300,7 +306,7 @@ export class ActorModel extends Model { .then(a => !!a) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { + static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird { const query = { where: { followersUrl: { @@ -313,7 +319,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findAll(query) } - static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) { + static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird { const query = { where: { preferredUsername, @@ -325,7 +331,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByNameAndHost (preferredUsername: string, host: string) { + static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { const query = { where: { preferredUsername @@ -344,7 +350,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByUrl (url: string, transaction?: Sequelize.Transaction) { + static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird { const query = { where: { url @@ -367,7 +373,7 @@ export class ActorModel extends Model { return ActorModel.unscoped().findOne(query) } - static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) { + static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird { const query = { where: { url diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index 903d551df..b680be237 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts @@ -18,6 +18,8 @@ import { Transaction } from 'sequelize' import { AccountModel } from '../account/account' import { ActorModel } from '../activitypub/actor' import { clearCacheByToken } from '../../lib/oauth-model' +import * as Bluebird from 'bluebird' +import { MOAuthTokenUser } from '@server/typings/models/oauth/oauth-token' export type OAuthTokenInfo = { refreshToken: string @@ -160,7 +162,7 @@ export class OAuthTokenModel extends Model { }) } - static getByTokenAndPopulateUser (bearerToken: string) { + static getByTokenAndPopulateUser (bearerToken: string): Bluebird { const query = { where: { accessToken: bearerToken @@ -170,13 +172,13 @@ export class OAuthTokenModel extends Model { return OAuthTokenModel.scope(ScopeNames.WITH_USER) .findOne(query) .then(token => { - if (token) token[ 'user' ] = token.User + if (!token) return null - return token + return Object.assign(token, { user: token.User }) }) } - static getByRefreshTokenAndPopulateUser (refreshToken: string) { + static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird { const query = { where: { refreshToken: refreshToken @@ -186,12 +188,9 @@ export class OAuthTokenModel extends Model { return OAuthTokenModel.scope(ScopeNames.WITH_USER) .findOne(query) .then(token => { - if (token) { - token['user'] = token.User - return token - } else { - return new OAuthTokenModel() - } + if (!token) return new OAuthTokenModel() + + return Object.assign(token, { user: token.User }) }) } diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index 3df1c4f9c..1c216b300 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -30,6 +30,7 @@ import * as Bluebird from 'bluebird' import { col, FindOptions, fn, literal, Op, Transaction } from 'sequelize' import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' import { CONFIG } from '../../initializers/config' +import { MVideoRedundancy, MVideoRedundancyVideo } from '@server/typings/models' export enum ScopeNames { WITH_VIDEO = 'WITH_VIDEO' @@ -166,7 +167,7 @@ export class VideoRedundancyModel extends Model { return undefined } - static async loadLocalByFileId (videoFileId: number) { + static async loadLocalByFileId (videoFileId: number): Promise { const actor = await getServerActor() const query = { @@ -179,7 +180,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) } - static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number) { + static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise { const actor = await getServerActor() const query = { @@ -192,7 +193,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) } - static loadByUrl (url: string, transaction?: Transaction) { + static loadByUrl (url: string, transaction?: Transaction): Bluebird { const query = { where: { url @@ -306,7 +307,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) } - static async loadOldestLocalThatAlreadyExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number) { + static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise { const expiredDate = new Date() expiredDate.setMilliseconds(expiredDate.getMilliseconds() - expiresAfterMs) diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index a15f9a7e2..debd25ea1 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts @@ -11,6 +11,8 @@ import { PluginType } from '../../../shared/models/plugins/plugin.type' import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' import { FindAndCountOptions, json } from 'sequelize' import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' +import * as Bluebird from 'bluebird' +import { MPlugin } from '@server/typings/models' @DefaultScope(() => ({ attributes: { @@ -85,7 +87,7 @@ export class PluginModel extends Model { @UpdatedAt updatedAt: Date - static listEnabledPluginsAndThemes () { + static listEnabledPluginsAndThemes (): Bluebird { const query = { where: { enabled: true, @@ -96,7 +98,7 @@ export class PluginModel extends Model { return PluginModel.findAll(query) } - static loadByNpmName (npmName: string) { + static loadByNpmName (npmName: string): Bluebird { const name = this.normalizePluginName(npmName) const type = this.getTypeFromNpmName(npmName) @@ -206,13 +208,13 @@ export class PluginModel extends Model { if (options.pluginType) query.where['type'] = options.pluginType return PluginModel - .findAndCountAll(query) + .findAndCountAll(query) .then(({ rows, count }) => { return { total: count, data: rows } }) } - static listInstalled () { + static listInstalled (): Bluebird { const query = { where: { uninstalled: false diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index 5138b0f76..e4db93dfc 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -3,6 +3,8 @@ import { AccountModel } from '../account/account' import { ServerModel } from './server' import { ServerBlock } from '../../../shared/models/blocklist' import { getSort } from '../utils' +import * as Bluebird from 'bluebird' +import { MServerBlocklist, MServerBlocklistAccountServer } from '@server/typings/models' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -73,7 +75,7 @@ export class ServerBlocklistModel extends Model { }) BlockedServer: ServerModel - static loadByAccountAndHost (accountId: number, host: string) { + static loadByAccountAndHost (accountId: number, host: string): Bluebird { const query = { where: { accountId @@ -104,7 +106,7 @@ export class ServerBlocklistModel extends Model { return ServerBlocklistModel .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) - .findAndCountAll(query) + .findAndCountAll(query) .then(({ rows, count }) => { return { total: count, data: rows } }) diff --git a/server/models/server/server.ts b/server/models/server/server.ts index 1d211f1e0..b0bdd2b0b 100644 --- a/server/models/server/server.ts +++ b/server/models/server/server.ts @@ -2,8 +2,9 @@ import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, Updat import { isHostValid } from '../../helpers/custom-validators/servers' import { ActorModel } from '../activitypub/actor' import { throwIfNotValid } from '../utils' -import { AccountBlocklistModel } from '../account/account-blocklist' import { ServerBlocklistModel } from './server-blocklist' +import * as Bluebird from 'bluebird' +import { MServer } from '@server/typings/models/server' @Table({ tableName: 'server', @@ -50,7 +51,7 @@ export class ServerModel extends Model { }) BlockedByAccounts: ServerBlocklistModel[] - static loadByHost (host: string) { + static loadByHost (host: string): Bluebird { const query = { where: { host diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index 1ac7919b3..af7b40d11 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -11,6 +11,8 @@ import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoAbuseState } from '../../../shared' import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' +import { MVideoAbuse, MVideoAbuseAccountVideo, MVideoAbuseVideo } from '../../typings/models' +import * as Bluebird from 'bluebird' @Table({ tableName: 'videoAbuse', @@ -73,7 +75,7 @@ export class VideoAbuseModel extends Model { }) Video: VideoModel - static loadByIdAndVideoId (id: number, videoId: number) { + static loadByIdAndVideoId (id: number, videoId: number): Bluebird { const query = { where: { id, @@ -106,7 +108,7 @@ export class VideoAbuseModel extends Model { }) } - toFormattedJSON (): VideoAbuse { + toFormattedJSON (this: MVideoAbuseAccountVideo): VideoAbuse { return { id: this.id, reason: this.reason, @@ -125,7 +127,7 @@ export class VideoAbuseModel extends Model { } } - toActivityPubObject (): VideoAbuseObject { + toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject { return { type: 'Flag' as 'Flag', content: this.reason, diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 22d949da0..5a0cac94a 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,12 +1,14 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { getSortOnModel, SortType, throwIfNotValid } from '../utils' -import { ScopeNames as VideoModelScopeNames, VideoModel } from './video' +import { VideoModel } from './video' import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { FindOptions } from 'sequelize' import { ThumbnailModel } from './thumbnail' +import * as Bluebird from 'bluebird' +import { MVideoBlacklist } from '@server/typings/models' @Table({ tableName: 'videoBlacklist', @@ -99,7 +101,7 @@ export class VideoBlacklistModel extends Model { }) } - static loadByVideoId (id: number) { + static loadByVideoId (id: number): Bluebird { const query = { where: { videoId: id diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index a01565851..9ce350d12 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -21,6 +21,8 @@ import { join } from 'path' import { logger } from '../../helpers/logger' import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' +import * as Bluebird from 'bluebird' +import { MVideoCaptionVideo } from '@server/typings/models' export enum ScopeNames { WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' @@ -30,7 +32,7 @@ export enum ScopeNames { [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: { include: [ { - attributes: [ 'uuid', 'remote' ], + attributes: [ 'id', 'uuid', 'remote' ], model: VideoModel.unscoped(), required: true } @@ -93,7 +95,7 @@ export class VideoCaptionModel extends Model { return undefined } - static loadByVideoIdAndLanguage (videoId: string | number, language: string) { + static loadByVideoIdAndLanguage (videoId: string | number, language: string): Bluebird { const videoInclude = { model: VideoModel.unscoped(), attributes: [ 'id', 'remote', 'uuid' ], @@ -122,7 +124,7 @@ export class VideoCaptionModel extends Model { .then(([ caption ]) => caption) } - static listVideoCaptions (videoId: number) { + static listVideoCaptions (videoId: number): Bluebird { const query = { order: [ [ 'language', 'ASC' ] ] as OrderItem[], where: { diff --git a/server/models/video/video-change-ownership.ts b/server/models/video/video-change-ownership.ts index b545a2f8c..2d0ff48fb 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -3,6 +3,8 @@ import { AccountModel } from '../account/account' import { ScopeNames as VideoScopeNames, VideoModel } from './video' import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' import { getSort } from '../utils' +import { MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership' +import * as Bluebird from 'bluebird' enum ScopeNames { WITH_ACCOUNTS = 'WITH_ACCOUNTS', @@ -108,11 +110,11 @@ export class VideoChangeOwnershipModel extends Model return Promise.all([ VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query), - VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll(query) + VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll(query) ]).then(([ count, rows ]) => ({ total: count, data: rows })) } - static load (id: number) { + static load (id: number): Bluebird { return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]) .findByPk(id) } diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 6241a75a3..79b9e7d2b 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -33,6 +33,13 @@ import { ServerModel } from '../server/server' import { FindOptions, ModelIndexesOptions, Op } from 'sequelize' import { AvatarModel } from '../avatar/avatar' import { VideoPlaylistModel } from './video-playlist' +import * as Bluebird from 'bluebird' +import { + MChannelAccountDefault, + MChannelActor, + MChannelActorAccountDefault, + MChannelActorAccountDefaultVideos +} from '../../typings/models/video' // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation const indexes: ModelIndexesOptions[] = [ @@ -47,7 +54,7 @@ const indexes: ModelIndexesOptions[] = [ ] export enum ScopeNames { - AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', + FOR_API = 'FOR_API', WITH_ACCOUNT = 'WITH_ACCOUNT', WITH_ACTOR = 'WITH_ACTOR', WITH_VIDEOS = 'WITH_VIDEOS', @@ -74,10 +81,10 @@ export type SummaryOptions = { @Scopes(() => ({ [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { const base: FindOptions = { - attributes: [ 'name', 'description', 'id', 'actorId' ], + attributes: [ 'id', 'name', 'description', 'actorId' ], include: [ { - attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ], + attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], model: ActorModel.unscoped(), required: true, include: [ @@ -106,7 +113,7 @@ export type SummaryOptions = { return base }, - [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { + [ScopeNames.FOR_API]: (options: AvailableForListOptions) => { // Only list local channels OR channels that are on an instance followed by actorId const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId) @@ -268,7 +275,7 @@ export class VideoChannelModel extends Model { } const scopes = { - method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ] + method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] } return VideoChannelModel .scope(scopes) @@ -278,7 +285,7 @@ export class VideoChannelModel extends Model { }) } - static listLocalsForSitemap (sort: string) { + static listLocalsForSitemap (sort: string): Bluebird { const query = { attributes: [ ], offset: 0, @@ -331,7 +338,7 @@ export class VideoChannelModel extends Model { } const scopes = { - method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ] + method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ] } return VideoChannelModel .scope(scopes) @@ -369,13 +376,13 @@ export class VideoChannelModel extends Model { }) } - static loadByIdAndPopulateAccount (id: number) { + static loadByIdAndPopulateAccount (id: number): Bluebird { return VideoChannelModel.unscoped() .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) .findByPk(id) } - static loadByIdAndAccount (id: number, accountId: number) { + static loadByIdAndAccount (id: number, accountId: number): Bluebird { const query = { where: { id, @@ -388,13 +395,13 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadAndPopulateAccount (id: number) { + static loadAndPopulateAccount (id: number): Bluebird { return VideoChannelModel.unscoped() .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) .findByPk(id) } - static loadByUrlAndPopulateAccount (url: string) { + static loadByUrlAndPopulateAccount (url: string): Bluebird { const query = { include: [ { @@ -420,7 +427,7 @@ export class VideoChannelModel extends Model { return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) } - static loadLocalByNameAndPopulateAccount (name: string) { + static loadLocalByNameAndPopulateAccount (name: string): Bluebird { const query = { include: [ { @@ -439,7 +446,7 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadByNameAndHostAndPopulateAccount (name: string, host: string) { + static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird { const query = { include: [ { @@ -464,7 +471,7 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadAndPopulateAccountAndVideos (id: number) { + static loadAndPopulateAccountAndVideos (id: number): Bluebird { const options = { include: [ VideoModel diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 58b75510d..c88dac1c1 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,36 +1,30 @@ -import { - AllowNull, - BeforeDestroy, - BelongsTo, - Column, - CreatedAt, - DataType, - ForeignKey, - Is, - Model, - Scopes, - Table, - UpdatedAt -} from 'sequelize-typescript' +import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' import { VideoComment } from '../../../shared/models/videos/video-comment.model' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' -import { sendDeleteVideoComment } from '../../lib/activitypub/send' import { AccountModel } from '../account/account' import { ActorModel } from '../activitypub/actor' -import { AvatarModel } from '../avatar/avatar' -import { ServerModel } from '../server/server' import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' import { getServerActor } from '../../helpers/utils' -import { UserModel } from '../account/user' import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor' import { regexpCapture } from '../../helpers/regexp' import { uniq } from 'lodash' -import { FindOptions, literal, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' +import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' +import * as Bluebird from 'bluebird' +import { + MComment, + MCommentId, + MCommentOwner, + MCommentOwnerReplyVideoLight, + MCommentOwnerVideo, + MCommentOwnerVideoFeed, + MCommentOwnerVideoReply +} from '../../typings/models/video' +import { MUserAccountId } from '@server/typings/models' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -68,22 +62,7 @@ enum ScopeNames { [ScopeNames.WITH_ACCOUNT]: { include: [ { - model: AccountModel, - include: [ - { - model: ActorModel, - include: [ - { - model: ServerModel, - required: false - }, - { - model: AvatarModel, - required: false - } - ] - } - ] + model: AccountModel } ] }, @@ -102,22 +81,12 @@ enum ScopeNames { required: true, include: [ { - model: VideoChannelModel.unscoped(), + model: VideoChannelModel, required: true, include: [ - { - model: ActorModel, - required: true - }, { model: AccountModel, - required: true, - include: [ - { - model: ActorModel, - required: true - } - ] + required: true } ] } @@ -212,7 +181,7 @@ export class VideoCommentModel extends Model { }) Account: AccountModel - static loadById (id: number, t?: Transaction) { + static loadById (id: number, t?: Transaction): Bluebird { const query: FindOptions = { where: { id @@ -224,7 +193,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findOne(query) } - static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction) { + static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird { const query: FindOptions = { where: { id @@ -238,7 +207,7 @@ export class VideoCommentModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction) { + static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -250,7 +219,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) } - static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction) { + static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -273,7 +242,7 @@ export class VideoCommentModel extends Model { start: number, count: number, sort: string, - user?: UserModel + user?: MUserAccountId }) { const { videoId, start, count, sort, user } = parameters @@ -314,7 +283,7 @@ export class VideoCommentModel extends Model { static async listThreadCommentsForApi (parameters: { videoId: number, threadId: number, - user?: UserModel + user?: MUserAccountId }) { const { videoId, threadId, user } = parameters @@ -353,7 +322,7 @@ export class VideoCommentModel extends Model { }) } - static listThreadParentComments (comment: VideoCommentModel, t: Transaction, order: 'ASC' | 'DESC' = 'ASC') { + static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird { const query = { order: [ [ 'createdAt', order ] ] as Order, where: { @@ -389,10 +358,10 @@ export class VideoCommentModel extends Model { transaction: t } - return VideoCommentModel.findAndCountAll(query) + return VideoCommentModel.findAndCountAll(query) } - static listForFeed (start: number, count: number, videoId?: number) { + static listForFeed (start: number, count: number, videoId?: number): Bluebird { const query = { order: [ [ 'createdAt', 'DESC' ] ] as Order, offset: start, @@ -521,7 +490,7 @@ export class VideoCommentModel extends Model { } as VideoComment } - toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject { + toActivityPubObject (threadParentComments: MCommentOwner[]): VideoCommentObject { let inReplyTo: string // New thread, so in AS we reply to the video if (this.inReplyToCommentId === null) { diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 05c490759..6304f741c 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -25,6 +25,7 @@ import { VideoRedundancyModel } from '../redundancy/video-redundancy' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' import { FindOptions, QueryTypes, Transaction } from 'sequelize' import { MIMETYPES } from '../../initializers/constants' +import { MVideoFile } from '@server/typings/models' @Table({ tableName: 'videoFile', @@ -166,7 +167,7 @@ export class VideoFileModel extends Model { return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname] } - hasSameUniqueKeysThan (other: VideoFileModel) { + hasSameUniqueKeysThan (other: MVideoFile) { return this.fps === other.fps && this.resolution === other.resolution && this.videoId === other.videoId diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts index 284539def..4e7eb5f0c 100644 --- a/server/models/video/video-format-utils.ts +++ b/server/models/video/video-format-utils.ts @@ -1,6 +1,5 @@ import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' import { VideoModel } from './video' -import { VideoFileModel } from './video-file' import { ActivityPlaylistInfohashesObject, ActivityPlaylistSegmentHashesObject, @@ -17,7 +16,9 @@ import { } from '../../lib/activitypub' import { isArray } from '../../helpers/custom-validators/misc' import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' -import { VideoStreamingPlaylistModel } from './video-streaming-playlist' +import { MVideo, MVideoAP, MVideoDetails } from '../../typings/models' +import { MStreamingPlaylistRedundancies } from '../../typings/models/video/video-streaming-playlist' +import { MVideoFileRedundanciesOpt } from '../../typings/models/video/video-file' export type VideoFormattingJSONOptions = { completeDescription?: boolean @@ -102,7 +103,7 @@ function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormatting return videoObject } -function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails { +function videoModelToFormattedDetailsJSON (video: MVideoDetails): VideoDetails { const formattedJson = video.toFormattedJSON({ additionalAttributes: { scheduledUpdate: true, @@ -114,7 +115,7 @@ function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails { const tags = video.Tags ? video.Tags.map(t => t.name) : [] - const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists) + const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video.VideoStreamingPlaylists) const detailsJson = { support: video.support, @@ -142,7 +143,7 @@ function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails { return Object.assign(formattedJson, detailsJson) } -function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] { +function streamingPlaylistsModelToFormattedJSON (playlists: MStreamingPlaylistRedundancies[]): VideoStreamingPlaylist[] { if (isArray(playlists) === false) return [] return playlists @@ -161,7 +162,7 @@ function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: V }) } -function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] { +function videoFilesModelToFormattedJSON (video: MVideo, videoFiles: MVideoFileRedundanciesOpt[]): VideoFile[] { const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() return videoFiles @@ -189,7 +190,7 @@ function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFil }) } -function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject { +function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject { const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() if (!video.Tags) video.Tags = [] diff --git a/server/models/video/video-import.ts b/server/models/video/video-import.ts index 480a671c8..f596eea9d 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts @@ -20,6 +20,8 @@ import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../help import { VideoImport, VideoImportState } from '../../../shared' import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos' import { UserModel } from '../account/user' +import * as Bluebird from 'bluebird' +import { MVideoImportDefault } from '@server/typings/models/video/video-import' @DefaultScope(() => ({ include: [ @@ -28,7 +30,11 @@ import { UserModel } from '../account/user' required: true }, { - model: VideoModel.scope([ VideoModelScopeNames.WITH_ACCOUNT_DETAILS, VideoModelScopeNames.WITH_TAGS]), + model: VideoModel.scope([ + VideoModelScopeNames.WITH_ACCOUNT_DETAILS, + VideoModelScopeNames.WITH_TAGS, + VideoModelScopeNames.WITH_THUMBNAILS + ]), required: false } ] @@ -114,7 +120,7 @@ export class VideoImportModel extends Model { return undefined } - static loadAndPopulateVideo (id: number) { + static loadAndPopulateVideo (id: number): Bluebird { return VideoImportModel.findByPk(id) } @@ -135,7 +141,7 @@ export class VideoImportModel extends Model { } } - return VideoImportModel.findAndCountAll(query) + return VideoImportModel.findAndCountAll(query) .then(({ rows, count }) => { return { data: rows, diff --git a/server/models/video/video-playlist-element.ts b/server/models/video/video-playlist-element.ts index dd7653533..901113161 100644 --- a/server/models/video/video-playlist-element.ts +++ b/server/models/video/video-playlist-element.ts @@ -25,6 +25,9 @@ import { UserModel } from '../account/user' import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model' import { AccountModel } from '../account/account' import { VideoPrivacy } from '../../../shared/models/videos' +import * as Bluebird from 'bluebird' +import { MVideoPlaylistAP, MVideoPlaylistElement, MVideoPlaylistVideoThumbnail } from '@server/typings/models/video/video-playlist-element' +import { MUserAccountId } from '@server/typings/models' @Table({ tableName: 'videoPlaylistElement', @@ -116,7 +119,7 @@ export class VideoPlaylistElementModel extends Model count: number, videoPlaylistId: number, serverAccount: AccountModel, - user?: UserModel + user?: MUserAccountId }) { const accountIds = [ options.serverAccount.id ] const videoScope: (ScopeOptions | string)[] = [ @@ -162,7 +165,7 @@ export class VideoPlaylistElementModel extends Model ]).then(([ total, data ]) => ({ total, data })) } - static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) { + static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird { const query = { where: { videoPlaylistId, @@ -173,11 +176,11 @@ export class VideoPlaylistElementModel extends Model return VideoPlaylistElementModel.findOne(query) } - static loadById (playlistElementId: number) { + static loadById (playlistElementId: number): Bluebird { return VideoPlaylistElementModel.findByPk(playlistElementId) } - static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) { + static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string): Bluebird { const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId } const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId } @@ -218,7 +221,7 @@ export class VideoPlaylistElementModel extends Model }) } - static loadFirstElementWithVideoThumbnail (videoPlaylistId: number) { + static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird { const query = { order: getSort('position'), where: { diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index c8e97c491..9f1d03ac5 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -43,6 +43,14 @@ import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video- import { ThumbnailModel } from './thumbnail' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' +import * as Bluebird from 'bluebird' +import { + MVideoPlaylistAccountThumbnail, + MVideoPlaylistFull, + MVideoPlaylistFullSummary, + MVideoPlaylistIdWithElements +} from '../../typings/models/video/video-playlist' +import { MThumbnail } from '../../typings/models/video/thumbnail' enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', @@ -332,7 +340,7 @@ export class VideoPlaylistModel extends Model { }) } - static listPlaylistIdsOf (accountId: number, videoIds: number[]) { + static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird { const query = { attributes: [ 'id' ], where: { @@ -368,7 +376,7 @@ export class VideoPlaylistModel extends Model { .then(e => !!e) } - static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction) { + static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -381,7 +389,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadWithAccountAndChannel (id: number | string, transaction: Transaction) { + static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -394,7 +402,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccount (url: string) { + static loadByUrlAndPopulateAccount (url: string): Bluebird { const query = { where: { url @@ -423,7 +431,7 @@ export class VideoPlaylistModel extends Model { return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) } - async setAndSaveThumbnail (thumbnail: ThumbnailModel, t: Transaction) { + async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) { thumbnail.videoPlaylistId = this.id this.Thumbnail = await thumbnail.save({ transaction: t }) diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index d8ed64557..9019b401a 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -8,6 +8,8 @@ import { buildLocalActorIdsIn, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' import { Op, Transaction } from 'sequelize' +import { MVideoShareActor, MVideoShareFull } from '../../typings/models/video' +import { MActorDefault } from '../../typings/models' enum ScopeNames { FULL = 'FULL', @@ -88,7 +90,7 @@ export class VideoShareModel extends Model { }) Video: VideoModel - static load (actorId: number, videoId: number, t?: Transaction) { + static load (actorId: number, videoId: number, t?: Transaction): Bluebird { return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({ where: { actorId, @@ -98,7 +100,7 @@ export class VideoShareModel extends Model { }) } - static loadByUrl (url: string, t: Transaction) { + static loadByUrl (url: string, t: Transaction): Bluebird { return VideoShareModel.scope(ScopeNames.FULL).findOne({ where: { url @@ -107,7 +109,7 @@ export class VideoShareModel extends Model { }) } - static loadActorsByShare (videoId: number, t: Transaction) { + static loadActorsByShare (videoId: number, t: Transaction): Bluebird { const query = { where: { videoId @@ -122,10 +124,10 @@ export class VideoShareModel extends Model { } return VideoShareModel.scope(ScopeNames.FULL).findAll(query) - .then(res => res.map(r => r.Actor)) + .then((res: MVideoShareFull[]) => res.map(r => r.Actor)) } - static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird { + static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird { const query = { attributes: [], include: [ @@ -163,7 +165,7 @@ export class VideoShareModel extends Model { .then(res => res.map(r => r.Actor)) } - static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird { + static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird { const query = { attributes: [], include: [ diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index 31dc82c54..0ea90d28c 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -1,16 +1,16 @@ -import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, HasMany, Is, Model, Table, UpdatedAt, DataType } from 'sequelize-typescript' +import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoRedundancyModel } from '../redundancy/video-redundancy' import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { CONSTRAINTS_FIELDS, STATIC_PATHS, P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants' -import { VideoFileModel } from './video-file' +import { CONSTRAINTS_FIELDS, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_PATHS } from '../../initializers/constants' import { join } from 'path' import { sha1 } from '../../helpers/core-utils' import { isArrayOf } from '../../helpers/custom-validators/misc' -import { QueryTypes, Op } from 'sequelize' +import { Op, QueryTypes } from 'sequelize' +import { MStreamingPlaylist, MVideoFile } from '@server/typings/models' @Table({ tableName: 'videoStreamingPlaylist', @@ -91,7 +91,7 @@ export class VideoStreamingPlaylistModel extends Model results.length === 1) } - static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: VideoFileModel[]) { + static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: MVideoFile[]) { const hashes: string[] = [] // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115 @@ -165,7 +165,7 @@ export class VideoStreamingPlaylistModel extends Model { VideoCaptions: VideoCaptionModel[] @BeforeDestroy - static async sendDelete (instance: VideoModel, options) { + static async sendDelete (instance: MVideoAccountLight, options) { if (instance.isOwned()) { if (!instance.VideoChannel) { instance.VideoChannel = await instance.$get('VideoChannel', { include: [ - { - model: AccountModel, - include: [ ActorModel ] - } + ActorModel, + AccountModel ], transaction: options.transaction - }) as VideoChannelModel + }) as MChannelActorAccountDefault } return sendDeleteVideo(instance, options.transaction) @@ -1039,7 +1054,7 @@ export class VideoModel extends Model { return undefined } - static listLocal () { + static listLocal (): Bluebird { const query = { where: { remote: false @@ -1159,7 +1174,7 @@ export class VideoModel extends Model { }) } - static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { + static listUserVideosForApi (accountId: number, start: number, count: number, sort: string) { function buildBaseQuery (): FindOptions { return { offset: start, @@ -1192,19 +1207,12 @@ export class VideoModel extends Model { ScopeNames.WITH_THUMBNAILS ] - if (withFiles === true) { - findQuery.include.push({ - model: VideoFileModel.unscoped(), - required: true - }) - } - return Promise.all([ VideoModel.count(countQuery), VideoModel.scope(findScopes).findAll(findQuery) ]).then(([ count, rows ]) => { return { - data: rows, + data: rows as MVideoWithBlacklistThumbnailScheduled[], total: count } }) @@ -1228,8 +1236,8 @@ export class VideoModel extends Model { followerActorId?: number videoPlaylistId?: number, trendingDays?: number, - user?: UserModel, - historyOfUser?: UserModel + user?: MUserAccountId, + historyOfUser?: MUserId }, countVideos = true) { if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { throw new Error('Try to filter all-local but no user has not the see all videos right') @@ -1294,7 +1302,7 @@ export class VideoModel extends Model { tagsAllOf?: string[] durationMin?: number // seconds durationMax?: number // seconds - user?: UserModel, + user?: MUserAccountId, filter?: VideoFilter }) { const whereAnd = [] @@ -1387,7 +1395,7 @@ export class VideoModel extends Model { return VideoModel.getAvailableForApi(query, queryOptions) } - static load (id: number | string, t?: Transaction) { + static load (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1397,7 +1405,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithRights (id: number | string, t?: Transaction) { + static loadWithRights (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1411,7 +1419,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadOnlyId (id: number | string, t?: Transaction) { + static loadOnlyId (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { @@ -1423,7 +1431,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean) { + static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -1439,7 +1447,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadByUUID (uuid: string) { + static loadByUUID (uuid: string): Bluebird { const options = { where: { uuid @@ -1449,7 +1457,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadByUrl (url: string, transaction?: Transaction) { + static loadByUrl (url: string, transaction?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -1460,7 +1468,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query) } - static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction) { + static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -1472,11 +1480,12 @@ export class VideoModel extends Model { ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS, - ScopeNames.WITH_THUMBNAILS + ScopeNames.WITH_THUMBNAILS, + ScopeNames.WITH_BLACKLISTED ]).findOne(query) } - static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number) { + static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Bluebird { const where = buildWhereIdOrUUID(id) const options = { @@ -1508,7 +1517,7 @@ export class VideoModel extends Model { id: number | string, t?: Transaction, userId?: number - }) { + }): Bluebird { const { id, t, userId } = parameters const where = buildWhereIdOrUUID(id) @@ -1586,7 +1595,7 @@ export class VideoModel extends Model { .then(results => results.length === 1) } - static bulkUpdateSupportField (videoChannel: VideoChannelModel, t: Transaction) { + static bulkUpdateSupportField (videoChannel: MChannel, t: Transaction) { const options = { where: { channelId: videoChannel.id @@ -1597,7 +1606,7 @@ export class VideoModel extends Model { return VideoModel.update({ support: videoChannel.support }, options) } - static getAllIdsFromChannel (videoChannel: VideoChannelModel) { + static getAllIdsFromChannel (videoChannel: MChannelId): Bluebird { const query = { attributes: [ 'id' ], where: { @@ -1769,7 +1778,7 @@ export class VideoModel extends Model { return this.VideoFiles.find(f => f.resolution === resolution) } - async addAndSaveThumbnail (thumbnail: ThumbnailModel, transaction: Transaction) { + async addAndSaveThumbnail (thumbnail: MThumbnail, transaction: Transaction) { thumbnail.videoId = this.id const savedThumbnail = await thumbnail.save({ transaction }) @@ -1782,7 +1791,7 @@ export class VideoModel extends Model { this.Thumbnails.push(savedThumbnail) } - getVideoFilename (videoFile: VideoFileModel) { + getVideoFilename (videoFile: MVideoFile) { return this.uuid + '-' + videoFile.resolution + videoFile.extname } @@ -1806,7 +1815,7 @@ export class VideoModel extends Model { return this.Thumbnails.find(t => t.type === ThumbnailType.PREVIEW) } - getTorrentFileName (videoFile: VideoFileModel) { + getTorrentFileName (videoFile: MVideoFile) { const extension = '.torrent' return this.uuid + '-' + videoFile.resolution + extension } @@ -1815,15 +1824,15 @@ export class VideoModel extends Model { return this.remote === false } - getTorrentFilePath (videoFile: VideoFileModel) { + getTorrentFilePath (videoFile: MVideoFile) { return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) } - getVideoFilePath (videoFile: VideoFileModel) { + getVideoFilePath (videoFile: MVideoFile) { return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) } - async createTorrentAndSetInfoHash (videoFile: VideoFileModel) { + async createTorrentAndSetInfoHash (videoFile: MVideoFile) { const options = { // Keep the extname, it's used by the client to stream the file inside a web browser name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`, @@ -1908,7 +1917,7 @@ export class VideoModel extends Model { return this.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) } - removeFile (videoFile: VideoFileModel, isRedundancy = false) { + removeFile (videoFile: MVideoFile, isRedundancy = false) { const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR const filePath = join(baseDir, this.getVideoFilename(videoFile)) @@ -1916,7 +1925,7 @@ export class VideoModel extends Model { .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) } - removeTorrent (videoFile: VideoFileModel) { + removeTorrent (videoFile: MVideoFile) { const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) return remove(torrentPath) .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) @@ -1957,7 +1966,7 @@ export class VideoModel extends Model { return { baseUrlHttp, baseUrlWs } } - generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) { + generateMagnetUri (videoFile: MVideoFileRedundanciesOpt, baseUrlHttp: string, baseUrlWs: string) { const xs = this.getTorrentUrl(videoFile, baseUrlHttp) const announce = this.getTrackerUrls(baseUrlHttp, baseUrlWs) let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ] @@ -1980,27 +1989,27 @@ export class VideoModel extends Model { return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] } - getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) } - getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile) } - getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) } - getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoRedundancyUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile) } - getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile) } - getBandwidthBits (videoFile: VideoFileModel) { + getBandwidthBits (videoFile: MVideoFile) { return Math.ceil((videoFile.size * 8) / this.duration) } } -- cgit v1.2.3