]>
Commit | Line | Data |
---|---|---|
1 | import { | |
2 | AfterUpdate, | |
3 | AllowNull, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | Default, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
11 | Is, | |
12 | Model, | |
13 | Table, | |
14 | UpdatedAt | |
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' | |
26 | ||
27 | @DefaultScope(() => ({ | |
28 | include: [ | |
29 | { | |
30 | model: UserModel.unscoped(), | |
31 | required: true | |
32 | }, | |
33 | { | |
34 | model: VideoModel.scope([ | |
35 | VideoModelScopeNames.WITH_ACCOUNT_DETAILS, | |
36 | VideoModelScopeNames.WITH_TAGS, | |
37 | VideoModelScopeNames.WITH_THUMBNAILS | |
38 | ]), | |
39 | required: false | |
40 | } | |
41 | ] | |
42 | })) | |
43 | ||
44 | @Table({ | |
45 | tableName: 'videoImport', | |
46 | indexes: [ | |
47 | { | |
48 | fields: [ 'videoId' ], | |
49 | unique: true | |
50 | }, | |
51 | { | |
52 | fields: [ 'userId' ] | |
53 | } | |
54 | ] | |
55 | }) | |
56 | export class VideoImportModel extends Model<Partial<AttributesOnly<VideoImportModel>>> { | |
57 | @CreatedAt | |
58 | createdAt: Date | |
59 | ||
60 | @UpdatedAt | |
61 | updatedAt: Date | |
62 | ||
63 | @AllowNull(true) | |
64 | @Default(null) | |
65 | @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl', true)) | |
66 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) | |
67 | targetUrl: string | |
68 | ||
69 | @AllowNull(true) | |
70 | @Default(null) | |
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 | |
73 | magnetUri: string | |
74 | ||
75 | @AllowNull(true) | |
76 | @Default(null) | |
77 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max)) | |
78 | torrentName: string | |
79 | ||
80 | @AllowNull(false) | |
81 | @Default(null) | |
82 | @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state')) | |
83 | @Column | |
84 | state: VideoImportState | |
85 | ||
86 | @AllowNull(true) | |
87 | @Default(null) | |
88 | @Column(DataType.TEXT) | |
89 | error: string | |
90 | ||
91 | @ForeignKey(() => UserModel) | |
92 | @Column | |
93 | userId: number | |
94 | ||
95 | @BelongsTo(() => UserModel, { | |
96 | foreignKey: { | |
97 | allowNull: false | |
98 | }, | |
99 | onDelete: 'cascade' | |
100 | }) | |
101 | User: UserModel | |
102 | ||
103 | @ForeignKey(() => VideoModel) | |
104 | @Column | |
105 | videoId: number | |
106 | ||
107 | @BelongsTo(() => VideoModel, { | |
108 | foreignKey: { | |
109 | allowNull: true | |
110 | }, | |
111 | onDelete: 'set null' | |
112 | }) | |
113 | Video: VideoModel | |
114 | ||
115 | @AfterUpdate | |
116 | static deleteVideoIfFailed (instance: VideoImportModel, options) { | |
117 | if (instance.state === VideoImportState.FAILED) { | |
118 | return afterCommitIfTransaction(options.transaction, () => instance.Video.destroy()) | |
119 | } | |
120 | ||
121 | return undefined | |
122 | } | |
123 | ||
124 | static loadAndPopulateVideo (id: number): Promise<MVideoImportDefault> { | |
125 | return VideoImportModel.findByPk(id) | |
126 | } | |
127 | ||
128 | static listUserVideoImportsForApi (userId: number, start: number, count: number, sort: string) { | |
129 | const query = { | |
130 | distinct: true, | |
131 | include: [ | |
132 | { | |
133 | attributes: [ 'id' ], | |
134 | model: UserModel.unscoped(), // FIXME: Without this, sequelize try to COUNT(DISTINCT(*)) which is an invalid SQL query | |
135 | required: true | |
136 | } | |
137 | ], | |
138 | offset: start, | |
139 | limit: count, | |
140 | order: getSort(sort), | |
141 | where: { | |
142 | userId | |
143 | } | |
144 | } | |
145 | ||
146 | return VideoImportModel.findAndCountAll<MVideoImportDefault>(query) | |
147 | .then(({ rows, count }) => { | |
148 | return { | |
149 | data: rows, | |
150 | total: count | |
151 | } | |
152 | }) | |
153 | } | |
154 | ||
155 | getTargetIdentifier () { | |
156 | return this.targetUrl || this.magnetUri || this.torrentName | |
157 | } | |
158 | ||
159 | toFormattedJSON (this: MVideoImportFormattable): VideoImport { | |
160 | const videoFormatOptions = { | |
161 | completeDescription: true, | |
162 | additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } | |
163 | } | |
164 | const video = this.Video | |
165 | ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { tags: this.Video.Tags.map(t => t.name) }) | |
166 | : undefined | |
167 | ||
168 | return { | |
169 | id: this.id, | |
170 | ||
171 | targetUrl: this.targetUrl, | |
172 | magnetUri: this.magnetUri, | |
173 | torrentName: this.torrentName, | |
174 | ||
175 | state: { | |
176 | id: this.state, | |
177 | label: VideoImportModel.getStateLabel(this.state) | |
178 | }, | |
179 | error: this.error, | |
180 | updatedAt: this.updatedAt.toISOString(), | |
181 | createdAt: this.createdAt.toISOString(), | |
182 | video | |
183 | } | |
184 | } | |
185 | ||
186 | private static getStateLabel (id: number) { | |
187 | return VIDEO_IMPORT_STATES[id] || 'Unknown' | |
188 | } | |
189 | } |