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