15 } from 'sequelize-typescript'
16 import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers'
17 import { getSort, throwIfNotValid } from '../utils'
18 import { VideoModel } from './video'
19 import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
20 import { VideoImport, VideoImportState } from '../../../shared'
21 import { VideoChannelModel } from './video-channel'
22 import { AccountModel } from '../account/account'
23 import { TagModel } from './tag'
28 model: () => VideoModel,
32 model: () => VideoChannelModel,
36 model: () => AccountModel,
42 model: () => TagModel,
51 tableName: 'videoImport',
54 fields: [ 'videoId' ],
59 export class VideoImportModel extends Model<VideoImportModel> {
67 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl'))
68 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
73 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
75 state: VideoImportState
79 @Column(DataType.TEXT)
82 @ForeignKey(() => VideoModel)
86 @BelongsTo(() => VideoModel, {
95 static deleteVideoIfFailed (instance: VideoImportModel, options) {
96 if (instance.state === VideoImportState.FAILED) {
97 return instance.Video.destroy({ transaction: options.transaction })
103 static loadAndPopulateVideo (id: number) {
104 return VideoImportModel.findById(id)
107 static listUserVideoImportsForApi (accountId: number, start: number, count: number, sort: string) {
111 order: getSort(sort),
118 model: VideoChannelModel,
139 return VideoImportModel.unscoped()
140 .findAndCountAll(query)
141 .then(({ rows, count }) => {
149 toFormattedJSON (): VideoImport {
150 const videoFormatOptions = {
151 additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
153 const video = this.Video
154 ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), {
155 tags: this.Video.Tags.map(t => t.name)
160 targetUrl: this.targetUrl,
163 label: VideoImportModel.getStateLabel(this.state)
165 updatedAt: this.updatedAt.toISOString(),
166 createdAt: this.createdAt.toISOString(),
170 private static getStateLabel (id: number) {
171 return VIDEO_IMPORT_STATES[id] || 'Unknown'