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,
50 tableName: 'videoImport',
53 fields: [ 'videoId' ],
58 export class VideoImportModel extends Model<VideoImportModel> {
66 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl'))
67 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
72 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
74 state: VideoImportState
78 @Column(DataType.TEXT)
81 @ForeignKey(() => VideoModel)
85 @BelongsTo(() => VideoModel, {
94 static deleteVideoIfFailed (instance: VideoImportModel, options) {
95 if (instance.state === VideoImportState.FAILED) {
96 return instance.Video.destroy({ transaction: options.transaction })
102 static loadAndPopulateVideo (id: number) {
103 return VideoImportModel.findById(id)
106 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)
161 targetUrl: this.targetUrl,
164 label: VideoImportModel.getStateLabel(this.state)
167 updatedAt: this.updatedAt.toISOString(),
168 createdAt: this.createdAt.toISOString(),
172 private static getStateLabel (id: number) {
173 return VIDEO_IMPORT_STATES[id] || 'Unknown'