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