15 } from 'sequelize-typescript'
16 import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import'
17 import { VideoImport, VideoImportState } from '../../../shared'
18 import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
19 import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
20 import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants'
21 import { UserModel } from '../account/user'
22 import { getSort, throwIfNotValid } from '../utils'
23 import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
25 @DefaultScope(() => ({
28 model: UserModel.unscoped(),
32 model: VideoModel.scope([
33 VideoModelScopeNames.WITH_ACCOUNT_DETAILS,
34 VideoModelScopeNames.WITH_TAGS,
35 VideoModelScopeNames.WITH_THUMBNAILS
43 tableName: 'videoImport',
46 fields: [ 'videoId' ],
54 export class VideoImportModel extends Model {
63 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl', true))
64 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
69 @Is('VideoImportMagnetUri', value => throwIfNotValid(value, isVideoMagnetUriValid, 'magnetUri', true))
70 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) // Use the same constraints than URLs
75 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max))
80 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
82 state: VideoImportState
86 @Column(DataType.TEXT)
89 @ForeignKey(() => UserModel)
93 @BelongsTo(() => UserModel, {
101 @ForeignKey(() => VideoModel)
105 @BelongsTo(() => VideoModel, {
114 static deleteVideoIfFailed (instance: VideoImportModel, options) {
115 if (instance.state === VideoImportState.FAILED) {
116 return instance.Video.destroy({ transaction: options.transaction })
122 static loadAndPopulateVideo (id: number): Promise<MVideoImportDefault> {
123 return VideoImportModel.findByPk(id)
126 static listUserVideoImportsForApi (userId: number, start: number, count: number, sort: string) {
131 attributes: [ 'id' ],
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'