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