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