From 16c016e8b1d5ca46343d3363f9a49e24c5d7c944 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 May 2021 14:09:04 +0200 Subject: Stricter models typing --- server/models/video/schedule-video-update.ts | 9 +++++---- server/models/video/tag.ts | 3 ++- server/models/video/thumbnail.ts | 3 ++- server/models/video/video-blacklist.ts | 3 ++- server/models/video/video-caption.ts | 3 ++- server/models/video/video-change-ownership.ts | 3 ++- server/models/video/video-channel.ts | 3 ++- server/models/video/video-comment.ts | 3 ++- server/models/video/video-file.ts | 3 ++- server/models/video/video-import.ts | 3 ++- server/models/video/video-live.ts | 3 ++- server/models/video/video-playlist-element.ts | 6 ++++-- server/models/video/video-playlist.ts | 3 ++- server/models/video/video-query-builder.ts | 22 +++++++++++----------- server/models/video/video-share.ts | 3 ++- server/models/video/video-streaming-playlist.ts | 3 ++- server/models/video/video-tag.ts | 3 ++- server/models/video/video-view.ts | 5 +++-- server/models/video/video.ts | 9 +++++---- 19 files changed, 56 insertions(+), 37 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts index 22b08e91a..b0952c431 100644 --- a/server/models/video/schedule-video-update.ts +++ b/server/models/video/schedule-video-update.ts @@ -1,8 +1,9 @@ -import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { ScopeNames as VideoScopeNames, VideoModel } from './video' -import { VideoPrivacy } from '../../../shared/models/videos' import { Op, Transaction } from 'sequelize' +import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdateVideoAll } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' +import { VideoPrivacy } from '../../../shared/models/videos' +import { ScopeNames as VideoScopeNames, VideoModel } from './video' @Table({ tableName: 'scheduleVideoUpdate', @@ -16,7 +17,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 d04205703..c1eebe27f 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts @@ -1,6 +1,7 @@ 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 { AttributesOnly } from '@shared/core-utils' import { VideoPrivacy, VideoState } from '../../../shared/models/videos' import { isVideoTagValid } from '../../helpers/custom-validators/videos' import { throwIfNotValid } from '../utils' @@ -21,7 +22,7 @@ import { VideoTagModel } from './video-tag' } ] }) -export class TagModel extends Model { +export class TagModel extends Model>> { @AllowNull(false) @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index f1187c8d6..3388478d9 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -17,6 +17,7 @@ import { } from 'sequelize-typescript' import { afterCommitIfTransaction } from '@server/helpers/database-utils' import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' @@ -40,7 +41,7 @@ import { VideoPlaylistModel } from './video-playlist' } ] }) -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 aa18896da..98f4ec9c5 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,6 +1,7 @@ import { FindOptions } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' @@ -18,7 +19,7 @@ import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } ] }) -export class VideoBlacklistModel extends Model { +export class VideoBlacklistModel extends Model>> { @AllowNull(true) @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true)) diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index bfdec73e9..d2c742b66 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -17,6 +17,7 @@ import { } from 'sequelize-typescript' import { v4 as uuidv4 } from 'uuid' import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { logger } from '../../helpers/logger' @@ -57,7 +58,7 @@ export enum ScopeNames { } ] }) -export class VideoCaptionModel extends Model { +export class VideoCaptionModel extends Model>> { @CreatedAt createdAt: Date diff --git a/server/models/video/video-change-ownership.ts b/server/models/video/video-change-ownership.ts index 298e8bfe2..7d20a954d 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -1,5 +1,6 @@ import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' +import { AttributesOnly } from '@shared/core-utils' import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' import { AccountModel } from '../account/account' import { getSort } from '../utils' @@ -53,7 +54,7 @@ enum ScopeNames { ] } })) -export class VideoChangeOwnershipModel extends Model { +export class VideoChangeOwnershipModel extends Model>> { @CreatedAt createdAt: Date diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 8e4b78723..8c4357009 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -19,6 +19,7 @@ import { } from 'sequelize-typescript' import { setAsUpdated } from '@server/helpers/database-utils' import { MAccountActor } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { ActivityPubActor } from '../../../shared/models/activitypub' import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' import { @@ -246,7 +247,7 @@ export type SummaryOptions = { } ] }) -export class VideoChannelModel extends Model { +export class VideoChannelModel extends Model>> { @AllowNull(false) @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index ffd324526..bdf5d86bc 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -16,6 +16,7 @@ import { } from 'sequelize-typescript' import { getServerActor } from '@server/models/application/application' import { MAccount, MAccountId, MUserAccountId } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { VideoPrivacy } from '@shared/models' import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' @@ -173,7 +174,7 @@ export enum ScopeNames { } ] }) -export class VideoCommentModel extends Model { +export class VideoCommentModel extends Model>> { @CreatedAt createdAt: Date diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 1ad796104..e3fa2f3d2 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -25,6 +25,7 @@ import { logger } from '@server/helpers/logger' import { extractVideo } from '@server/helpers/video' import { getTorrentFilePath } from '@server/lib/video-paths' import { MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { isVideoFileExtnameValid, isVideoFileInfoHashValid, @@ -149,7 +150,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 d8f09e1e5..5c73fb07c 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts @@ -15,6 +15,7 @@ import { } from 'sequelize-typescript' import { afterCommitIfTransaction } from '@server/helpers/database-utils' import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import' +import { AttributesOnly } from '@shared/core-utils' import { VideoImport, VideoImportState } from '../../../shared' import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos' @@ -52,7 +53,7 @@ import { ScopeNames as VideoModelScopeNames, VideoModel } from './video' } ] }) -export class VideoImportModel extends Model { +export class VideoImportModel extends Model>> { @CreatedAt createdAt: Date diff --git a/server/models/video/video-live.ts b/server/models/video/video-live.ts index cb4a9b896..014491d50 100644 --- a/server/models/video/video-live.ts +++ b/server/models/video/video-live.ts @@ -1,6 +1,7 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' import { WEBSERVER } from '@server/initializers/constants' import { MVideoLive, MVideoLiveVideo } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { LiveVideo, VideoState } from '@shared/models' import { VideoModel } from './video' import { VideoBlacklistModel } from './video-blacklist' @@ -28,7 +29,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 d2d7e2740..e6906cb19 100644 --- a/server/models/video/video-playlist-element.ts +++ b/server/models/video/video-playlist-element.ts @@ -32,6 +32,7 @@ import { AccountModel } from '../account/account' import { getSort, throwIfNotValid } from '../utils' import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video' import { VideoPlaylistModel } from './video-playlist' +import { AttributesOnly } from '@shared/core-utils' @Table({ tableName: 'videoPlaylistElement', @@ -48,7 +49,7 @@ import { VideoPlaylistModel } from './video-playlist' } ] }) -export class VideoPlaylistElementModel extends Model { +export class VideoPlaylistElementModel extends Model>> { @CreatedAt createdAt: Date @@ -274,7 +275,8 @@ export class VideoPlaylistElementModel extends Model { validate: false // We use a literal to update the position } - return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query) + const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) + return VideoPlaylistElementModel.update({ position: positionQuery as any }, query) } static increasePositionOf ( diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index b48dd2945..c293287d3 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -19,6 +19,7 @@ import { } from 'sequelize-typescript' import { v4 as uuidv4 } from 'uuid' import { MAccountId, MChannelId } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' @@ -221,7 +222,7 @@ type AvailableForListOptions = { } ] }) -export class VideoPlaylistModel extends Model { +export class VideoPlaylistModel extends Model>> { @CreatedAt createdAt: Date diff --git a/server/models/video/video-query-builder.ts b/server/models/video/video-query-builder.ts index 155afe64b..2aa5e65c8 100644 --- a/server/models/video/video-query-builder.ts +++ b/server/models/video/video-query-builder.ts @@ -1,9 +1,9 @@ -import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models' -import { buildDirectionAndField, createSafeIn } from '@server/models/utils' -import { Model } from 'sequelize-typescript' -import { MUserAccountId, MUserId } from '@server/types/models' +import { Sequelize } from 'sequelize/types' import validator from 'validator' import { exists } from '@server/helpers/custom-validators/misc' +import { buildDirectionAndField, createSafeIn } from '@server/models/utils' +import { MUserAccountId, MUserId } from '@server/types/models' +import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models' export type BuildVideosQueryOptions = { attributes?: string[] @@ -55,7 +55,7 @@ export type BuildVideosQueryOptions = { having?: string } -function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) { +function buildListQuery (sequelize: Sequelize, options: BuildVideosQueryOptions) { const and: string[] = [] const joins: string[] = [] const replacements: any = {} @@ -77,7 +77,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) const blockerIds = [ options.serverAccountId ] if (options.user) blockerIds.push(options.user.Account.id) - const inClause = createSafeIn(model, blockerIds) + const inClause = createSafeIn(sequelize, blockerIds) and.push( 'NOT EXISTS (' + @@ -179,7 +179,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) 'EXISTS (' + ' SELECT 1 FROM "videoTag" ' + ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + - ' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsOneOfLower) + ') ' + + ' WHERE lower("tag"."name") IN (' + createSafeIn(sequelize, tagsOneOfLower) + ') ' + ' AND "video"."id" = "videoTag"."videoId"' + ')' ) @@ -192,7 +192,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) 'EXISTS (' + ' SELECT 1 FROM "videoTag" ' + ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + - ' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsAllOfLower) + ') ' + + ' WHERE lower("tag"."name") IN (' + createSafeIn(sequelize, tagsAllOfLower) + ') ' + ' AND "video"."id" = "videoTag"."videoId" ' + ' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length + ')' @@ -232,7 +232,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) languagesQueryParts.push( 'EXISTS (' + ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' + - ' IN (' + createSafeIn(model, languages) + ') AND ' + + ' IN (' + createSafeIn(sequelize, languages) + ') AND ' + ' "videoCaption"."videoId" = "video"."id"' + ')' ) @@ -345,8 +345,8 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) } if (options.search) { - const escapedSearch = model.sequelize.escape(options.search) - const escapedLikeSearch = model.sequelize.escape('%' + options.search + '%') + const escapedSearch = sequelize.escape(options.search) + const escapedLikeSearch = sequelize.escape('%' + options.search + '%') cte.push( '"trigramSearch" AS (' + diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index 99a24dbdf..505c305e2 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -1,5 +1,6 @@ import { literal, Op, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { AttributesOnly } from '@shared/core-utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { MActorDefault } from '../../types/models' @@ -50,7 +51,7 @@ enum ScopeNames { } ] }) -export class VideoShareModel extends Model { +export class VideoShareModel extends Model>> { @AllowNull(false) @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index c9375b433..d627e8c9d 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -13,6 +13,7 @@ import { CONSTRAINTS_FIELDS, MEMOIZE_LENGTH, MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_ import { VideoRedundancyModel } from '../redundancy/video-redundancy' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' +import { AttributesOnly } from '@shared/core-utils' @Table({ tableName: 'videoStreamingPlaylist', @@ -30,7 +31,7 @@ import { VideoModel } from './video' } ] }) -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 5052b8c4d..1285d375b 100644 --- a/server/models/video/video-tag.ts +++ b/server/models/video/video-tag.ts @@ -1,4 +1,5 @@ import { Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { AttributesOnly } from '@shared/core-utils' import { TagModel } from './tag' import { VideoModel } from './video' @@ -13,7 +14,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 992cf258a..dfc6296ce 100644 --- a/server/models/video/video-view.ts +++ b/server/models/video/video-view.ts @@ -1,6 +1,7 @@ +import * as Sequelize from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table } from 'sequelize-typescript' +import { AttributesOnly } from '@shared/core-utils' import { VideoModel } from './video' -import * as Sequelize from 'sequelize' @Table({ tableName: 'videoView', @@ -14,7 +15,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 f8a099d9c..749ef7197 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -31,6 +31,7 @@ import { LiveManager } from '@server/lib/live-manager' import { getHLSDirectory, getVideoFilePath } from '@server/lib/video-paths' import { getServerActor } from '@server/models/application/application' import { ModelCache } from '@server/models/model-cache' +import { AttributesOnly } from '@shared/core-utils' import { VideoFile } from '@shared/models/videos/video-file.model' import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' import { VideoObject } from '../../../shared/models/activitypub/objects' @@ -489,7 +490,7 @@ export type AvailableForListIDsOptions = { } ] }) -export class VideoModel extends Model { +export class VideoModel extends Model>> { @AllowNull(false) @Default(DataType.UUIDV4) @@ -1617,7 +1618,7 @@ export class VideoModel extends Model { includeLocalVideos: true } - const { query, replacements } = buildListQuery(VideoModel, queryOptions) + const { query, replacements } = buildListQuery(VideoModel.sequelize, queryOptions) return this.sequelize.query(query, { replacements, type: QueryTypes.SELECT }) .then(rows => rows.map(r => r[field])) @@ -1645,7 +1646,7 @@ export class VideoModel extends Model { if (countVideos !== true) return Promise.resolve(undefined) const countOptions = Object.assign({}, options, { isCount: true }) - const { query: queryCount, replacements: replacementsCount } = buildListQuery(VideoModel, countOptions) + const { query: queryCount, replacements: replacementsCount } = buildListQuery(VideoModel.sequelize, countOptions) return VideoModel.sequelize.query(queryCount, { replacements: replacementsCount, type: QueryTypes.SELECT }) .then(rows => rows.length !== 0 ? rows[0].total : 0) @@ -1654,7 +1655,7 @@ export class VideoModel extends Model { function getModels () { if (options.count === 0) return Promise.resolve([]) - const { query, replacements, order } = buildListQuery(VideoModel, options) + const { query, replacements, order } = buildListQuery(VideoModel.sequelize, options) const queryModels = wrapForAPIResults(query, replacements, options, order) return VideoModel.sequelize.query(queryModels, { replacements, type: QueryTypes.SELECT, nest: true }) -- cgit v1.2.3