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