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