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