15 } from 'sequelize-typescript'
16 import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants'
17 import { getSort, throwIfNotValid } from '../utils'
18 import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
19 import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
20 import { VideoImport, VideoImportState } from '../../../shared'
21 import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
22 import { UserModel } from '../account/user'
23 import * as Bluebird from 'bluebird'
24 import { MVideoImportDefault, MVideoImportFormattable } from '@server/typings/models/video/video-import'
26 @DefaultScope(() => ({
29 model: UserModel.unscoped(),
33 model: VideoModel.scope([
34 VideoModelScopeNames.WITH_ACCOUNT_DETAILS,
35 VideoModelScopeNames.WITH_TAGS,
36 VideoModelScopeNames.WITH_THUMBNAILS
44 tableName: 'videoImport',
47 fields: [ 'videoId' ],
55 export class VideoImportModel extends Model<VideoImportModel> {
64 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl', true))
65 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
70 @Is('VideoImportMagnetUri', value => throwIfNotValid(value, isVideoMagnetUriValid, 'magnetUri', true))
71 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) // Use the same constraints than URLs
76 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max))
81 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
83 state: VideoImportState
87 @Column(DataType.TEXT)
90 @ForeignKey(() => UserModel)
94 @BelongsTo(() => UserModel, {
102 @ForeignKey(() => VideoModel)
106 @BelongsTo(() => VideoModel, {
115 static deleteVideoIfFailed (instance: VideoImportModel, options) {
116 if (instance.state === VideoImportState.FAILED) {
117 return instance.Video.destroy({ transaction: options.transaction })
123 static loadAndPopulateVideo (id: number): Bluebird<MVideoImportDefault> {
124 return VideoImportModel.findByPk(id)
127 static listUserVideoImportsForApi (userId: number, start: number, count: number, sort: string) {
132 model: UserModel.unscoped(), // FIXME: Without this, sequelize try to COUNT(DISTINCT(*)) which is an invalid SQL query
138 order: getSort(sort),
144 return VideoImportModel.findAndCountAll<MVideoImportDefault>(query)
145 .then(({ rows, count }) => {
153 getTargetIdentifier () {
154 return this.targetUrl || this.magnetUri || this.torrentName
157 toFormattedJSON (this: MVideoImportFormattable): VideoImport {
158 const videoFormatOptions = {
159 completeDescription: true,
160 additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
162 const video = this.Video
163 ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { tags: this.Video.Tags.map(t => t.name) })
169 targetUrl: this.targetUrl,
170 magnetUri: this.magnetUri,
171 torrentName: this.torrentName,
175 label: VideoImportModel.getStateLabel(this.state)
178 updatedAt: this.updatedAt.toISOString(),
179 createdAt: this.createdAt.toISOString(),
184 private static getStateLabel (id: number) {
185 return VIDEO_IMPORT_STATES[id] || 'Unknown'