15 } from 'sequelize-typescript'
16 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
17 import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import'
18 import { AttributesOnly } from '@shared/core-utils'
19 import { VideoImport, VideoImportState } from '../../../shared'
20 import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
21 import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
22 import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants'
23 import { UserModel } from '../user/user'
24 import { getSort, throwIfNotValid } from '../utils'
25 import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
27 @DefaultScope(() => ({
30 model: UserModel.unscoped(),
34 model: VideoModel.scope([
35 VideoModelScopeNames.WITH_ACCOUNT_DETAILS,
36 VideoModelScopeNames.WITH_TAGS,
37 VideoModelScopeNames.WITH_THUMBNAILS
45 tableName: 'videoImport',
48 fields: [ 'videoId' ],
56 export class VideoImportModel extends Model<Partial<AttributesOnly<VideoImportModel>>> {
65 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl', true))
66 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
71 @Is('VideoImportMagnetUri', value => throwIfNotValid(value, isVideoMagnetUriValid, 'magnetUri', true))
72 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) // Use the same constraints than URLs
77 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max))
82 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
84 state: VideoImportState
88 @Column(DataType.TEXT)
91 @ForeignKey(() => UserModel)
95 @BelongsTo(() => UserModel, {
103 @ForeignKey(() => VideoModel)
107 @BelongsTo(() => VideoModel, {
116 static deleteVideoIfFailed (instance: VideoImportModel, options) {
117 if (instance.state === VideoImportState.FAILED) {
118 return afterCommitIfTransaction(options.transaction, () => instance.Video.destroy())
124 static loadAndPopulateVideo (id: number): Promise<MVideoImportDefault> {
125 return VideoImportModel.findByPk(id)
128 static listUserVideoImportsForApi (userId: number, start: number, count: number, sort: string) {
133 attributes: [ 'id' ],
134 model: UserModel.unscoped(), // FIXME: Without this, sequelize try to COUNT(DISTINCT(*)) which is an invalid SQL query
140 order: getSort(sort),
146 return VideoImportModel.findAndCountAll<MVideoImportDefault>(query)
147 .then(({ rows, count }) => {
155 getTargetIdentifier () {
156 return this.targetUrl || this.magnetUri || this.torrentName
159 toFormattedJSON (this: MVideoImportFormattable): VideoImport {
160 const videoFormatOptions = {
161 completeDescription: true,
162 additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
164 const video = this.Video
165 ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { tags: this.Video.Tags.map(t => t.name) })
171 targetUrl: this.targetUrl,
172 magnetUri: this.magnetUri,
173 torrentName: this.torrentName,
177 label: VideoImportModel.getStateLabel(this.state)
180 updatedAt: this.updatedAt.toISOString(),
181 createdAt: this.createdAt.toISOString(),
186 private static getStateLabel (id: number) {
187 return VIDEO_IMPORT_STATES[id] || 'Unknown'