]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-channel.ts
Cleanup server fixme
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
CommitLineData
3fd3ab2d 1import {
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 19import { ActivityPubActor } from '../../../shared/models/activitypub'
418d092a 20import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
2422c46b 21import {
06a05d5f
C
22 isVideoChannelDescriptionValid,
23 isVideoChannelNameValid,
2422c46b
C
24 isVideoChannelSupportValid
25} from '../../helpers/custom-validators/video-channels'
c5a893d5 26import { sendDeleteActor } from '../../lib/activitypub/send'
bfbd9128 27import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
f37dc0dd 28import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
418d092a 29import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
3fd3ab2d 30import { VideoModel } from './video'
74dc3bca 31import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
8a19bee1 32import { ServerModel } from '../server/server'
1735c825 33import { FindOptions, ModelIndexesOptions, Op } from 'sequelize'
418d092a
C
34import { AvatarModel } from '../avatar/avatar'
35import { VideoPlaylistModel } from './video-playlist'
453e83ea
C
36import * as Bluebird from 'bluebird'
37import {
38 MChannelAccountDefault,
39 MChannelActor,
b5fecbf4
C
40 MChannelActorAccountDefaultVideos,
41 MChannelAP,
42 MChannelFormattable,
43 MChannelSummaryFormattable
453e83ea 44} from '../../typings/models/video'
f37dc0dd 45
418d092a 46export enum ScopeNames {
453e83ea 47 FOR_API = 'FOR_API',
d48ff09d 48 WITH_ACCOUNT = 'WITH_ACCOUNT',
50d6de9c 49 WITH_ACTOR = 'WITH_ACTOR',
418d092a
C
50 WITH_VIDEOS = 'WITH_VIDEOS',
51 SUMMARY = 'SUMMARY'
d48ff09d
C
52}
53
f37dc0dd
C
54type AvailableForListOptions = {
55 actorId: number
56}
57
bfbd9128
C
58export type SummaryOptions = {
59 withAccount?: boolean // Default: false
60 withAccountBlockerIds?: number[]
61}
62
3acc5084 63@DefaultScope(() => ({
50d6de9c
C
64 include: [
65 {
3acc5084 66 model: ActorModel,
50d6de9c
C
67 required: true
68 }
69 ]
3acc5084
C
70}))
71@Scopes(() => ({
bfbd9128 72 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
1735c825 73 const base: FindOptions = {
453e83ea 74 attributes: [ 'id', 'name', 'description', 'actorId' ],
418d092a
C
75 include: [
76 {
453e83ea 77 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
418d092a
C
78 model: ActorModel.unscoped(),
79 required: true,
80 include: [
81 {
82 attributes: [ 'host' ],
83 model: ServerModel.unscoped(),
84 required: false
85 },
86 {
87 model: AvatarModel.unscoped(),
88 required: false
89 }
90 ]
91 }
92 ]
93 }
94
bfbd9128 95 if (options.withAccount === true) {
418d092a 96 base.include.push({
bfbd9128
C
97 model: AccountModel.scope({
98 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
99 }),
418d092a
C
100 required: true
101 })
102 }
f37dc0dd 103
418d092a
C
104 return base
105 },
453e83ea 106 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
f37dc0dd 107 // Only list local channels OR channels that are on an instance followed by actorId
418d092a 108 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
f37dc0dd
C
109
110 return {
111 include: [
112 {
113 attributes: {
114 exclude: unusedActorAttributesForAPI
115 },
116 model: ActorModel,
117 where: {
1735c825 118 [Op.or]: [
c305467c 119 {
f37dc0dd
C
120 serverId: null
121 },
122 {
123 serverId: {
1735c825 124 [ Op.in ]: Sequelize.literal(inQueryInstanceFollow)
f37dc0dd 125 }
c305467c
C
126 }
127 ]
50d6de9c 128 }
f37dc0dd
C
129 },
130 {
131 model: AccountModel,
132 required: true,
133 include: [
134 {
135 attributes: {
136 exclude: unusedActorAttributesForAPI
137 },
138 model: ActorModel, // Default scope includes avatar and server
139 required: true
140 }
141 ]
142 }
143 ]
144 }
145 },
146 [ScopeNames.WITH_ACCOUNT]: {
147 include: [
148 {
3acc5084 149 model: AccountModel,
f37dc0dd 150 required: true
d48ff09d
C
151 }
152 ]
153 },
154 [ScopeNames.WITH_VIDEOS]: {
155 include: [
3acc5084 156 VideoModel
d48ff09d 157 ]
50d6de9c
C
158 },
159 [ScopeNames.WITH_ACTOR]: {
160 include: [
3acc5084 161 ActorModel
50d6de9c 162 ]
d48ff09d 163 }
3acc5084 164}))
3fd3ab2d
C
165@Table({
166 tableName: 'videoChannel',
0374b6b5
C
167 indexes: [
168 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
169
170 {
171 fields: [ 'accountId' ]
172 },
173 {
174 fields: [ 'actorId' ]
175 }
176 ]
3fd3ab2d
C
177})
178export 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) {
e6122097 249 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
f05a1c30
C
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
1ca9f7c3
C
485 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
486 const actor = this.Actor.toFormattedSummaryJSON()
487
488 return {
489 id: this.id,
490 name: actor.name,
491 displayName: this.getDisplayName(),
492 url: actor.url,
493 host: actor.host,
494 avatar: actor.avatar
495 }
496 }
497
498 toFormattedJSON (this: MChannelFormattable): VideoChannel {
50d6de9c 499 const actor = this.Actor.toFormattedJSON()
6b738c7a 500 const videoChannel = {
3fd3ab2d 501 id: this.id,
749c7247 502 displayName: this.getDisplayName(),
3fd3ab2d 503 description: this.description,
2422c46b 504 support: this.support,
50d6de9c 505 isLocal: this.Actor.isOwned(),
3fd3ab2d 506 createdAt: this.createdAt,
6b738c7a 507 updatedAt: this.updatedAt,
06a05d5f 508 ownerAccount: undefined
6b738c7a
C
509 }
510
a4f99a76 511 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
72c7248b 512
6b738c7a 513 return Object.assign(actor, videoChannel)
72c7248b
C
514 }
515
b5fecbf4 516 toActivityPubObject (this: MChannelAP): ActivityPubActor {
8424c402 517 const obj = this.Actor.toActivityPubObject(this.name)
50d6de9c
C
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}