From 3acc50844047a37698f0618fa235c138e386a053 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Apr 2019 09:50:57 +0200 Subject: Upgrade sequelize --- server/models/video/video-playlist.ts | 47 ++++++++++++++++------------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'server/models/video/video-playlist.ts') diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 3e436acfc..63b4a0715 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -42,7 +42,7 @@ import { activityPubCollectionPagination } from '../../helpers/activitypub' import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model' import { ThumbnailModel } from './thumbnail' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' -import { fn, literal, Op, Transaction } from 'sequelize' +import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', @@ -61,11 +61,11 @@ type AvailableForListOptions = { privateAndUnlisted?: boolean } -@Scopes({ +@Scopes(() => ({ [ ScopeNames.WITH_THUMBNAIL ]: { include: [ { - model: () => ThumbnailModel, + model: ThumbnailModel, required: false } ] @@ -73,21 +73,17 @@ type AvailableForListOptions = { [ ScopeNames.WITH_VIDEOS_LENGTH ]: { attributes: { include: [ - [ - fn('COUNT', 'toto'), - 'coucou' - ], [ literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), 'videosLength' ] ] } - }, + } as FindOptions, [ ScopeNames.WITH_ACCOUNT ]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true } ] @@ -95,11 +91,11 @@ type AvailableForListOptions = { [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: { include: [ { - model: () => AccountModel.scope(AccountScopeNames.SUMMARY), + model: AccountModel.scope(AccountScopeNames.SUMMARY), required: true }, { - model: () => VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), + model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), required: false } ] @@ -107,11 +103,11 @@ type AvailableForListOptions = { [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true }, { - model: () => VideoChannelModel, + model: VideoChannelModel, required: false } ] @@ -132,7 +128,7 @@ type AvailableForListOptions = { ] } - const whereAnd: any[] = [] + const whereAnd: WhereOptions[] = [] if (options.privateAndUnlisted !== true) { whereAnd.push({ @@ -178,9 +174,9 @@ type AvailableForListOptions = { required: false } ] - } + } as FindOptions } -}) +})) @Table({ tableName: 'videoPlaylist', @@ -269,6 +265,7 @@ export class VideoPlaylistModel extends Model { VideoPlaylistElements: VideoPlaylistElementModel[] @HasOne(() => ThumbnailModel, { + foreignKey: { name: 'videoPlaylistId', allowNull: true @@ -294,7 +291,7 @@ export class VideoPlaylistModel extends Model { order: getSort(options.sort) } - const scopes = [ + const scopes: (string | ScopeOptions)[] = [ { method: [ ScopeNames.AVAILABLE_FOR_LIST, @@ -306,7 +303,7 @@ export class VideoPlaylistModel extends Model { privateAndUnlisted: options.privateAndUnlisted } as AvailableForListOptions ] - } as any, // FIXME: typings + }, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ] @@ -348,7 +345,7 @@ export class VideoPlaylistModel extends Model { model: VideoPlaylistElementModel.unscoped(), where: { videoId: { - [Op.any]: videoIds + [Op.in]: videoIds // FIXME: sequelize ANY seems broken } }, required: true @@ -427,12 +424,10 @@ export class VideoPlaylistModel extends Model { return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) } - setThumbnail (thumbnail: ThumbnailModel) { - this.Thumbnail = thumbnail - } + async setAndSaveThumbnail (thumbnail: ThumbnailModel, t: Transaction) { + thumbnail.videoPlaylistId = this.id - getThumbnail () { - return this.Thumbnail + this.Thumbnail = await thumbnail.save({ transaction: t }) } hasThumbnail () { @@ -448,13 +443,13 @@ export class VideoPlaylistModel extends Model { getThumbnailUrl () { if (!this.hasThumbnail()) return null - return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.getThumbnail().filename + return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename } getThumbnailStaticPath () { if (!this.hasThumbnail()) return null - return join(STATIC_PATHS.THUMBNAILS, this.getThumbnail().filename) + return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename) } setAsRefreshed () { -- cgit v1.2.3