diff options
Diffstat (limited to 'server/models/video/video-import.ts')
-rw-r--r-- | server/models/video/video-import.ts | 82 |
1 files changed, 75 insertions, 7 deletions
diff --git a/server/models/video/video-import.ts b/server/models/video/video-import.ts index 89eeafd6a..6b8a16b65 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { | 1 | import { |
2 | AfterUpdate, | ||
2 | AllowNull, | 3 | AllowNull, |
3 | BelongsTo, | 4 | BelongsTo, |
4 | Column, | 5 | Column, |
@@ -12,13 +13,14 @@ import { | |||
12 | Table, | 13 | Table, |
13 | UpdatedAt | 14 | UpdatedAt |
14 | } from 'sequelize-typescript' | 15 | } from 'sequelize-typescript' |
15 | import { CONSTRAINTS_FIELDS } from '../../initializers' | 16 | import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers' |
16 | import { throwIfNotValid } from '../utils' | 17 | import { getSort, throwIfNotValid } from '../utils' |
17 | import { VideoModel } from './video' | 18 | import { VideoModel } from './video' |
18 | import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' | 19 | import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' |
19 | import { VideoImport, VideoImportState } from '../../../shared' | 20 | import { VideoImport, VideoImportState } from '../../../shared' |
20 | import { VideoChannelModel } from './video-channel' | 21 | import { VideoChannelModel } from './video-channel' |
21 | import { AccountModel } from '../account/account' | 22 | import { AccountModel } from '../account/account' |
23 | import { TagModel } from './tag' | ||
22 | 24 | ||
23 | @DefaultScope({ | 25 | @DefaultScope({ |
24 | include: [ | 26 | include: [ |
@@ -35,6 +37,10 @@ import { AccountModel } from '../account/account' | |||
35 | required: true | 37 | required: true |
36 | } | 38 | } |
37 | ] | 39 | ] |
40 | }, | ||
41 | { | ||
42 | model: () => TagModel, | ||
43 | required: false | ||
38 | } | 44 | } |
39 | ] | 45 | ] |
40 | } | 46 | } |
@@ -79,27 +85,89 @@ export class VideoImportModel extends Model<VideoImportModel> { | |||
79 | 85 | ||
80 | @BelongsTo(() => VideoModel, { | 86 | @BelongsTo(() => VideoModel, { |
81 | foreignKey: { | 87 | foreignKey: { |
82 | allowNull: false | 88 | allowNull: true |
83 | }, | 89 | }, |
84 | onDelete: 'CASCADE' | 90 | onDelete: 'set null' |
85 | }) | 91 | }) |
86 | Video: VideoModel | 92 | Video: VideoModel |
87 | 93 | ||
94 | @AfterUpdate | ||
95 | static deleteVideoIfFailed (instance: VideoImportModel, options) { | ||
96 | if (instance.state === VideoImportState.FAILED) { | ||
97 | return instance.Video.destroy({ transaction: options.transaction }) | ||
98 | } | ||
99 | |||
100 | return undefined | ||
101 | } | ||
102 | |||
88 | static loadAndPopulateVideo (id: number) { | 103 | static loadAndPopulateVideo (id: number) { |
89 | return VideoImportModel.findById(id) | 104 | return VideoImportModel.findById(id) |
90 | } | 105 | } |
91 | 106 | ||
107 | static listUserVideoImportsForApi (accountId: number, start: number, count: number, sort: string) { | ||
108 | const query = { | ||
109 | offset: start, | ||
110 | limit: count, | ||
111 | order: getSort(sort), | ||
112 | include: [ | ||
113 | { | ||
114 | model: VideoModel, | ||
115 | required: true, | ||
116 | include: [ | ||
117 | { | ||
118 | model: VideoChannelModel, | ||
119 | required: true, | ||
120 | include: [ | ||
121 | { | ||
122 | model: AccountModel, | ||
123 | required: true, | ||
124 | where: { | ||
125 | id: accountId | ||
126 | } | ||
127 | } | ||
128 | ] | ||
129 | }, | ||
130 | { | ||
131 | model: TagModel, | ||
132 | required: false | ||
133 | } | ||
134 | ] | ||
135 | } | ||
136 | ] | ||
137 | } | ||
138 | |||
139 | return VideoImportModel.unscoped() | ||
140 | .findAndCountAll(query) | ||
141 | .then(({ rows, count }) => { | ||
142 | return { | ||
143 | data: rows, | ||
144 | total: count | ||
145 | } | ||
146 | }) | ||
147 | } | ||
148 | |||
92 | toFormattedJSON (): VideoImport { | 149 | toFormattedJSON (): VideoImport { |
93 | const videoFormatOptions = { | 150 | const videoFormatOptions = { |
94 | additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } | 151 | additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } |
95 | } | 152 | } |
96 | const video = Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { | 153 | const video = this.Video |
97 | tags: this.Video.Tags.map(t => t.name) | 154 | ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { |
98 | }) | 155 | tags: this.Video.Tags.map(t => t.name) |
156 | }) | ||
157 | : undefined | ||
99 | 158 | ||
100 | return { | 159 | return { |
101 | targetUrl: this.targetUrl, | 160 | targetUrl: this.targetUrl, |
161 | state: { | ||
162 | id: this.state, | ||
163 | label: VideoImportModel.getStateLabel(this.state) | ||
164 | }, | ||
165 | updatedAt: this.updatedAt.toISOString(), | ||
166 | createdAt: this.createdAt.toISOString(), | ||
102 | video | 167 | video |
103 | } | 168 | } |
104 | } | 169 | } |
170 | private static getStateLabel (id: number) { | ||
171 | return VIDEO_IMPORT_STATES[id] || 'Unknown' | ||
172 | } | ||
105 | } | 173 | } |