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