]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-playlist-element.ts
Fix multiple server tests
[github/Chocobozzz/PeerTube.git] / server / models / video / video-playlist-element.ts
1 import {
2 AllowNull,
3 BelongsTo,
4 Column,
5 CreatedAt,
6 DataType,
7 Default,
8 ForeignKey,
9 Is,
10 IsInt,
11 Min,
12 Model,
13 Table,
14 UpdatedAt
15 } from 'sequelize-typescript'
16 import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
17 import { VideoPlaylistModel } from './video-playlist'
18 import { getSort, throwIfNotValid } from '../utils'
19 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
20 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
21 import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
22 import * as validator from 'validator'
23 import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
24 import { UserModel } from '../account/user'
25 import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
26 import { AccountModel } from '../account/account'
27 import { VideoPrivacy } from '../../../shared/models/videos'
28
29 @Table({
30 tableName: 'videoPlaylistElement',
31 indexes: [
32 {
33 fields: [ 'videoPlaylistId' ]
34 },
35 {
36 fields: [ 'videoId' ]
37 },
38 {
39 fields: [ 'videoPlaylistId', 'videoId' ],
40 unique: true
41 },
42 {
43 fields: [ 'url' ],
44 unique: true
45 }
46 ]
47 })
48 export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
49 @CreatedAt
50 createdAt: Date
51
52 @UpdatedAt
53 updatedAt: Date
54
55 @AllowNull(false)
56 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
57 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
58 url: string
59
60 @AllowNull(false)
61 @Default(1)
62 @IsInt
63 @Min(1)
64 @Column
65 position: number
66
67 @AllowNull(true)
68 @IsInt
69 @Min(0)
70 @Column
71 startTimestamp: number
72
73 @AllowNull(true)
74 @IsInt
75 @Min(0)
76 @Column
77 stopTimestamp: number
78
79 @ForeignKey(() => VideoPlaylistModel)
80 @Column
81 videoPlaylistId: number
82
83 @BelongsTo(() => VideoPlaylistModel, {
84 foreignKey: {
85 allowNull: false
86 },
87 onDelete: 'CASCADE'
88 })
89 VideoPlaylist: VideoPlaylistModel
90
91 @ForeignKey(() => VideoModel)
92 @Column
93 videoId: number
94
95 @BelongsTo(() => VideoModel, {
96 foreignKey: {
97 allowNull: true
98 },
99 onDelete: 'set null'
100 })
101 Video: VideoModel
102
103 static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
104 const query = {
105 where: {
106 videoPlaylistId
107 },
108 transaction
109 }
110
111 return VideoPlaylistElementModel.destroy(query)
112 }
113
114 static listForApi (options: {
115 start: number,
116 count: number,
117 videoPlaylistId: number,
118 serverAccount: AccountModel,
119 user?: UserModel
120 }) {
121 const accountIds = [ options.serverAccount.id ]
122 const videoScope: (ScopeOptions | string)[] = [
123 VideoScopeNames.WITH_BLACKLISTED
124 ]
125
126 if (options.user) {
127 accountIds.push(options.user.Account.id)
128 videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
129 }
130
131 const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
132 videoScope.push({
133 method: [
134 VideoScopeNames.FOR_API, forApiOptions
135 ]
136 })
137
138 const findQuery = {
139 offset: options.start,
140 limit: options.count,
141 order: getSort('position'),
142 where: {
143 videoPlaylistId: options.videoPlaylistId
144 },
145 include: [
146 {
147 model: VideoModel.scope(videoScope),
148 required: false
149 }
150 ]
151 }
152
153 const countQuery = {
154 where: {
155 videoPlaylistId: options.videoPlaylistId
156 }
157 }
158
159 return Promise.all([
160 VideoPlaylistElementModel.count(countQuery),
161 VideoPlaylistElementModel.findAll(findQuery)
162 ]).then(([ total, data ]) => ({ total, data }))
163 }
164
165 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) {
166 const query = {
167 where: {
168 videoPlaylistId,
169 videoId
170 }
171 }
172
173 return VideoPlaylistElementModel.findOne(query)
174 }
175
176 static loadById (playlistElementId: number) {
177 return VideoPlaylistElementModel.findByPk(playlistElementId)
178 }
179
180 static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) {
181 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
182 const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
183
184 const query = {
185 include: [
186 {
187 attributes: [ 'privacy' ],
188 model: VideoPlaylistModel.unscoped(),
189 where: playlistWhere
190 },
191 {
192 attributes: [ 'url' ],
193 model: VideoModel.unscoped(),
194 where: videoWhere
195 }
196 ]
197 }
198
199 return VideoPlaylistElementModel.findOne(query)
200 }
201
202 static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
203 const query = {
204 attributes: [ 'url' ],
205 offset: start,
206 limit: count,
207 order: getSort('position'),
208 where: {
209 videoPlaylistId
210 },
211 transaction: t
212 }
213
214 return VideoPlaylistElementModel
215 .findAndCountAll(query)
216 .then(({ rows, count }) => {
217 return { total: count, data: rows.map(e => e.url) }
218 })
219 }
220
221 static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
222 const query: AggregateOptions<number> = {
223 where: {
224 videoPlaylistId
225 },
226 transaction
227 }
228
229 return VideoPlaylistElementModel.max('position', query)
230 .then(position => position ? position + 1 : 1)
231 }
232
233 static reassignPositionOf (
234 videoPlaylistId: number,
235 firstPosition: number,
236 endPosition: number,
237 newPosition: number,
238 transaction?: Transaction
239 ) {
240 const query = {
241 where: {
242 videoPlaylistId,
243 position: {
244 [Op.gte]: firstPosition,
245 [Op.lte]: endPosition
246 }
247 },
248 transaction,
249 validate: false // We use a literal to update the position
250 }
251
252 return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
253 }
254
255 static increasePositionOf (
256 videoPlaylistId: number,
257 fromPosition: number,
258 toPosition?: number,
259 by = 1,
260 transaction?: Transaction
261 ) {
262 const query = {
263 where: {
264 videoPlaylistId,
265 position: {
266 [Op.gte]: fromPosition
267 }
268 },
269 transaction
270 }
271
272 return VideoPlaylistElementModel.increment({ position: by }, query)
273 }
274
275 getType (displayNSFW?: boolean, accountId?: number) {
276 const video = this.Video
277
278 if (!video) return VideoPlaylistElementType.DELETED
279
280 // Owned video, don't filter it
281 if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
282
283 if (video.privacy === VideoPrivacy.PRIVATE) return VideoPlaylistElementType.PRIVATE
284
285 if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
286 if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
287
288 return VideoPlaylistElementType.REGULAR
289 }
290
291 getVideoElement (displayNSFW?: boolean, accountId?: number) {
292 if (!this.Video) return null
293 if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
294
295 return this.Video.toFormattedJSON()
296 }
297
298 toFormattedJSON (options: { displayNSFW?: boolean, accountId?: number } = {}): VideoPlaylistElement {
299 return {
300 id: this.id,
301 position: this.position,
302 startTimestamp: this.startTimestamp,
303 stopTimestamp: this.stopTimestamp,
304
305 type: this.getType(options.displayNSFW, options.accountId),
306
307 video: this.getVideoElement(options.displayNSFW, options.accountId)
308 }
309 }
310
311 toActivityPubObject (): PlaylistElementObject {
312 const base: PlaylistElementObject = {
313 id: this.url,
314 type: 'PlaylistElement',
315
316 url: this.Video.url,
317 position: this.position
318 }
319
320 if (this.startTimestamp) base.startTimestamp = this.startTimestamp
321 if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp
322
323 return base
324 }
325 }