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