From b49f22d8f9a52ab75fd38db2d377249eb58fa678 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Dec 2020 14:30:29 +0100 Subject: Upgrade sequelize to v6 --- server/models/abuse/abuse-message.ts | 6 +- server/models/abuse/abuse.ts | 7 +- server/models/abuse/video-abuse.ts | 2 +- server/models/abuse/video-comment-abuse.ts | 2 +- server/models/account/account-blocklist.ts | 9 +-- server/models/account/account-video-rate.ts | 53 ++++++++------ server/models/account/account.ts | 58 ++++++++------- server/models/account/user-notification-setting.ts | 2 +- server/models/account/user-notification.ts | 2 +- server/models/account/user-video-history.ts | 2 +- server/models/account/user.ts | 35 +++++---- server/models/activitypub/actor-follow.ts | 13 ++-- server/models/activitypub/actor.ts | 44 +++++------ server/models/application/application.ts | 2 +- server/models/avatar/avatar.ts | 2 +- server/models/model-cache.ts | 2 +- server/models/oauth/oauth-client.ts | 2 +- server/models/oauth/oauth-token.ts | 17 ++--- server/models/redundancy/video-redundancy.ts | 45 ++++++------ server/models/server/plugin.ts | 9 +-- server/models/server/server-blocklist.ts | 7 +- server/models/server/server.ts | 9 +-- server/models/utils.ts | 3 +- server/models/video/schedule-video-update.ts | 2 +- server/models/video/tag.ts | 13 ++-- server/models/video/thumbnail.ts | 2 +- server/models/video/video-blacklist.ts | 17 ++--- server/models/video/video-caption.ts | 23 +++--- server/models/video/video-change-ownership.ts | 11 ++- server/models/video/video-channel.ts | 78 ++++++++++---------- server/models/video/video-comment.ts | 13 ++-- server/models/video/video-file.ts | 2 +- server/models/video/video-import.ts | 15 ++-- server/models/video/video-live.ts | 2 +- server/models/video/video-playlist-element.ts | 33 ++++----- server/models/video/video-playlist.ts | 22 +++--- server/models/video/video-share.ts | 19 +++-- server/models/video/video-streaming-playlist.ts | 2 +- server/models/video/video-tag.ts | 2 +- server/models/video/video-view.ts | 2 +- server/models/video/video.ts | 85 +++++++++++----------- 41 files changed, 333 insertions(+), 343 deletions(-) (limited to 'server/models') diff --git a/server/models/abuse/abuse-message.ts b/server/models/abuse/abuse-message.ts index fce20f7a7..7e51b3e07 100644 --- a/server/models/abuse/abuse-message.ts +++ b/server/models/abuse/abuse-message.ts @@ -1,10 +1,10 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses' +import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models' import { AbuseMessage } from '@shared/models' import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account' -import { throwIfNotValid, getSort } from '../utils' +import { getSort, throwIfNotValid } from '../utils' import { AbuseModel } from './abuse' -import { MAbuseMessageFormattable, MAbuseMessage } from '@server/types/models' @Table({ tableName: 'abuseMessage', @@ -17,7 +17,7 @@ import { MAbuseMessageFormattable, MAbuseMessage } from '@server/types/models' } ] }) -export class AbuseMessageModel extends Model { +export class AbuseMessageModel extends Model { @AllowNull(false) @Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message')) diff --git a/server/models/abuse/abuse.ts b/server/models/abuse/abuse.ts index 290270fe2..de249d211 100644 --- a/server/models/abuse/abuse.ts +++ b/server/models/abuse/abuse.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { invert } from 'lodash' import { literal, Op, QueryTypes } from 'sequelize' import { @@ -188,7 +187,7 @@ export enum ScopeNames { } ] }) -export class AbuseModel extends Model { +export class AbuseModel extends Model { @AllowNull(false) @Default(null) @@ -265,7 +264,7 @@ export class AbuseModel extends Model { }) VideoAbuse: VideoAbuseModel - static loadByIdWithReporter (id: number): Bluebird { + static loadByIdWithReporter (id: number): Promise { const query = { where: { id @@ -281,7 +280,7 @@ export class AbuseModel extends Model { return AbuseModel.findOne(query) } - static loadFull (id: number): Bluebird { + static loadFull (id: number): Promise { const query = { where: { id diff --git a/server/models/abuse/video-abuse.ts b/server/models/abuse/video-abuse.ts index d92bcf19f..90aa0695e 100644 --- a/server/models/abuse/video-abuse.ts +++ b/server/models/abuse/video-abuse.ts @@ -14,7 +14,7 @@ import { AbuseModel } from './abuse' } ] }) -export class VideoAbuseModel extends Model { +export class VideoAbuseModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/abuse/video-comment-abuse.ts b/server/models/abuse/video-comment-abuse.ts index 8b34009b4..d3fce76a5 100644 --- a/server/models/abuse/video-comment-abuse.ts +++ b/server/models/abuse/video-comment-abuse.ts @@ -13,7 +13,7 @@ import { AbuseModel } from './abuse' } ] }) -export class VideoCommentAbuseModel extends Model { +export class VideoCommentAbuseModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index 577b7dc19..fe9168ab8 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { Op } from 'sequelize' import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models' @@ -41,7 +40,7 @@ enum ScopeNames { } ] }) -export class AccountBlocklistModel extends Model { +export class AccountBlocklistModel extends Model { @CreatedAt createdAt: Date @@ -102,7 +101,7 @@ export class AccountBlocklistModel extends Model { }) } - static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird { + static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise { const query = { where: { accountId, @@ -151,9 +150,9 @@ export class AccountBlocklistModel extends Model { }) } - static listHandlesBlockedBy (accountIds: number[]): Bluebird { + static listHandlesBlockedBy (accountIds: number[]): Promise { const query = { - attributes: [], + attributes: [ 'id' ], where: { accountId: { [Op.in]: accountIds diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 6955f45ee..d9c529491 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -1,22 +1,21 @@ import { values } from 'lodash' -import { FindOptions, Op, Transaction } from 'sequelize' +import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { VideoRateType } from '../../../shared/models/videos' -import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' -import { VideoModel } from '../video/video' -import { AccountModel } from './account' -import { ActorModel } from '../activitypub/actor' -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, MAccountVideoRateFormattable } from '@server/types/models/video/video-rate' +import { AccountVideoRate } from '../../../shared' +import { VideoRateType } from '../../../shared/models/videos' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' +import { ActorModel } from '../activitypub/actor' +import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' +import { VideoModel } from '../video/video' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' +import { AccountModel } from './account' /* Account rates per video. @@ -43,7 +42,7 @@ import { } ] }) -export class AccountVideoRateModel extends Model { +export class AccountVideoRateModel extends Model { @AllowNull(false) @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES))) @@ -84,7 +83,7 @@ export class AccountVideoRateModel extends Model { }) Account: AccountModel - static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird { + static load (accountId: number, videoId: number, transaction?: Transaction): Promise { const options: FindOptions = { where: { accountId, @@ -96,7 +95,7 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } - static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird { + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise { const options: FindOptions = { where: { [Op.or]: [ @@ -152,7 +151,7 @@ export class AccountVideoRateModel extends Model { accountName: string, videoId: number | string, t?: Transaction - ): Bluebird { + ): Promise { const options: FindOptions = { where: { videoId, @@ -240,17 +239,23 @@ export class AccountVideoRateModel extends Model { transaction: t } - const deleted = await AccountVideoRateModel.destroy(query) + await AccountVideoRateModel.destroy(query) - const options = { - transaction: t, - where: { - id: videoId - } - } + const field = type === 'like' + ? 'likes' + : 'dislikes' + + const rawQuery = `UPDATE "video" SET "${field}" = ` + + '(' + + 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' + + ') ' + + 'WHERE "video"."id" = :videoId' - if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) - else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) + return AccountVideoRateModel.sequelize.query(rawQuery, { + transaction: t, + replacements: { videoId, rateType: type }, + type: QueryTypes.UPDATE + }) }) } diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 8c244d432..c72f9c63d 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,5 +1,4 @@ -import * as Bluebird from 'bluebird' -import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Includeable, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -73,28 +72,29 @@ export type SummaryOptions = { required: false } - const query: FindOptions = { - attributes: [ 'id', 'name', 'actorId' ], - include: [ - { - attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], - model: ActorModel.unscoped(), - required: options.actorRequired ?? true, - where: whereActor, - include: [ - serverInclude, + const queryInclude: Includeable[] = [ + { + attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], + model: ActorModel.unscoped(), + required: options.actorRequired ?? true, + where: whereActor, + include: [ + serverInclude, - { - model: AvatarModel.unscoped(), - required: false - } - ] - } - ] + { + model: AvatarModel.unscoped(), + required: false + } + ] + } + ] + + const query: FindOptions = { + attributes: [ 'id', 'name', 'actorId' ] } if (options.withAccountBlockerIds) { - query.include.push({ + queryInclude.push({ attributes: [ 'id' ], model: AccountBlocklistModel.unscoped(), as: 'BlockedAccounts', @@ -120,6 +120,8 @@ export type SummaryOptions = { ] } + query.include = queryInclude + return query } })) @@ -138,7 +140,7 @@ export type SummaryOptions = { } ] }) -export class AccountModel extends Model { +export class AccountModel extends Model { @AllowNull(false) @Column @@ -244,11 +246,11 @@ export class AccountModel extends Model { return undefined } - static load (id: number, transaction?: Transaction): Bluebird { + static load (id: number, transaction?: Transaction): Promise { return AccountModel.findByPk(id, { transaction }) } - static loadByNameWithHost (nameWithHost: string): Bluebird { + static loadByNameWithHost (nameWithHost: string): Promise { const [ accountName, host ] = nameWithHost.split('@') if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) @@ -256,7 +258,7 @@ export class AccountModel extends Model { return AccountModel.loadByNameAndHost(accountName, host) } - static loadLocalByName (name: string): Bluebird { + static loadLocalByName (name: string): Promise { const fun = () => { const query = { where: { @@ -296,7 +298,7 @@ export class AccountModel extends Model { }) } - static loadByNameAndHost (name: string, host: string): Bluebird { + static loadByNameAndHost (name: string, host: string): Promise { const query = { include: [ { @@ -321,7 +323,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query = { include: [ { @@ -354,7 +356,7 @@ export class AccountModel extends Model { }) } - static loadAccountIdFromVideo (videoId: number): Bluebird { + static loadAccountIdFromVideo (videoId: number): Promise { const query = { include: [ { @@ -377,7 +379,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static listLocalsForSitemap (sort: string): Bluebird { + static listLocalsForSitemap (sort: string): Promise { const query = { attributes: [ ], offset: 0, diff --git a/server/models/account/user-notification-setting.ts b/server/models/account/user-notification-setting.ts index acc192d53..ebab8b6d2 100644 --- a/server/models/account/user-notification-setting.ts +++ b/server/models/account/user-notification-setting.ts @@ -28,7 +28,7 @@ import { UserModel } from './user' } ] }) -export class UserNotificationSettingModel extends Model { +export class UserNotificationSettingModel extends Model { @AllowNull(false) @Default(null) diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 452574dc8..52b792a5b 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -254,7 +254,7 @@ function buildAccountInclude (required: boolean, withActor = false) { } ] as (ModelIndexesOptions & { where?: WhereOptions })[] }) -export class UserNotificationModel extends Model { +export class UserNotificationModel extends Model { @AllowNull(false) @Default(null) diff --git a/server/models/account/user-video-history.ts b/server/models/account/user-video-history.ts index 76b469fbf..45171fc60 100644 --- a/server/models/account/user-video-history.ts +++ b/server/models/account/user-video-history.ts @@ -19,7 +19,7 @@ import { MUserAccountId, MUserId } from '@server/types/models' } ] }) -export class UserVideoHistoryModel extends Model { +export class UserVideoHistoryModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 10117099b..8e437c3be 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { values } from 'lodash' import { col, FindOptions, fn, literal, Op, QueryTypes, where, WhereOptions } from 'sequelize' import { @@ -16,11 +15,11 @@ import { HasOne, Is, IsEmail, + IsUUID, Model, Scopes, Table, - UpdatedAt, - IsUUID + UpdatedAt } from 'sequelize-typescript' import { MMyUserFormattable, @@ -220,7 +219,7 @@ enum ScopeNames { } ] }) -export class UserModel extends Model { +export class UserModel extends Model { @AllowNull(true) @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password', true)) @@ -483,7 +482,7 @@ export class UserModel extends Model { }) } - static listWithRight (right: UserRight): Bluebird { + static listWithRight (right: UserRight): Promise { const roles = Object.keys(USER_ROLE_LABELS) .map(k => parseInt(k, 10) as UserRole) .filter(role => hasUserRight(role, right)) @@ -499,7 +498,7 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static listUserSubscribersOf (actorId: number): Bluebird { + static listUserSubscribersOf (actorId: number): Promise { const query = { include: [ { @@ -538,7 +537,7 @@ export class UserModel extends Model { return UserModel.unscoped().findAll(query) } - static listByUsernames (usernames: string[]): Bluebird { + static listByUsernames (usernames: string[]): Promise { const query = { where: { username: usernames @@ -548,11 +547,11 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static loadById (id: number): Bluebird { + static loadById (id: number): Promise { return UserModel.unscoped().findByPk(id) } - static loadByIdWithChannels (id: number, withStats = false): Bluebird { + static loadByIdWithChannels (id: number, withStats = false): Promise { const scopes = [ ScopeNames.WITH_VIDEOCHANNELS ] @@ -562,7 +561,7 @@ export class UserModel extends Model { return UserModel.scope(scopes).findByPk(id) } - static loadByUsername (username: string): Bluebird { + static loadByUsername (username: string): Promise { const query = { where: { username: { [Op.iLike]: username } @@ -572,7 +571,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadForMeAPI (username: string): Bluebird { + static loadForMeAPI (username: string): Promise { const query = { where: { username: { [Op.iLike]: username } @@ -582,7 +581,7 @@ export class UserModel extends Model { return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query) } - static loadByEmail (email: string): Bluebird { + static loadByEmail (email: string): Promise { const query = { where: { email @@ -592,7 +591,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByUsernameOrEmail (username: string, email?: string): Bluebird { + static loadByUsernameOrEmail (username: string, email?: string): Promise { if (!email) email = username const query = { @@ -608,7 +607,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoId (videoId: number): Bluebird { + static loadByVideoId (videoId: number): Promise { const query = { include: [ { @@ -639,7 +638,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoImportId (videoImportId: number): Bluebird { + static loadByVideoImportId (videoImportId: number): Promise { const query = { include: [ { @@ -656,7 +655,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByChannelActorId (videoChannelActorId: number): Bluebird { + static loadByChannelActorId (videoChannelActorId: number): Promise { const query = { include: [ { @@ -680,7 +679,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByAccountActorId (accountActorId: number): Bluebird { + static loadByAccountActorId (accountActorId: number): Promise { const query = { include: [ { @@ -697,7 +696,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByLiveId (liveId: number): Bluebird { + static loadByLiveId (liveId: number): Promise { const query = { include: [ { diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 58bc63d34..ce6a4e267 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { difference, values } from 'lodash' import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' import { @@ -62,7 +61,7 @@ import { ActorModel, unusedActorAttributesForAPI } from './actor' } ] }) -export class ActorFollowModel extends Model { +export class ActorFollowModel extends Model { @AllowNull(false) @Column(DataType.ENUM(...values(FOLLOW_STATES))) @@ -176,7 +175,7 @@ export class ActorFollowModel extends Model { .then(results => results.length === 1) } - static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird { + static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise { const query = { where: { actorId, @@ -205,7 +204,7 @@ export class ActorFollowModel extends Model { targetName: string, targetHost: string, t?: Transaction - ): Bluebird { + ): Promise { const actorFollowingPartInclude: IncludeOptions = { model: ActorModel, required: true, @@ -258,7 +257,7 @@ export class ActorFollowModel extends Model { }) } - static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird { + static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise { const whereTab = targets .map(t => { if (t.host) { @@ -287,7 +286,7 @@ export class ActorFollowModel extends Model { }) const query = { - attributes: [], + attributes: [ 'id' ], where: { [Op.and]: [ { @@ -671,7 +670,7 @@ export class ActorFollowModel extends Model { selections.push('COUNT(*) AS "total"') - const tasks: Bluebird[] = [] + const tasks: Promise[] = [] for (const selection of selections) { let query = 'SELECT ' + selection + ' FROM "actor" ' + diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index a2fee0012..3b98e8841 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -1,5 +1,6 @@ import { values } from 'lodash' import { extname } from 'path' +import { literal, Op, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -16,6 +17,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { ModelCache } from '@server/models/model-cache' import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' import { Avatar } from '../../../shared/models/avatars/avatar.model' import { activityPubContextify } from '../../helpers/activitypub' @@ -28,13 +30,6 @@ import { } from '../../helpers/custom-validators/activitypub/actor' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' -import { AccountModel } from '../account/account' -import { AvatarModel } from '../avatar/avatar' -import { ServerModel } from '../server/server' -import { isOutdated, throwIfNotValid } from '../utils' -import { VideoChannelModel } from '../video/video-channel' -import { ActorFollowModel } from './actor-follow' -import { VideoModel } from '../video/video' import { MActor, MActorAccountChannelId, @@ -43,12 +38,17 @@ import { MActorFull, MActorHost, MActorServer, - MActorSummaryFormattable, MActorUrl, + MActorSummaryFormattable, + MActorUrl, MActorWithInboxes } from '../../types/models' -import * as Bluebird from 'bluebird' -import { Op, Transaction, literal } from 'sequelize' -import { ModelCache } from '@server/models/model-cache' +import { AccountModel } from '../account/account' +import { AvatarModel } from '../avatar/avatar' +import { ServerModel } from '../server/server' +import { isOutdated, throwIfNotValid } from '../utils' +import { VideoModel } from '../video/video' +import { VideoChannelModel } from '../video/video-channel' +import { ActorFollowModel } from './actor-follow' enum ScopeNames { FULL = 'FULL' @@ -146,7 +146,7 @@ export const unusedActorAttributesForAPI = [ } ] }) -export class ActorModel extends Model { +export class ActorModel extends Model { @AllowNull(false) @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) @@ -276,15 +276,15 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel - static load (id: number): Bluebird { + static load (id: number): Promise { return ActorModel.unscoped().findByPk(id) } - static loadFull (id: number): Bluebird { + static loadFull (id: number): Promise { return ActorModel.scope(ScopeNames.FULL).findByPk(id) } - static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird { + static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise { const query = { include: [ { @@ -328,7 +328,7 @@ export class ActorModel extends Model { .then(a => !!a) } - static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird { + static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise { const query = { where: { followersUrl: { @@ -341,7 +341,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findAll(query) } - static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird { + static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise { const fun = () => { const query = { where: { @@ -364,7 +364,7 @@ export class ActorModel extends Model { }) } - static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird { + static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise { const fun = () => { const query = { attributes: [ 'url' ], @@ -388,7 +388,7 @@ export class ActorModel extends Model { }) } - static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { + static loadByNameAndHost (preferredUsername: string, host: string): Promise { const query = { where: { preferredUsername @@ -407,7 +407,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query = { where: { url @@ -430,7 +430,7 @@ export class ActorModel extends Model { return ActorModel.unscoped().findOne(query) } - static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird { + static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise { const query = { where: { url @@ -461,7 +461,7 @@ export class ActorModel extends Model { }, { where, transaction }) } - static loadAccountActorByVideoId (videoId: number): Bluebird { + static loadAccountActorByVideoId (videoId: number): Promise { const query = { include: [ { diff --git a/server/models/application/application.ts b/server/models/application/application.ts index 3bba2c70e..909569de1 100644 --- a/server/models/application/application.ts +++ b/server/models/application/application.ts @@ -24,7 +24,7 @@ export const getServerActor = memoizee(async function () { tableName: 'application', timestamps: false }) -export class ApplicationModel extends Model { +export class ApplicationModel extends Model { @AllowNull(false) @Default(0) diff --git a/server/models/avatar/avatar.ts b/server/models/avatar/avatar.ts index d874dec99..0d246a144 100644 --- a/server/models/avatar/avatar.ts +++ b/server/models/avatar/avatar.ts @@ -18,7 +18,7 @@ import { MAvatarFormattable } from '@server/types/models' } ] }) -export class AvatarModel extends Model { +export class AvatarModel extends Model { @AllowNull(false) @Column diff --git a/server/models/model-cache.ts b/server/models/model-cache.ts index a87f99aa2..eafb5c13c 100644 --- a/server/models/model-cache.ts +++ b/server/models/model-cache.ts @@ -40,7 +40,7 @@ class ModelCache { doCache (options: { cacheType: ModelCacheType key: string - fun: () => Bluebird + fun: () => Promise whitelist?: () => boolean deleteKey?: DeleteKey }) { diff --git a/server/models/oauth/oauth-client.ts b/server/models/oauth/oauth-client.ts index 42c59bb79..8dbc1c2f5 100644 --- a/server/models/oauth/oauth-client.ts +++ b/server/models/oauth/oauth-client.ts @@ -14,7 +14,7 @@ import { OAuthTokenModel } from './oauth-token' } ] }) -export class OAuthClientModel extends Model { +export class OAuthClientModel extends Model { @AllowNull(false) @Column diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index 7f2313dad..6bc6cf27c 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts @@ -1,3 +1,4 @@ +import { Transaction } from 'sequelize' import { AfterDestroy, AfterUpdate, @@ -11,15 +12,13 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' import { logger } from '../../helpers/logger' -import { UserModel } from '../account/user' -import { OAuthClientModel } from './oauth-client' -import { Transaction } from 'sequelize' +import { clearCacheByToken } from '../../lib/oauth-model' import { AccountModel } from '../account/account' +import { UserModel } from '../account/user' import { ActorModel } from '../activitypub/actor' -import { clearCacheByToken } from '../../lib/oauth-model' -import * as Bluebird from 'bluebird' -import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' +import { OAuthClientModel } from './oauth-client' export type OAuthTokenInfo = { refreshToken: string @@ -80,7 +79,7 @@ enum ScopeNames { } ] }) -export class OAuthTokenModel extends Model { +export class OAuthTokenModel extends Model { @AllowNull(false) @Column @@ -174,7 +173,7 @@ export class OAuthTokenModel extends Model { }) } - static getByTokenAndPopulateUser (bearerToken: string): Bluebird { + static getByTokenAndPopulateUser (bearerToken: string): Promise { const query = { where: { accessToken: bearerToken @@ -190,7 +189,7 @@ export class OAuthTokenModel extends Model { }) } - static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird { + static getByRefreshTokenAndPopulateUser (refreshToken: string): Promise { const query = { where: { refreshToken diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index 1b967996c..fa96a1e39 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -1,3 +1,5 @@ +import { sample } from 'lodash' +import { col, FindOptions, fn, literal, Op, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -12,23 +14,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { ActorModel } from '../activitypub/actor' -import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' -import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' -import { VideoFileModel } from '../video/video-file' -import { VideoModel } from '../video/video' -import { VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../shared/models/redundancy' -import { logger } from '../../helpers/logger' -import { CacheFileObject, VideoPrivacy } from '../../../shared' -import { VideoChannelModel } from '../video/video-channel' -import { ServerModel } from '../server/server' -import { sample } from 'lodash' -import { isTestInstance } from '../../helpers/core-utils' -import * as Bluebird from 'bluebird' -import { col, FindOptions, fn, literal, Op, Transaction, WhereOptions } from 'sequelize' -import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' -import { CONFIG } from '../../initializers/config' +import { getServerActor } from '@server/models/application/application' import { MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models' import { VideoRedundanciesTarget } from '@shared/models/redundancy/video-redundancies-filters.model' import { @@ -36,7 +22,20 @@ import { StreamingPlaylistRedundancyInformation, VideoRedundancy } from '@shared/models/redundancy/video-redundancy.model' -import { getServerActor } from '@server/models/application/application' +import { CacheFileObject, VideoPrivacy } from '../../../shared' +import { VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../shared/models/redundancy' +import { isTestInstance } from '../../helpers/core-utils' +import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { logger } from '../../helpers/logger' +import { CONFIG } from '../../initializers/config' +import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' +import { ActorModel } from '../activitypub/actor' +import { ServerModel } from '../server/server' +import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' +import { VideoModel } from '../video/video' +import { VideoChannelModel } from '../video/video-channel' +import { VideoFileModel } from '../video/video-file' +import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' export enum ScopeNames { WITH_VIDEO = 'WITH_VIDEO' @@ -84,7 +83,7 @@ export enum ScopeNames { } ] }) -export class VideoRedundancyModel extends Model { +export class VideoRedundancyModel extends Model { @CreatedAt createdAt: Date @@ -199,7 +198,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) } - static loadByIdWithVideo (id: number, transaction?: Transaction): Bluebird { + static loadByIdWithVideo (id: number, transaction?: Transaction): Promise { const query = { where: { id }, transaction @@ -208,7 +207,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query = { where: { url @@ -251,7 +250,7 @@ export class VideoRedundancyModel extends Model { .then(r => !!r) } - static async getVideoSample (p: Bluebird) { + static async getVideoSample (p: Promise) { const rows = await p if (rows.length === 0) return undefined @@ -696,7 +695,7 @@ export class VideoRedundancyModel extends Model { const notIn = literal( '(' + - `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` + + `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` + ')' ) diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index 95fb386b5..82387af6a 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { FindAndCountOptions, json, QueryTypes } from 'sequelize' import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { MPlugin, MPluginFormattable } from '@server/types/models' @@ -29,7 +28,7 @@ import { getSort, throwIfNotValid } from '../utils' } ] }) -export class PluginModel extends Model { +export class PluginModel extends Model { @AllowNull(false) @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name')) @@ -87,7 +86,7 @@ export class PluginModel extends Model { @UpdatedAt updatedAt: Date - static listEnabledPluginsAndThemes (): Bluebird { + static listEnabledPluginsAndThemes (): Promise { const query = { where: { enabled: true, @@ -98,7 +97,7 @@ export class PluginModel extends Model { return PluginModel.findAll(query) } - static loadByNpmName (npmName: string): Bluebird { + static loadByNpmName (npmName: string): Promise { const name = this.normalizePluginName(npmName) const type = this.getTypeFromNpmName(npmName) @@ -252,7 +251,7 @@ export class PluginModel extends Model { }) } - static listInstalled (): Bluebird { + static listInstalled (): Promise { const query = { where: { uninstalled: false diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index 68cd72ee7..4dc236537 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { Op } from 'sequelize' import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models' @@ -43,7 +42,7 @@ enum ScopeNames { } ] }) -export class ServerBlocklistModel extends Model { +export class ServerBlocklistModel extends Model { @CreatedAt createdAt: Date @@ -101,7 +100,7 @@ export class ServerBlocklistModel extends Model { }) } - static loadByAccountAndHost (accountId: number, host: string): Bluebird { + static loadByAccountAndHost (accountId: number, host: string): Promise { const query = { where: { accountId @@ -120,7 +119,7 @@ export class ServerBlocklistModel extends Model { return ServerBlocklistModel.findOne(query) } - static listHostsBlockedBy (accountIds: number[]): Bluebird { + static listHostsBlockedBy (accountIds: number[]): Promise { const query = { attributes: [ ], where: { diff --git a/server/models/server/server.ts b/server/models/server/server.ts index 771200797..0e58beeaf 100644 --- a/server/models/server/server.ts +++ b/server/models/server/server.ts @@ -1,10 +1,9 @@ import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { MServer, MServerFormattable } from '@server/types/models/server' import { isHostValid } from '../../helpers/custom-validators/servers' import { ActorModel } from '../activitypub/actor' import { throwIfNotValid } from '../utils' import { ServerBlocklistModel } from './server-blocklist' -import * as Bluebird from 'bluebird' -import { MServer, MServerFormattable } from '@server/types/models/server' @Table({ tableName: 'server', @@ -15,7 +14,7 @@ import { MServer, MServerFormattable } from '@server/types/models/server' } ] }) -export class ServerModel extends Model { +export class ServerModel extends Model { @AllowNull(false) @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host')) @@ -51,7 +50,7 @@ export class ServerModel extends Model { }) BlockedByAccounts: ServerBlocklistModel[] - static load (id: number): Bluebird { + static load (id: number): Promise { const query = { where: { id @@ -61,7 +60,7 @@ export class ServerModel extends Model { return ServerModel.findOne(query) } - static loadByHost (host: string): Bluebird { + static loadByHost (host: string): Promise { const query = { where: { host diff --git a/server/models/utils.ts b/server/models/utils.ts index a06d57831..143c1a23c 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -113,7 +113,8 @@ function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldN function buildTrigramSearchIndex (indexName: string, attribute: string) { return { name: indexName, - fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ], + // FIXME: gin_trgm_ops is not taken into account in Sequelize 6, so adding it ourselves in the literal function + fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + ')) gin_trgm_ops') as any ], using: 'gin', operator: 'gin_trgm_ops' } diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts index 1ce6bbfe5..22b08e91a 100644 --- a/server/models/video/schedule-video-update.ts +++ b/server/models/video/schedule-video-update.ts @@ -16,7 +16,7 @@ import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdateVideoAll } from '@ } ] }) -export class ScheduleVideoUpdateModel extends Model { +export class ScheduleVideoUpdateModel extends Model { @AllowNull(false) @Default(null) diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index adbc4fb7d..d04205703 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts @@ -1,12 +1,11 @@ -import * as Bluebird from 'bluebird' -import { fn, QueryTypes, Transaction, col } from 'sequelize' +import { col, fn, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { MTag } from '@server/types/models' +import { VideoPrivacy, VideoState } from '../../../shared/models/videos' import { isVideoTagValid } from '../../helpers/custom-validators/videos' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoTagModel } from './video-tag' -import { VideoPrivacy, VideoState } from '../../../shared/models/videos' -import { MTag } from '@server/types/models' @Table({ tableName: 'tag', @@ -22,7 +21,7 @@ import { MTag } from '@server/types/models' } ] }) -export class TagModel extends Model { +export class TagModel extends Model { @AllowNull(false) @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) @@ -45,7 +44,7 @@ export class TagModel extends Model { static findOrCreateTags (tags: string[], transaction: Transaction): Promise { if (tags === null) return Promise.resolve([]) - const tasks: Bluebird[] = [] + const tasks: Promise[] = [] tags.forEach(tag => { const query = { where: { @@ -66,7 +65,7 @@ export class TagModel extends Model { } // threshold corresponds to how many video the field should have to be returned - static getRandomSamples (threshold: number, count: number): Bluebird { + static getRandomSamples (threshold: number, count: number): Promise { const query = 'SELECT tag.name FROM tag ' + 'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' + 'INNER JOIN video ON video.id = "videoTag"."videoId" ' + diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 20c1e5858..6878a3155 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -34,7 +34,7 @@ import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' } ] }) -export class ThumbnailModel extends Model { +export class ThumbnailModel extends Model { @AllowNull(false) @Column diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 36d2a30fa..aa18896da 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,14 +1,13 @@ +import { FindOptions } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { getBlacklistSort, SortType, throwIfNotValid, searchAttribute } from '../utils' -import { VideoModel } from './video' -import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' -import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' +import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models' import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' +import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' -import { FindOptions } from 'sequelize' +import { getBlacklistSort, searchAttribute, SortType, throwIfNotValid } from '../utils' import { ThumbnailModel } from './thumbnail' -import * as Bluebird from 'bluebird' -import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models' +import { VideoModel } from './video' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' @Table({ tableName: 'videoBlacklist', @@ -19,7 +18,7 @@ import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/model } ] }) -export class VideoBlacklistModel extends Model { +export class VideoBlacklistModel extends Model { @AllowNull(true) @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true)) @@ -109,7 +108,7 @@ export class VideoBlacklistModel extends Model { }) } - static loadByVideoId (id: number): Bluebird { + static loadByVideoId (id: number): Promise { const query = { where: { videoId: id diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index b68a6e99f..e8e883dd0 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -1,3 +1,5 @@ +import { remove } from 'fs-extra' +import { join } from 'path' import { OrderItem, Transaction } from 'sequelize' import { AllowNull, @@ -13,18 +15,15 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' -import { VideoModel } from './video' -import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' +import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' +import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' -import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants' -import { join } from 'path' +import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { logger } from '../../helpers/logger' -import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' -import * as Bluebird from 'bluebird' -import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' -import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' +import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants' +import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' +import { VideoModel } from './video' export enum ScopeNames { WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' @@ -54,7 +53,7 @@ export enum ScopeNames { } ] }) -export class VideoCaptionModel extends Model { +export class VideoCaptionModel extends Model { @CreatedAt createdAt: Date @@ -101,7 +100,7 @@ export class VideoCaptionModel extends Model { return undefined } - static loadByVideoIdAndLanguage (videoId: string | number, language: string): Bluebird { + static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise { const videoInclude = { model: VideoModel.unscoped(), attributes: [ 'id', 'remote', 'uuid' ], @@ -131,7 +130,7 @@ export class VideoCaptionModel extends Model { .then(([ caption ]) => caption) } - static listVideoCaptions (videoId: number): Bluebird { + static listVideoCaptions (videoId: number): Promise { 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 ac0ab7e8b..298e8bfe2 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -1,10 +1,9 @@ import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' -import { AccountModel } from '../account/account' -import { ScopeNames as VideoScopeNames, VideoModel } from './video' +import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' +import { AccountModel } from '../account/account' import { getSort } from '../utils' -import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' -import * as Bluebird from 'bluebird' +import { ScopeNames as VideoScopeNames, VideoModel } from './video' enum ScopeNames { WITH_ACCOUNTS = 'WITH_ACCOUNTS', @@ -54,7 +53,7 @@ enum ScopeNames { ] } })) -export class VideoChangeOwnershipModel extends Model { +export class VideoChangeOwnershipModel extends Model { @CreatedAt createdAt: Date @@ -119,7 +118,7 @@ export class VideoChangeOwnershipModel extends Model ]).then(([ count, rows ]) => ({ total: count, data: rows })) } - static load (id: number): Bluebird { + static load (id: number): Promise { 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 0c8aef18f..178878c55 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -1,5 +1,4 @@ -import * as Bluebird from 'bluebird' -import { FindOptions, literal, Op, ScopeOptions } from 'sequelize' +import { FindOptions, Includeable, literal, Op, ScopeOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -119,30 +118,31 @@ export type SummaryOptions = { } }, [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { + const include: Includeable[] = [ + { + attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], + model: ActorModel.unscoped(), + required: options.actorRequired ?? true, + include: [ + { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: false + }, + { + model: AvatarModel.unscoped(), + required: false + } + ] + } + ] + const base: FindOptions = { - attributes: [ 'id', 'name', 'description', 'actorId' ], - include: [ - { - attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], - model: ActorModel.unscoped(), - required: options.actorRequired ?? true, - include: [ - { - attributes: [ 'host' ], - model: ServerModel.unscoped(), - required: false - }, - { - model: AvatarModel.unscoped(), - required: false - } - ] - } - ] + attributes: [ 'id', 'name', 'description', 'actorId' ] } if (options.withAccount === true) { - base.include.push({ + include.push({ model: AccountModel.scope({ method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ] }), @@ -150,6 +150,8 @@ export type SummaryOptions = { }) } + base.include = include + return base }, [ScopeNames.WITH_ACCOUNT]: { @@ -221,7 +223,7 @@ export type SummaryOptions = { } ] }) -export class VideoChannelModel extends Model { +export class VideoChannelModel extends Model { @AllowNull(false) @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) @@ -328,18 +330,17 @@ export class VideoChannelModel extends Model { order: getSort(parameters.sort) } - const scopes = { - method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] - } return VideoChannelModel - .scope(scopes) + .scope({ + method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] + }) .findAndCountAll(query) .then(({ rows, count }) => { return { total: count, data: rows } }) } - static listLocalsForSitemap (sort: string): Bluebird { + static listLocalsForSitemap (sort: string): Promise { const query = { attributes: [ ], offset: 0, @@ -391,11 +392,10 @@ export class VideoChannelModel extends Model { } } - const scopes = { - method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ] - } return VideoChannelModel - .scope(scopes) + .scope({ + method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ] + }) .findAndCountAll(query) .then(({ rows, count }) => { return { total: count, data: rows } @@ -457,13 +457,13 @@ export class VideoChannelModel extends Model { }) } - static loadByIdAndPopulateAccount (id: number): Bluebird { + static loadByIdAndPopulateAccount (id: number): Promise { return VideoChannelModel.unscoped() .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) .findByPk(id) } - static loadByIdAndAccount (id: number, accountId: number): Bluebird { + static loadByIdAndAccount (id: number, accountId: number): Promise { const query = { where: { id, @@ -476,13 +476,13 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadAndPopulateAccount (id: number): Bluebird { + static loadAndPopulateAccount (id: number): Promise { return VideoChannelModel.unscoped() .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) .findByPk(id) } - static loadByUrlAndPopulateAccount (url: string): Bluebird { + static loadByUrlAndPopulateAccount (url: string): Promise { const query = { include: [ { @@ -508,7 +508,7 @@ export class VideoChannelModel extends Model { return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) } - static loadLocalByNameAndPopulateAccount (name: string): Bluebird { + static loadLocalByNameAndPopulateAccount (name: string): Promise { const query = { include: [ { @@ -527,7 +527,7 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird { + static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise { const query = { include: [ { @@ -552,7 +552,7 @@ export class VideoChannelModel extends Model { .findOne(query) } - static loadAndPopulateAccountAndVideos (id: number): Bluebird { + static loadAndPopulateAccountAndVideos (id: number): Promise { const options = { include: [ VideoModel diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index ed4a345eb..8d1c38826 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { uniq } from 'lodash' import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { @@ -174,7 +173,7 @@ export enum ScopeNames { } ] }) -export class VideoCommentModel extends Model { +export class VideoCommentModel extends Model { @CreatedAt createdAt: Date @@ -255,7 +254,7 @@ export class VideoCommentModel extends Model { }) CommentAbuses: VideoCommentAbuseModel[] - static loadById (id: number, t?: Transaction): Bluebird { + static loadById (id: number, t?: Transaction): Promise { const query: FindOptions = { where: { id @@ -267,7 +266,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findOne(query) } - static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird { + static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise { const query: FindOptions = { where: { id @@ -281,7 +280,7 @@ export class VideoCommentModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird { + static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise { const query: FindOptions = { where: { url @@ -293,7 +292,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) } - static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird { + static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise { const query: FindOptions = { where: { url @@ -501,7 +500,7 @@ export class VideoCommentModel extends Model { }) } - static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird { + static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise { const query = { order: [ [ 'createdAt', order ] ] as Order, where: { diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index d48c9f5d4..48b337c68 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -101,7 +101,7 @@ export enum ScopeNames { } ] }) -export class VideoFileModel extends Model { +export class VideoFileModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/video/video-import.ts b/server/models/video/video-import.ts index ea1e085af..f3ed651b2 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts @@ -13,15 +13,14 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants' -import { getSort, throwIfNotValid } from '../utils' -import { ScopeNames as VideoModelScopeNames, VideoModel } from './video' -import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' +import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import' import { VideoImport, VideoImportState } from '../../../shared' +import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos' +import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants' import { UserModel } from '../account/user' -import * as Bluebird from 'bluebird' -import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import' +import { getSort, throwIfNotValid } from '../utils' +import { ScopeNames as VideoModelScopeNames, VideoModel } from './video' @DefaultScope(() => ({ include: [ @@ -52,7 +51,7 @@ import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/mode } ] }) -export class VideoImportModel extends Model { +export class VideoImportModel extends Model { @CreatedAt createdAt: Date @@ -120,7 +119,7 @@ export class VideoImportModel extends Model { return undefined } - static loadAndPopulateVideo (id: number): Bluebird { + static loadAndPopulateVideo (id: number): Promise { return VideoImportModel.findByPk(id) } diff --git a/server/models/video/video-live.ts b/server/models/video/video-live.ts index 875ba9b31..cb4a9b896 100644 --- a/server/models/video/video-live.ts +++ b/server/models/video/video-live.ts @@ -28,7 +28,7 @@ import { VideoBlacklistModel } from './video-blacklist' } ] }) -export class VideoLiveModel extends Model { +export class VideoLiveModel extends Model { @AllowNull(true) @Column(DataType.STRING) diff --git a/server/models/video/video-playlist-element.ts b/server/models/video/video-playlist-element.ts index d357766e9..d2d7e2740 100644 --- a/server/models/video/video-playlist-element.ts +++ b/server/models/video/video-playlist-element.ts @@ -1,3 +1,4 @@ +import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -13,18 +14,8 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video' -import { VideoPlaylistModel } from './video-playlist' -import { getSort, throwIfNotValid } from '../utils' -import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { CONSTRAINTS_FIELDS } from '../../initializers/constants' -import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object' import validator from 'validator' -import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize' -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 { MUserAccountId } from '@server/types/models' import { MVideoPlaylistElement, MVideoPlaylistElementAP, @@ -32,7 +23,15 @@ import { MVideoPlaylistElementVideoUrlPlaylistPrivacy, MVideoPlaylistVideoThumbnail } from '@server/types/models/video/video-playlist-element' -import { MUserAccountId } from '@server/types/models' +import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object' +import { VideoPrivacy } from '../../../shared/models/videos' +import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { CONSTRAINTS_FIELDS } from '../../initializers/constants' +import { AccountModel } from '../account/account' +import { getSort, throwIfNotValid } from '../utils' +import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video' +import { VideoPlaylistModel } from './video-playlist' @Table({ tableName: 'videoPlaylistElement', @@ -49,7 +48,7 @@ import { MUserAccountId } from '@server/types/models' } ] }) -export class VideoPlaylistElementModel extends Model { +export class VideoPlaylistElementModel extends Model { @CreatedAt createdAt: Date @@ -166,7 +165,7 @@ export class VideoPlaylistElementModel extends Model ]).then(([ total, data ]) => ({ total, data })) } - static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird { + static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise { const query = { where: { videoPlaylistId, @@ -177,14 +176,14 @@ export class VideoPlaylistElementModel extends Model return VideoPlaylistElementModel.findOne(query) } - static loadById (playlistElementId: number | string): Bluebird { + static loadById (playlistElementId: number | string): Promise { return VideoPlaylistElementModel.findByPk(playlistElementId) } static loadByPlaylistAndElementIdForAP ( playlistId: number | string, playlistElementId: number - ): Bluebird { + ): Promise { const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId } const query = { @@ -226,7 +225,7 @@ export class VideoPlaylistElementModel extends Model }) } - static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird { + static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise { const query = { order: getSort('position'), where: { diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 9f9e0b069..93ecf8cea 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { join } from 'path' import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' import { @@ -125,7 +124,6 @@ type AvailableForListOptions = { ] }, [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { - let whereActor: WhereOptions = {} const whereAnd: WhereOptions[] = [] @@ -182,15 +180,13 @@ type AvailableForListOptions = { [Op.and]: whereAnd } - const accountScope = { - method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] - } - return { where, include: [ { - model: AccountModel.scope(accountScope), + model: AccountModel.scope({ + method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] + }), required: true }, { @@ -217,7 +213,7 @@ type AvailableForListOptions = { } ] }) -export class VideoPlaylistModel extends Model { +export class VideoPlaylistModel extends Model { @CreatedAt createdAt: Date @@ -367,7 +363,7 @@ export class VideoPlaylistModel extends Model { }) } - static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird { + static listPlaylistIdsOf (accountId: number, videoIds: number[]): Promise { const query = { attributes: [ 'id' ], where: { @@ -392,7 +388,7 @@ export class VideoPlaylistModel extends Model { static doesPlaylistExist (url: string) { const query = { - attributes: [], + attributes: [ 'id' ], where: { url } @@ -403,7 +399,7 @@ export class VideoPlaylistModel extends Model { .then(e => !!e) } - static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird { + static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Promise { const where = buildWhereIdOrUUID(id) const query = { @@ -416,7 +412,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird { + static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Promise { const where = buildWhereIdOrUUID(id) const query = { @@ -429,7 +425,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccount (url: string): Bluebird { + static loadByUrlAndPopulateAccount (url: string): Promise { const query = { where: { url diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index f9ee94589..b7f5f3fa3 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -1,13 +1,12 @@ -import * as Bluebird from 'bluebird' +import { literal, Op, Transaction } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' +import { MActorDefault } from '../../types/models' +import { MVideoShareActor, MVideoShareFull } from '../../types/models/video' import { ActorModel } from '../activitypub/actor' import { buildLocalActorIdsIn, throwIfNotValid } from '../utils' import { VideoModel } from './video' -import { literal, Op, Transaction } from 'sequelize' -import { MVideoShareActor, MVideoShareFull } from '../../types/models/video' -import { MActorDefault } from '../../types/models' enum ScopeNames { FULL = 'FULL', @@ -51,7 +50,7 @@ enum ScopeNames { } ] }) -export class VideoShareModel extends Model { +export class VideoShareModel extends Model { @AllowNull(false) @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) @@ -88,7 +87,7 @@ export class VideoShareModel extends Model { }) Video: VideoModel - static load (actorId: number | string, videoId: number | string, t?: Transaction): Bluebird { + static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise { return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({ where: { actorId, @@ -98,7 +97,7 @@ export class VideoShareModel extends Model { }) } - static loadByUrl (url: string, t: Transaction): Bluebird { + static loadByUrl (url: string, t: Transaction): Promise { return VideoShareModel.scope(ScopeNames.FULL).findOne({ where: { url @@ -107,7 +106,7 @@ export class VideoShareModel extends Model { }) } - static loadActorsByShare (videoId: number, t: Transaction): Bluebird { + static loadActorsByShare (videoId: number, t: Transaction): Promise { const query = { where: { videoId @@ -125,7 +124,7 @@ export class VideoShareModel extends Model { .then((res: MVideoShareFull[]) => res.map(r => r.Actor)) } - static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird { + static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise { const safeOwnerId = parseInt(actorOwnerId + '', 10) // /!\ On actor model @@ -150,7 +149,7 @@ export class VideoShareModel extends Model { return ActorModel.findAll(query) } - static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird { + static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise { const safeChannelId = parseInt(videoChannelId + '', 10) // /!\ On actor model diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index 73bd89844..148768c21 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -40,7 +40,7 @@ import { logger } from '@server/helpers/logger' } ] }) -export class VideoStreamingPlaylistModel extends Model { +export class VideoStreamingPlaylistModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/video/video-tag.ts b/server/models/video/video-tag.ts index ca15e3426..5052b8c4d 100644 --- a/server/models/video/video-tag.ts +++ b/server/models/video/video-tag.ts @@ -13,7 +13,7 @@ import { VideoModel } from './video' } ] }) -export class VideoTagModel extends Model { +export class VideoTagModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/video/video-view.ts b/server/models/video/video-view.ts index 40db5effd..992cf258a 100644 --- a/server/models/video/video-view.ts +++ b/server/models/video/video-view.ts @@ -14,7 +14,7 @@ import * as Sequelize from 'sequelize' } ] }) -export class VideoViewModel extends Model { +export class VideoViewModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/video/video.ts b/server/models/video/video.ts index a43abbc09..d3fed338a 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -2,7 +2,7 @@ import * as Bluebird from 'bluebird' import { remove } from 'fs-extra' import { maxBy, minBy, pick } from 'lodash' import { join } from 'path' -import { FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Includeable, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -190,26 +190,26 @@ export type AvailableForListIDsOptions = { attributes: [ 'id', 'url', 'uuid', 'remote' ] }, [ScopeNames.FOR_API]: (options: ForAPIOptions) => { - const query: FindOptions = { - include: [ - { - model: VideoChannelModel.scope({ - method: [ - VideoChannelScopeNames.SUMMARY, { - withAccount: true, - withAccountBlockerIds: options.withAccountBlockerIds - } as SummaryOptions - ] - }), - required: true - }, - { - attributes: [ 'type', 'filename' ], - model: ThumbnailModel, - required: false - } - ] - } + const include: Includeable[] = [ + { + model: VideoChannelModel.scope({ + method: [ + VideoChannelScopeNames.SUMMARY, { + withAccount: true, + withAccountBlockerIds: options.withAccountBlockerIds + } as SummaryOptions + ] + }), + required: true + }, + { + attributes: [ 'type', 'filename' ], + model: ThumbnailModel, + required: false + } + ] + + const query: FindOptions = {} if (options.ids) { query.where = { @@ -220,14 +220,14 @@ export type AvailableForListIDsOptions = { } if (options.withFiles === true) { - query.include.push({ + include.push({ model: VideoFileModel, required: true }) } if (options.videoPlaylistId) { - query.include.push({ + include.push({ model: VideoPlaylistElementModel.unscoped(), required: true, where: { @@ -236,6 +236,8 @@ export type AvailableForListIDsOptions = { }) } + query.include = include + return query }, [ScopeNames.WITH_THUMBNAILS]: { @@ -477,7 +479,7 @@ export type AvailableForListIDsOptions = { } ] }) -export class VideoModel extends Model { +export class VideoModel extends Model { @AllowNull(false) @Default(DataType.UUIDV4) @@ -860,7 +862,7 @@ export class VideoModel extends Model { return undefined } - static listLocal (): Bluebird { + static listLocal (): Promise { const query = { where: { remote: false @@ -988,7 +990,7 @@ export class VideoModel extends Model { }) } - static listPublishedLiveIds () { + static async listPublishedLiveIds () { const options = { attributes: [ 'id' ], where: { @@ -997,8 +999,9 @@ export class VideoModel extends Model { } } - return VideoModel.findAll(options) - .map(v => v.id) + const result = await VideoModel.findAll(options) + + return result.map(v => v.id) } static listUserVideosForApi ( @@ -1214,7 +1217,7 @@ export class VideoModel extends Model { return VideoModel.count(options) } - static load (id: number | string, t?: Transaction): Bluebird { + static load (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1224,7 +1227,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithBlacklist (id: number | string, t?: Transaction): Bluebird { + static loadWithBlacklist (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1237,7 +1240,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadImmutableAttributes (id: number | string, t?: Transaction): Bluebird { + static loadImmutableAttributes (id: number | string, t?: Transaction): Promise { const fun = () => { const query = { where: buildWhereIdOrUUID(id), @@ -1255,7 +1258,7 @@ export class VideoModel extends Model { }) } - static loadWithRights (id: number | string, t?: Transaction): Bluebird { + static loadWithRights (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1269,7 +1272,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadOnlyId (id: number | string, t?: Transaction): Bluebird { + static loadOnlyId (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { @@ -1281,7 +1284,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Bluebird { + static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Promise { const where = buildWhereIdOrUUID(id) const query = { @@ -1297,7 +1300,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadByUUID (uuid: string): Bluebird { + static loadByUUID (uuid: string): Promise { const options = { where: { uuid @@ -1307,7 +1310,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query: FindOptions = { where: { url @@ -1318,7 +1321,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query) } - static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Bluebird { + static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Promise { const fun = () => { const query: FindOptions = { where: { @@ -1338,7 +1341,7 @@ export class VideoModel extends Model { }) } - static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Bluebird { + static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Promise { const query: FindOptions = { where: { url @@ -1355,7 +1358,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Bluebird { + static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Promise { const where = buildWhereIdOrUUID(id) const options = { @@ -1388,7 +1391,7 @@ export class VideoModel extends Model { id: number | string t?: Transaction userId?: number - }): Bluebird { + }): Promise { const { id, t, userId } = parameters const where = buildWhereIdOrUUID(id) @@ -1487,7 +1490,7 @@ export class VideoModel extends Model { return VideoModel.update({ support: videoChannel.support }, options) } - static getAllIdsFromChannel (videoChannel: MChannelId): Bluebird { + static getAllIdsFromChannel (videoChannel: MChannelId): Promise { const query = { attributes: [ 'id' ], where: { -- cgit v1.2.3