]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel.ts
Fix channels creation creation limit count
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
1 import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
2 import {
3 AllowNull,
4 BeforeDestroy,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
9 Default,
10 DefaultScope,
11 ForeignKey,
12 HasMany,
13 Is,
14 Model,
15 Scopes,
16 Sequelize,
17 Table,
18 UpdatedAt
19 } from 'sequelize-typescript'
20 import { CONFIG } from '@server/initializers/config'
21 import { MAccountActor } from '@server/types/models'
22 import { pick } from '@shared/core-utils'
23 import { AttributesOnly } from '@shared/typescript-utils'
24 import { ActivityPubActor } from '../../../shared/models/activitypub'
25 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
26 import {
27 isVideoChannelDescriptionValid,
28 isVideoChannelDisplayNameValid,
29 isVideoChannelSupportValid
30 } from '../../helpers/custom-validators/video-channels'
31 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
32 import { sendDeleteActor } from '../../lib/activitypub/send'
33 import {
34 MChannel,
35 MChannelActor,
36 MChannelAP,
37 MChannelBannerAccountDefault,
38 MChannelFormattable,
39 MChannelSummaryFormattable
40 } from '../../types/models/video'
41 import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
42 import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
43 import { ActorFollowModel } from '../actor/actor-follow'
44 import { ActorImageModel } from '../actor/actor-image'
45 import { ServerModel } from '../server/server'
46 import { setAsUpdated } from '../shared'
47 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
48 import { VideoModel } from './video'
49 import { VideoPlaylistModel } from './video-playlist'
50
51 export enum ScopeNames {
52 FOR_API = 'FOR_API',
53 SUMMARY = 'SUMMARY',
54 WITH_ACCOUNT = 'WITH_ACCOUNT',
55 WITH_ACTOR = 'WITH_ACTOR',
56 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
57 WITH_VIDEOS = 'WITH_VIDEOS',
58 WITH_STATS = 'WITH_STATS'
59 }
60
61 type AvailableForListOptions = {
62 actorId: number
63 search?: string
64 host?: string
65 handles?: string[]
66 forCount?: boolean
67 }
68
69 type AvailableWithStatsOptions = {
70 daysPrior: number
71 }
72
73 export type SummaryOptions = {
74 actorRequired?: boolean // Default: true
75 withAccount?: boolean // Default: false
76 withAccountBlockerIds?: number[]
77 }
78
79 @DefaultScope(() => ({
80 include: [
81 {
82 model: ActorModel,
83 required: true
84 }
85 ]
86 }))
87 @Scopes(() => ({
88 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
89 // Only list local channels OR channels that are on an instance followed by actorId
90 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
91
92 const whereActorAnd: WhereOptions[] = [
93 {
94 [Op.or]: [
95 {
96 serverId: null
97 },
98 {
99 serverId: {
100 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
101 }
102 }
103 ]
104 }
105 ]
106
107 let serverRequired = false
108 let whereServer: WhereOptions
109
110 if (options.host && options.host !== WEBSERVER.HOST) {
111 serverRequired = true
112 whereServer = { host: options.host }
113 }
114
115 if (options.host === WEBSERVER.HOST) {
116 whereActorAnd.push({
117 serverId: null
118 })
119 }
120
121 if (Array.isArray(options.handles) && options.handles.length !== 0) {
122 const or: string[] = []
123
124 for (const handle of options.handles || []) {
125 const [ preferredUsername, host ] = handle.split('@')
126
127 if (!host || host === WEBSERVER.HOST) {
128 or.push(`("preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} AND "serverId" IS NULL)`)
129 } else {
130 or.push(
131 `(` +
132 `"preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} ` +
133 `AND "host" = ${VideoChannelModel.sequelize.escape(host)}` +
134 `)`
135 )
136 }
137 }
138
139 whereActorAnd.push({
140 id: {
141 [Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
142 }
143 })
144 }
145
146 const channelActorInclude: Includeable[] = []
147 const accountActorInclude: Includeable[] = []
148
149 if (options.forCount !== true) {
150 accountActorInclude.push({
151 model: ServerModel,
152 required: false
153 })
154
155 accountActorInclude.push({
156 model: ActorImageModel,
157 as: 'Avatars',
158 required: false
159 })
160
161 channelActorInclude.push({
162 model: ActorImageModel,
163 as: 'Avatars',
164 required: false
165 })
166
167 channelActorInclude.push({
168 model: ActorImageModel,
169 as: 'Banners',
170 required: false
171 })
172 }
173
174 if (options.forCount !== true || serverRequired) {
175 channelActorInclude.push({
176 model: ServerModel,
177 duplicating: false,
178 required: serverRequired,
179 where: whereServer
180 })
181 }
182
183 return {
184 include: [
185 {
186 attributes: {
187 exclude: unusedActorAttributesForAPI
188 },
189 model: ActorModel.unscoped(),
190 where: {
191 [Op.and]: whereActorAnd
192 },
193 include: channelActorInclude
194 },
195 {
196 model: AccountModel.unscoped(),
197 required: true,
198 include: [
199 {
200 attributes: {
201 exclude: unusedActorAttributesForAPI
202 },
203 model: ActorModel.unscoped(),
204 required: true,
205 include: accountActorInclude
206 }
207 ]
208 }
209 ]
210 }
211 },
212 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
213 const include: Includeable[] = [
214 {
215 attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
216 model: ActorModel.unscoped(),
217 required: options.actorRequired ?? true,
218 include: [
219 {
220 attributes: [ 'host' ],
221 model: ServerModel.unscoped(),
222 required: false
223 },
224 {
225 model: ActorImageModel,
226 as: 'Avatars',
227 required: false
228 }
229 ]
230 }
231 ]
232
233 const base: FindOptions = {
234 attributes: [ 'id', 'name', 'description', 'actorId' ]
235 }
236
237 if (options.withAccount === true) {
238 include.push({
239 model: AccountModel.scope({
240 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
241 }),
242 required: true
243 })
244 }
245
246 base.include = include
247
248 return base
249 },
250 [ScopeNames.WITH_ACCOUNT]: {
251 include: [
252 {
253 model: AccountModel,
254 required: true
255 }
256 ]
257 },
258 [ScopeNames.WITH_ACTOR]: {
259 include: [
260 ActorModel
261 ]
262 },
263 [ScopeNames.WITH_ACTOR_BANNER]: {
264 include: [
265 {
266 model: ActorModel,
267 include: [
268 {
269 model: ActorImageModel,
270 required: false,
271 as: 'Banners'
272 }
273 ]
274 }
275 ]
276 },
277 [ScopeNames.WITH_VIDEOS]: {
278 include: [
279 VideoModel
280 ]
281 },
282 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
283 const daysPrior = parseInt(options.daysPrior + '', 10)
284
285 return {
286 attributes: {
287 include: [
288 [
289 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
290 'videosCount'
291 ],
292 [
293 literal(
294 '(' +
295 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
296 'FROM ( ' +
297 'WITH ' +
298 'days AS ( ' +
299 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
300 `date_trunc('day', now()), '1 day'::interval) AS day ` +
301 ') ' +
302 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
303 'FROM days ' +
304 'LEFT JOIN (' +
305 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
306 'AND "video"."channelId" = "VideoChannelModel"."id"' +
307 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
308 'GROUP BY day ' +
309 'ORDER BY day ' +
310 ') t' +
311 ')'
312 ),
313 'viewsPerDay'
314 ]
315 ]
316 }
317 }
318 }
319 }))
320 @Table({
321 tableName: 'videoChannel',
322 indexes: [
323 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
324
325 {
326 fields: [ 'accountId' ]
327 },
328 {
329 fields: [ 'actorId' ]
330 }
331 ]
332 })
333 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
334
335 @AllowNull(false)
336 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
337 @Column
338 name: string
339
340 @AllowNull(true)
341 @Default(null)
342 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
343 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
344 description: string
345
346 @AllowNull(true)
347 @Default(null)
348 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
349 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
350 support: string
351
352 @CreatedAt
353 createdAt: Date
354
355 @UpdatedAt
356 updatedAt: Date
357
358 @ForeignKey(() => ActorModel)
359 @Column
360 actorId: number
361
362 @BelongsTo(() => ActorModel, {
363 foreignKey: {
364 allowNull: false
365 },
366 onDelete: 'cascade'
367 })
368 Actor: ActorModel
369
370 @ForeignKey(() => AccountModel)
371 @Column
372 accountId: number
373
374 @BelongsTo(() => AccountModel, {
375 foreignKey: {
376 allowNull: false
377 }
378 })
379 Account: AccountModel
380
381 @HasMany(() => VideoModel, {
382 foreignKey: {
383 name: 'channelId',
384 allowNull: false
385 },
386 onDelete: 'CASCADE',
387 hooks: true
388 })
389 Videos: VideoModel[]
390
391 @HasMany(() => VideoPlaylistModel, {
392 foreignKey: {
393 allowNull: true
394 },
395 onDelete: 'CASCADE',
396 hooks: true
397 })
398 VideoPlaylists: VideoPlaylistModel[]
399
400 @BeforeDestroy
401 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
402 if (!instance.Actor) {
403 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
404 }
405
406 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
407
408 if (instance.Actor.isOwned()) {
409 return sendDeleteActor(instance.Actor, options.transaction)
410 }
411
412 return undefined
413 }
414
415 static countByAccount (accountId: number) {
416 const query = {
417 where: {
418 accountId
419 }
420 }
421
422 return VideoChannelModel.unscoped().count(query)
423 }
424
425 static async getStats () {
426
427 function getActiveVideoChannels (days: number) {
428 const options = {
429 type: QueryTypes.SELECT as QueryTypes.SELECT,
430 raw: true
431 }
432
433 const query = `
434 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
435 FROM "videoChannel" AS "VideoChannelModel"
436 INNER JOIN "video" AS "Videos"
437 ON "VideoChannelModel"."id" = "Videos"."channelId"
438 AND ("Videos"."publishedAt" > Now() - interval '${days}d')
439 INNER JOIN "account" AS "Account"
440 ON "VideoChannelModel"."accountId" = "Account"."id"
441 INNER JOIN "actor" AS "Account->Actor"
442 ON "Account"."actorId" = "Account->Actor"."id"
443 AND "Account->Actor"."serverId" IS NULL
444 LEFT OUTER JOIN "server" AS "Account->Actor->Server"
445 ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
446
447 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
448 .then(r => parseInt(r[0].count, 10))
449 }
450
451 const totalLocalVideoChannels = await VideoChannelModel.count()
452 const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
453 const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
454 const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
455 const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)
456
457 return {
458 totalLocalVideoChannels,
459 totalLocalDailyActiveVideoChannels,
460 totalLocalWeeklyActiveVideoChannels,
461 totalLocalMonthlyActiveVideoChannels,
462 totalHalfYearActiveVideoChannels
463 }
464 }
465
466 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
467 const query = {
468 attributes: [ ],
469 offset: 0,
470 order: getSort(sort),
471 include: [
472 {
473 attributes: [ 'preferredUsername', 'serverId' ],
474 model: ActorModel.unscoped(),
475 where: {
476 serverId: null
477 }
478 }
479 ]
480 }
481
482 return VideoChannelModel
483 .unscoped()
484 .findAll(query)
485 }
486
487 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
488 start: number
489 count: number
490 sort: string
491 }) {
492 const { actorId } = parameters
493
494 const query = {
495 offset: parameters.start,
496 limit: parameters.count,
497 order: getSort(parameters.sort)
498 }
499
500 const getScope = (forCount: boolean) => {
501 return { method: [ ScopeNames.FOR_API, { actorId, forCount } as AvailableForListOptions ] }
502 }
503
504 return Promise.all([
505 VideoChannelModel.scope(getScope(true)).count(),
506 VideoChannelModel.scope(getScope(false)).findAll(query)
507 ]).then(([ total, data ]) => ({ total, data }))
508 }
509
510 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
511 start: number
512 count: number
513 sort: string
514 }) {
515 let attributesInclude: any[] = [ literal('0 as similarity') ]
516 let where: WhereOptions
517
518 if (options.search) {
519 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
520 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
521 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
522
523 where = {
524 [Op.or]: [
525 Sequelize.literal(
526 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
527 ),
528 Sequelize.literal(
529 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
530 )
531 ]
532 }
533 }
534
535 const query = {
536 attributes: {
537 include: attributesInclude
538 },
539 offset: options.start,
540 limit: options.count,
541 order: getSort(options.sort),
542 where
543 }
544
545 const getScope = (forCount: boolean) => {
546 return {
547 method: [
548 ScopeNames.FOR_API, {
549 ...pick(options, [ 'actorId', 'host', 'handles' ]),
550
551 forCount
552 } as AvailableForListOptions
553 ]
554 }
555 }
556
557 return Promise.all([
558 VideoChannelModel.scope(getScope(true)).count(query),
559 VideoChannelModel.scope(getScope(false)).findAll(query)
560 ]).then(([ total, data ]) => ({ total, data }))
561 }
562
563 static listByAccountForAPI (options: {
564 accountId: number
565 start: number
566 count: number
567 sort: string
568 withStats?: boolean
569 search?: string
570 }) {
571 const escapedSearch = VideoModel.sequelize.escape(options.search)
572 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
573 const where = options.search
574 ? {
575 [Op.or]: [
576 Sequelize.literal(
577 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
578 ),
579 Sequelize.literal(
580 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
581 )
582 ]
583 }
584 : null
585
586 const getQuery = (forCount: boolean) => {
587 const accountModel = forCount
588 ? AccountModel.unscoped()
589 : AccountModel
590
591 return {
592 offset: options.start,
593 limit: options.count,
594 order: getSort(options.sort),
595 include: [
596 {
597 model: accountModel,
598 where: {
599 id: options.accountId
600 },
601 required: true
602 }
603 ],
604 where
605 }
606 }
607
608 const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
609
610 if (options.withStats === true) {
611 findScopes.push({
612 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
613 })
614 }
615
616 return Promise.all([
617 VideoChannelModel.unscoped().count(getQuery(true)),
618 VideoChannelModel.scope(findScopes).findAll(getQuery(false))
619 ]).then(([ total, data ]) => ({ total, data }))
620 }
621
622 static listAllByAccount (accountId: number): Promise<MChannel[]> {
623 const query = {
624 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
625 include: [
626 {
627 attributes: [],
628 model: AccountModel.unscoped(),
629 where: {
630 id: accountId
631 },
632 required: true
633 }
634 ]
635 }
636
637 return VideoChannelModel.findAll(query)
638 }
639
640 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
641 return VideoChannelModel.unscoped()
642 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
643 .findByPk(id, { transaction })
644 }
645
646 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
647 const query = {
648 include: [
649 {
650 model: ActorModel,
651 required: true,
652 where: {
653 url
654 },
655 include: [
656 {
657 model: ActorImageModel,
658 required: false,
659 as: 'Banners'
660 }
661 ]
662 }
663 ]
664 }
665
666 return VideoChannelModel
667 .scope([ ScopeNames.WITH_ACCOUNT ])
668 .findOne(query)
669 }
670
671 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
672 const [ name, host ] = nameWithHost.split('@')
673
674 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
675
676 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
677 }
678
679 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
680 const query = {
681 include: [
682 {
683 model: ActorModel,
684 required: true,
685 where: {
686 preferredUsername: name,
687 serverId: null
688 },
689 include: [
690 {
691 model: ActorImageModel,
692 required: false,
693 as: 'Banners'
694 }
695 ]
696 }
697 ]
698 }
699
700 return VideoChannelModel.unscoped()
701 .scope([ ScopeNames.WITH_ACCOUNT ])
702 .findOne(query)
703 }
704
705 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
706 const query = {
707 include: [
708 {
709 model: ActorModel,
710 required: true,
711 where: {
712 preferredUsername: name
713 },
714 include: [
715 {
716 model: ServerModel,
717 required: true,
718 where: { host }
719 },
720 {
721 model: ActorImageModel,
722 required: false,
723 as: 'Banners'
724 }
725 ]
726 }
727 ]
728 }
729
730 return VideoChannelModel.unscoped()
731 .scope([ ScopeNames.WITH_ACCOUNT ])
732 .findOne(query)
733 }
734
735 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
736 const actor = this.Actor.toFormattedSummaryJSON()
737
738 return {
739 id: this.id,
740 name: actor.name,
741 displayName: this.getDisplayName(),
742 url: actor.url,
743 host: actor.host,
744 avatars: actor.avatars,
745
746 // TODO: remove, deprecated in 4.2
747 avatar: actor.avatar
748 }
749 }
750
751 toFormattedJSON (this: MChannelFormattable): VideoChannel {
752 const viewsPerDayString = this.get('viewsPerDay') as string
753 const videosCount = this.get('videosCount') as number
754
755 let viewsPerDay: { date: Date, views: number }[]
756
757 if (viewsPerDayString) {
758 viewsPerDay = viewsPerDayString.split(',')
759 .map(v => {
760 const [ dateString, amount ] = v.split('|')
761
762 return {
763 date: new Date(dateString),
764 views: +amount
765 }
766 })
767 }
768
769 const actor = this.Actor.toFormattedJSON()
770 const videoChannel = {
771 id: this.id,
772 displayName: this.getDisplayName(),
773 description: this.description,
774 support: this.support,
775 isLocal: this.Actor.isOwned(),
776 updatedAt: this.updatedAt,
777
778 ownerAccount: undefined,
779
780 videosCount,
781 viewsPerDay,
782
783 avatars: actor.avatars,
784
785 // TODO: remove, deprecated in 4.2
786 avatar: actor.avatar
787 }
788
789 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
790
791 return Object.assign(actor, videoChannel)
792 }
793
794 toActivityPubObject (this: MChannelAP): ActivityPubActor {
795 const obj = this.Actor.toActivityPubObject(this.name)
796
797 return Object.assign(obj, {
798 summary: this.description,
799 support: this.support,
800 attributedTo: [
801 {
802 type: 'Person' as 'Person',
803 id: this.Account.Actor.url
804 }
805 ]
806 })
807 }
808
809 getLocalUrl (this: MAccountActor | MChannelActor) {
810 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
811 }
812
813 getDisplayName () {
814 return this.name
815 }
816
817 isOutdated () {
818 return this.Actor.isOutdated()
819 }
820
821 setAsUpdated (transaction?: Transaction) {
822 return setAsUpdated('videoChannel', this.id, transaction)
823 }
824 }