]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
06a05d5f C |
2 | AllowNull, |
3 | BeforeDestroy, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | Default, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
6dd9de95 | 11 | HasMany, |
06a05d5f C |
12 | Is, |
13 | Model, | |
14 | Scopes, | |
f37dc0dd | 15 | Sequelize, |
06a05d5f C |
16 | Table, |
17 | UpdatedAt | |
3fd3ab2d | 18 | } from 'sequelize-typescript' |
50d6de9c | 19 | import { ActivityPubActor } from '../../../shared/models/activitypub' |
418d092a | 20 | import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' |
2422c46b | 21 | import { |
06a05d5f C |
22 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | |
2422c46b C |
24 | isVideoChannelSupportValid |
25 | } from '../../helpers/custom-validators/video-channels' | |
c5a893d5 | 26 | import { sendDeleteActor } from '../../lib/activitypub/send' |
bfbd9128 | 27 | import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account' |
f37dc0dd | 28 | import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' |
418d092a | 29 | import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils' |
3fd3ab2d | 30 | import { VideoModel } from './video' |
74dc3bca | 31 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
8a19bee1 | 32 | import { ServerModel } from '../server/server' |
1735c825 | 33 | import { FindOptions, ModelIndexesOptions, Op } from 'sequelize' |
418d092a C |
34 | import { AvatarModel } from '../avatar/avatar' |
35 | import { VideoPlaylistModel } from './video-playlist' | |
f37dc0dd C |
36 | |
37 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation | |
1735c825 | 38 | const indexes: ModelIndexesOptions[] = [ |
f37dc0dd C |
39 | buildTrigramSearchIndex('video_channel_name_trigram', 'name'), |
40 | ||
41 | { | |
42 | fields: [ 'accountId' ] | |
43 | }, | |
44 | { | |
45 | fields: [ 'actorId' ] | |
46 | } | |
47 | ] | |
3fd3ab2d | 48 | |
418d092a | 49 | export enum ScopeNames { |
f37dc0dd | 50 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', |
d48ff09d | 51 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
50d6de9c | 52 | WITH_ACTOR = 'WITH_ACTOR', |
418d092a C |
53 | WITH_VIDEOS = 'WITH_VIDEOS', |
54 | SUMMARY = 'SUMMARY' | |
d48ff09d C |
55 | } |
56 | ||
f37dc0dd C |
57 | type AvailableForListOptions = { |
58 | actorId: number | |
59 | } | |
60 | ||
bfbd9128 C |
61 | export type SummaryOptions = { |
62 | withAccount?: boolean // Default: false | |
63 | withAccountBlockerIds?: number[] | |
64 | } | |
65 | ||
3acc5084 | 66 | @DefaultScope(() => ({ |
50d6de9c C |
67 | include: [ |
68 | { | |
3acc5084 | 69 | model: ActorModel, |
50d6de9c C |
70 | required: true |
71 | } | |
72 | ] | |
3acc5084 C |
73 | })) |
74 | @Scopes(() => ({ | |
bfbd9128 | 75 | [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { |
1735c825 | 76 | const base: FindOptions = { |
df0b219d | 77 | attributes: [ 'name', 'description', 'id', 'actorId' ], |
418d092a C |
78 | include: [ |
79 | { | |
57cfff78 | 80 | attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ], |
418d092a C |
81 | model: ActorModel.unscoped(), |
82 | required: true, | |
83 | include: [ | |
84 | { | |
85 | attributes: [ 'host' ], | |
86 | model: ServerModel.unscoped(), | |
87 | required: false | |
88 | }, | |
89 | { | |
90 | model: AvatarModel.unscoped(), | |
91 | required: false | |
92 | } | |
93 | ] | |
94 | } | |
95 | ] | |
96 | } | |
97 | ||
bfbd9128 | 98 | if (options.withAccount === true) { |
418d092a | 99 | base.include.push({ |
bfbd9128 C |
100 | model: AccountModel.scope({ |
101 | method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ] | |
102 | }), | |
418d092a C |
103 | required: true |
104 | }) | |
105 | } | |
f37dc0dd | 106 | |
418d092a C |
107 | return base |
108 | }, | |
109 | [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { | |
f37dc0dd | 110 | // Only list local channels OR channels that are on an instance followed by actorId |
418d092a | 111 | const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId) |
f37dc0dd C |
112 | |
113 | return { | |
114 | include: [ | |
115 | { | |
116 | attributes: { | |
117 | exclude: unusedActorAttributesForAPI | |
118 | }, | |
119 | model: ActorModel, | |
120 | where: { | |
1735c825 | 121 | [Op.or]: [ |
c305467c | 122 | { |
f37dc0dd C |
123 | serverId: null |
124 | }, | |
125 | { | |
126 | serverId: { | |
1735c825 | 127 | [ Op.in ]: Sequelize.literal(inQueryInstanceFollow) |
f37dc0dd | 128 | } |
c305467c C |
129 | } |
130 | ] | |
50d6de9c | 131 | } |
f37dc0dd C |
132 | }, |
133 | { | |
134 | model: AccountModel, | |
135 | required: true, | |
136 | include: [ | |
137 | { | |
138 | attributes: { | |
139 | exclude: unusedActorAttributesForAPI | |
140 | }, | |
141 | model: ActorModel, // Default scope includes avatar and server | |
142 | required: true | |
143 | } | |
144 | ] | |
145 | } | |
146 | ] | |
147 | } | |
148 | }, | |
149 | [ScopeNames.WITH_ACCOUNT]: { | |
150 | include: [ | |
151 | { | |
3acc5084 | 152 | model: AccountModel, |
f37dc0dd | 153 | required: true |
d48ff09d C |
154 | } |
155 | ] | |
156 | }, | |
157 | [ScopeNames.WITH_VIDEOS]: { | |
158 | include: [ | |
3acc5084 | 159 | VideoModel |
d48ff09d | 160 | ] |
50d6de9c C |
161 | }, |
162 | [ScopeNames.WITH_ACTOR]: { | |
163 | include: [ | |
3acc5084 | 164 | ActorModel |
50d6de9c | 165 | ] |
d48ff09d | 166 | } |
3acc5084 | 167 | })) |
3fd3ab2d C |
168 | @Table({ |
169 | tableName: 'videoChannel', | |
f37dc0dd | 170 | indexes |
3fd3ab2d C |
171 | }) |
172 | export class VideoChannelModel extends Model<VideoChannelModel> { | |
72c7248b | 173 | |
3fd3ab2d C |
174 | @AllowNull(false) |
175 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
176 | @Column | |
177 | name: string | |
72c7248b | 178 | |
3fd3ab2d | 179 | @AllowNull(true) |
2422c46b | 180 | @Default(null) |
1735c825 | 181 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true)) |
a10fc78b | 182 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) |
3fd3ab2d | 183 | description: string |
72c7248b | 184 | |
2422c46b C |
185 | @AllowNull(true) |
186 | @Default(null) | |
1735c825 | 187 | @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true)) |
a10fc78b | 188 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) |
2422c46b C |
189 | support: string |
190 | ||
3fd3ab2d C |
191 | @CreatedAt |
192 | createdAt: Date | |
72c7248b | 193 | |
3fd3ab2d C |
194 | @UpdatedAt |
195 | updatedAt: Date | |
4e50b6a1 | 196 | |
fadf619a C |
197 | @ForeignKey(() => ActorModel) |
198 | @Column | |
199 | actorId: number | |
200 | ||
201 | @BelongsTo(() => ActorModel, { | |
202 | foreignKey: { | |
203 | allowNull: false | |
204 | }, | |
205 | onDelete: 'cascade' | |
206 | }) | |
207 | Actor: ActorModel | |
208 | ||
3fd3ab2d C |
209 | @ForeignKey(() => AccountModel) |
210 | @Column | |
211 | accountId: number | |
4e50b6a1 | 212 | |
3fd3ab2d C |
213 | @BelongsTo(() => AccountModel, { |
214 | foreignKey: { | |
215 | allowNull: false | |
216 | }, | |
6b738c7a | 217 | hooks: true |
3fd3ab2d C |
218 | }) |
219 | Account: AccountModel | |
72c7248b | 220 | |
3fd3ab2d | 221 | @HasMany(() => VideoModel, { |
72c7248b | 222 | foreignKey: { |
3fd3ab2d | 223 | name: 'channelId', |
72c7248b C |
224 | allowNull: false |
225 | }, | |
f05a1c30 C |
226 | onDelete: 'CASCADE', |
227 | hooks: true | |
72c7248b | 228 | }) |
3fd3ab2d | 229 | Videos: VideoModel[] |
72c7248b | 230 | |
418d092a C |
231 | @HasMany(() => VideoPlaylistModel, { |
232 | foreignKey: { | |
07b1a18a | 233 | allowNull: true |
418d092a | 234 | }, |
df0b219d | 235 | onDelete: 'CASCADE', |
418d092a C |
236 | hooks: true |
237 | }) | |
238 | VideoPlaylists: VideoPlaylistModel[] | |
239 | ||
f05a1c30 C |
240 | @BeforeDestroy |
241 | static async sendDeleteIfOwned (instance: VideoChannelModel, options) { | |
242 | if (!instance.Actor) { | |
243 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel | |
244 | } | |
245 | ||
c5a893d5 | 246 | if (instance.Actor.isOwned()) { |
c5a893d5 C |
247 | return sendDeleteActor(instance.Actor, options.transaction) |
248 | } | |
249 | ||
250 | return undefined | |
3fd3ab2d | 251 | } |
72c7248b | 252 | |
3fd3ab2d C |
253 | static countByAccount (accountId: number) { |
254 | const query = { | |
255 | where: { | |
256 | accountId | |
257 | } | |
72c7248b | 258 | } |
3fd3ab2d C |
259 | |
260 | return VideoChannelModel.count(query) | |
72c7248b C |
261 | } |
262 | ||
f37dc0dd | 263 | static listForApi (actorId: number, start: number, count: number, sort: string) { |
3fd3ab2d C |
264 | const query = { |
265 | offset: start, | |
266 | limit: count, | |
3bb6c526 | 267 | order: getSort(sort) |
3fd3ab2d | 268 | } |
72c7248b | 269 | |
f37dc0dd C |
270 | const scopes = { |
271 | method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ] | |
272 | } | |
50d6de9c | 273 | return VideoChannelModel |
f37dc0dd C |
274 | .scope(scopes) |
275 | .findAndCountAll(query) | |
276 | .then(({ rows, count }) => { | |
277 | return { total: count, data: rows } | |
278 | }) | |
279 | } | |
280 | ||
2feebf3e C |
281 | static listLocalsForSitemap (sort: string) { |
282 | const query = { | |
283 | attributes: [ ], | |
284 | offset: 0, | |
285 | order: getSort(sort), | |
286 | include: [ | |
287 | { | |
288 | attributes: [ 'preferredUsername', 'serverId' ], | |
289 | model: ActorModel.unscoped(), | |
290 | where: { | |
291 | serverId: null | |
292 | } | |
293 | } | |
294 | ] | |
295 | } | |
296 | ||
297 | return VideoChannelModel | |
298 | .unscoped() | |
299 | .findAll(query) | |
300 | } | |
301 | ||
f37dc0dd C |
302 | static searchForApi (options: { |
303 | actorId: number | |
304 | search: string | |
305 | start: number | |
306 | count: number | |
307 | sort: string | |
308 | }) { | |
309 | const attributesInclude = [] | |
310 | const escapedSearch = VideoModel.sequelize.escape(options.search) | |
311 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
312 | attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search)) | |
313 | ||
314 | const query = { | |
315 | attributes: { | |
316 | include: attributesInclude | |
317 | }, | |
318 | offset: options.start, | |
319 | limit: options.count, | |
320 | order: getSort(options.sort), | |
321 | where: { | |
1735c825 | 322 | [Op.or]: [ |
c3c2ab1c C |
323 | Sequelize.literal( |
324 | 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' | |
325 | ), | |
326 | Sequelize.literal( | |
327 | 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' | |
f37dc0dd | 328 | ) |
c3c2ab1c | 329 | ] |
f37dc0dd C |
330 | } |
331 | } | |
332 | ||
333 | const scopes = { | |
334 | method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ] | |
335 | } | |
336 | return VideoChannelModel | |
337 | .scope(scopes) | |
50d6de9c | 338 | .findAndCountAll(query) |
3fd3ab2d C |
339 | .then(({ rows, count }) => { |
340 | return { total: count, data: rows } | |
341 | }) | |
72c7248b C |
342 | } |
343 | ||
91b66319 C |
344 | static listByAccount (options: { |
345 | accountId: number, | |
346 | start: number, | |
347 | count: number, | |
348 | sort: string | |
349 | }) { | |
3fd3ab2d | 350 | const query = { |
91b66319 C |
351 | offset: options.start, |
352 | limit: options.count, | |
353 | order: getSort(options.sort), | |
3fd3ab2d C |
354 | include: [ |
355 | { | |
356 | model: AccountModel, | |
357 | where: { | |
91b66319 | 358 | id: options.accountId |
3fd3ab2d | 359 | }, |
50d6de9c | 360 | required: true |
3fd3ab2d C |
361 | } |
362 | ] | |
363 | } | |
72c7248b | 364 | |
50d6de9c C |
365 | return VideoChannelModel |
366 | .findAndCountAll(query) | |
3fd3ab2d C |
367 | .then(({ rows, count }) => { |
368 | return { total: count, data: rows } | |
369 | }) | |
72c7248b C |
370 | } |
371 | ||
5cf84858 C |
372 | static loadByIdAndPopulateAccount (id: number) { |
373 | return VideoChannelModel.unscoped() | |
374 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
9b39106d | 375 | .findByPk(id) |
5cf84858 C |
376 | } |
377 | ||
3fd3ab2d | 378 | static loadByIdAndAccount (id: number, accountId: number) { |
8a19bee1 | 379 | const query = { |
3fd3ab2d C |
380 | where: { |
381 | id, | |
382 | accountId | |
d48ff09d | 383 | } |
571389d4 | 384 | } |
3fd3ab2d | 385 | |
5cf84858 | 386 | return VideoChannelModel.unscoped() |
50d6de9c | 387 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
8a19bee1 | 388 | .findOne(query) |
0d0e8dd0 C |
389 | } |
390 | ||
3fd3ab2d | 391 | static loadAndPopulateAccount (id: number) { |
5cf84858 | 392 | return VideoChannelModel.unscoped() |
50d6de9c | 393 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
9b39106d | 394 | .findByPk(id) |
3fd3ab2d | 395 | } |
0d0e8dd0 | 396 | |
f37dc0dd C |
397 | static loadByUrlAndPopulateAccount (url: string) { |
398 | const query = { | |
399 | include: [ | |
400 | { | |
401 | model: ActorModel, | |
402 | required: true, | |
403 | where: { | |
404 | url | |
405 | } | |
406 | } | |
407 | ] | |
408 | } | |
409 | ||
410 | return VideoChannelModel | |
411 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
8a19bee1 | 412 | .findOne(query) |
72c7248b C |
413 | } |
414 | ||
92bf2f62 C |
415 | static loadByNameWithHostAndPopulateAccount (nameWithHost: string) { |
416 | const [ name, host ] = nameWithHost.split('@') | |
417 | ||
6dd9de95 | 418 | if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name) |
92bf2f62 C |
419 | |
420 | return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) | |
421 | } | |
422 | ||
8a19bee1 C |
423 | static loadLocalByNameAndPopulateAccount (name: string) { |
424 | const query = { | |
3fd3ab2d | 425 | include: [ |
8a19bee1 C |
426 | { |
427 | model: ActorModel, | |
428 | required: true, | |
429 | where: { | |
430 | preferredUsername: name, | |
431 | serverId: null | |
432 | } | |
433 | } | |
3fd3ab2d C |
434 | ] |
435 | } | |
72c7248b | 436 | |
5cf84858 | 437 | return VideoChannelModel.unscoped() |
8a19bee1 C |
438 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
439 | .findOne(query) | |
72c7248b C |
440 | } |
441 | ||
8a19bee1 | 442 | static loadByNameAndHostAndPopulateAccount (name: string, host: string) { |
06a05d5f C |
443 | const query = { |
444 | include: [ | |
445 | { | |
446 | model: ActorModel, | |
447 | required: true, | |
448 | where: { | |
8a19bee1 C |
449 | preferredUsername: name |
450 | }, | |
451 | include: [ | |
452 | { | |
453 | model: ServerModel, | |
454 | required: true, | |
455 | where: { host } | |
456 | } | |
457 | ] | |
06a05d5f C |
458 | } |
459 | ] | |
460 | } | |
461 | ||
5cf84858 | 462 | return VideoChannelModel.unscoped() |
8a19bee1 C |
463 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
464 | .findOne(query) | |
465 | } | |
466 | ||
467 | static loadAndPopulateAccountAndVideos (id: number) { | |
468 | const options = { | |
469 | include: [ | |
470 | VideoModel | |
471 | ] | |
472 | } | |
473 | ||
5cf84858 | 474 | return VideoChannelModel.unscoped() |
8a19bee1 | 475 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) |
9b39106d | 476 | .findByPk(id, options) |
06a05d5f C |
477 | } |
478 | ||
2422c46b | 479 | toFormattedJSON (): VideoChannel { |
50d6de9c | 480 | const actor = this.Actor.toFormattedJSON() |
6b738c7a | 481 | const videoChannel = { |
3fd3ab2d | 482 | id: this.id, |
749c7247 | 483 | displayName: this.getDisplayName(), |
3fd3ab2d | 484 | description: this.description, |
2422c46b | 485 | support: this.support, |
50d6de9c | 486 | isLocal: this.Actor.isOwned(), |
3fd3ab2d | 487 | createdAt: this.createdAt, |
6b738c7a | 488 | updatedAt: this.updatedAt, |
06a05d5f | 489 | ownerAccount: undefined |
6b738c7a C |
490 | } |
491 | ||
a4f99a76 | 492 | if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() |
72c7248b | 493 | |
6b738c7a | 494 | return Object.assign(actor, videoChannel) |
72c7248b C |
495 | } |
496 | ||
418d092a C |
497 | toFormattedSummaryJSON (): VideoChannelSummary { |
498 | const actor = this.Actor.toFormattedJSON() | |
499 | ||
500 | return { | |
501 | id: this.id, | |
418d092a C |
502 | name: actor.name, |
503 | displayName: this.getDisplayName(), | |
504 | url: actor.url, | |
505 | host: actor.host, | |
506 | avatar: actor.avatar | |
507 | } | |
508 | } | |
509 | ||
50d6de9c C |
510 | toActivityPubObject (): ActivityPubActor { |
511 | const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel') | |
512 | ||
513 | return Object.assign(obj, { | |
514 | summary: this.description, | |
2422c46b | 515 | support: this.support, |
50d6de9c C |
516 | attributedTo: [ |
517 | { | |
518 | type: 'Person' as 'Person', | |
519 | id: this.Account.Actor.url | |
520 | } | |
521 | ] | |
522 | }) | |
72c7248b | 523 | } |
749c7247 C |
524 | |
525 | getDisplayName () { | |
526 | return this.name | |
527 | } | |
744d0eca C |
528 | |
529 | isOutdated () { | |
530 | return this.Actor.isOutdated() | |
531 | } | |
72c7248b | 532 | } |