1 import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
19 } from 'sequelize-typescript'
20 import { MAccountActor } from '@server/types/models'
21 import { AttributesOnly, pick } from '@shared/core-utils'
22 import { ActivityPubActor } from '../../../shared/models/activitypub'
23 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
25 isVideoChannelDescriptionValid,
26 isVideoChannelDisplayNameValid,
27 isVideoChannelSupportValid
28 } from '../../helpers/custom-validators/video-channels'
29 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
30 import { sendDeleteActor } from '../../lib/activitypub/send'
34 MChannelBannerAccountDefault,
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'
48 export enum ScopeNames {
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'
58 type AvailableForListOptions = {
65 type AvailableWithStatsOptions = {
69 export type SummaryOptions = {
70 actorRequired?: boolean // Default: true
71 withAccount?: boolean // Default: false
72 withAccountBlockerIds?: number[]
75 @DefaultScope(() => ({
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)
88 const whereActorAnd: WhereOptions[] = [
96 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
103 let serverRequired = false
104 let whereServer: WhereOptions
106 if (options.host && options.host !== WEBSERVER.HOST) {
107 serverRequired = true
108 whereServer = { host: options.host }
111 if (options.host === WEBSERVER.HOST) {
117 let rootWhere: WhereOptions
118 if (options.handles) {
119 const or: WhereOptions[] = []
121 for (const handle of options.handles || []) {
122 const [ preferredUsername, host ] = handle.split('@')
124 if (!host || host === WEBSERVER.HOST) {
126 '$Actor.preferredUsername$': preferredUsername,
127 '$Actor.serverId$': null
131 '$Actor.preferredUsername$': preferredUsername,
132 '$Actor.Server.host$': host
147 exclude: unusedActorAttributesForAPI
151 [Op.and]: whereActorAnd
156 required: serverRequired,
160 model: ActorImageModel,
165 model: ActorImageModel,
177 exclude: unusedActorAttributesForAPI
179 model: ActorModel, // Default scope includes avatar and server
187 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
188 const include: Includeable[] = [
190 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
191 model: ActorModel.unscoped(),
192 required: options.actorRequired ?? true,
195 attributes: [ 'host' ],
196 model: ServerModel.unscoped(),
200 model: ActorImageModel.unscoped(),
208 const base: FindOptions = {
209 attributes: [ 'id', 'name', 'description', 'actorId' ]
212 if (options.withAccount === true) {
214 model: AccountModel.scope({
215 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
221 base.include = include
225 [ScopeNames.WITH_ACCOUNT]: {
233 [ScopeNames.WITH_ACTOR]: {
238 [ScopeNames.WITH_ACTOR_BANNER]: {
244 model: ActorImageModel,
252 [ScopeNames.WITH_VIDEOS]: {
257 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
258 const daysPrior = parseInt(options.daysPrior + '', 10)
264 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
270 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
274 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
275 `date_trunc('day', now()), '1 day'::interval) AS day ` +
277 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
280 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
281 'AND "video"."channelId" = "VideoChannelModel"."id"' +
282 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
296 tableName: 'videoChannel',
298 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
301 fields: [ 'accountId' ]
304 fields: [ 'actorId' ]
308 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
311 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
317 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
318 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
323 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
324 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
333 @ForeignKey(() => ActorModel)
337 @BelongsTo(() => ActorModel, {
345 @ForeignKey(() => AccountModel)
349 @BelongsTo(() => AccountModel, {
354 Account: AccountModel
356 @HasMany(() => VideoModel, {
366 @HasMany(() => VideoPlaylistModel, {
373 VideoPlaylists: VideoPlaylistModel[]
376 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
377 if (!instance.Actor) {
378 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
381 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
383 if (instance.Actor.isOwned()) {
384 return sendDeleteActor(instance.Actor, options.transaction)
390 static countByAccount (accountId: number) {
397 return VideoChannelModel.count(query)
400 static async getStats () {
402 function getActiveVideoChannels (days: number) {
404 type: QueryTypes.SELECT as QueryTypes.SELECT,
409 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
410 FROM "videoChannel" AS "VideoChannelModel"
411 INNER JOIN "video" AS "Videos"
412 ON "VideoChannelModel"."id" = "Videos"."channelId"
413 AND ("Videos"."publishedAt" > Now() - interval '${days}d')
414 INNER JOIN "account" AS "Account"
415 ON "VideoChannelModel"."accountId" = "Account"."id"
416 INNER JOIN "actor" AS "Account->Actor"
417 ON "Account"."actorId" = "Account->Actor"."id"
418 AND "Account->Actor"."serverId" IS NULL
419 LEFT OUTER JOIN "server" AS "Account->Actor->Server"
420 ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
422 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
423 .then(r => parseInt(r[0].count, 10))
426 const totalLocalVideoChannels = await VideoChannelModel.count()
427 const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
428 const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
429 const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
430 const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)
433 totalLocalVideoChannels,
434 totalLocalDailyActiveVideoChannels,
435 totalLocalWeeklyActiveVideoChannels,
436 totalLocalMonthlyActiveVideoChannels,
437 totalHalfYearActiveVideoChannels
441 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
445 order: getSort(sort),
448 attributes: [ 'preferredUsername', 'serverId' ],
449 model: ActorModel.unscoped(),
457 return VideoChannelModel
462 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
467 const { actorId } = parameters
470 offset: parameters.start,
471 limit: parameters.count,
472 order: getSort(parameters.sort)
475 return VideoChannelModel
477 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
479 .findAndCountAll(query)
480 .then(({ rows, count }) => {
481 return { total: count, data: rows }
485 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
490 let attributesInclude: any[] = [ literal('0 as similarity') ]
491 let where: WhereOptions
493 if (options.search) {
494 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
495 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
496 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
501 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
504 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
512 include: attributesInclude
514 offset: options.start,
515 limit: options.count,
516 order: getSort(options.sort),
520 return VideoChannelModel
522 method: [ ScopeNames.FOR_API, pick(options, [ 'actorId', 'host', 'handles' ]) as AvailableForListOptions ]
524 .findAndCountAll(query)
525 .then(({ rows, count }) => {
526 return { total: count, data: rows }
530 static listByAccount (options: {
538 const escapedSearch = VideoModel.sequelize.escape(options.search)
539 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
540 const where = options.search
544 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
547 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
554 offset: options.start,
555 limit: options.count,
556 order: getSort(options.sort),
561 id: options.accountId
569 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
571 if (options.withStats === true) {
573 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
577 return VideoChannelModel
579 .findAndCountAll(query)
580 .then(({ rows, count }) => {
581 return { total: count, data: rows }
585 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
586 return VideoChannelModel.unscoped()
587 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
588 .findByPk(id, { transaction })
591 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
602 model: ActorImageModel,
611 return VideoChannelModel
612 .scope([ ScopeNames.WITH_ACCOUNT ])
616 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
617 const [ name, host ] = nameWithHost.split('@')
619 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
621 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
624 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
631 preferredUsername: name,
636 model: ActorImageModel,
645 return VideoChannelModel.unscoped()
646 .scope([ ScopeNames.WITH_ACCOUNT ])
650 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
657 preferredUsername: name
666 model: ActorImageModel,
675 return VideoChannelModel.unscoped()
676 .scope([ ScopeNames.WITH_ACCOUNT ])
680 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
681 const actor = this.Actor.toFormattedSummaryJSON()
686 displayName: this.getDisplayName(),
693 toFormattedJSON (this: MChannelFormattable): VideoChannel {
694 const viewsPerDayString = this.get('viewsPerDay') as string
695 const videosCount = this.get('videosCount') as number
697 let viewsPerDay: { date: Date, views: number }[]
699 if (viewsPerDayString) {
700 viewsPerDay = viewsPerDayString.split(',')
702 const [ dateString, amount ] = v.split('|')
705 date: new Date(dateString),
711 const actor = this.Actor.toFormattedJSON()
712 const videoChannel = {
714 displayName: this.getDisplayName(),
715 description: this.description,
716 support: this.support,
717 isLocal: this.Actor.isOwned(),
718 updatedAt: this.updatedAt,
719 ownerAccount: undefined,
724 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
726 return Object.assign(actor, videoChannel)
729 toActivityPubObject (this: MChannelAP): ActivityPubActor {
730 const obj = this.Actor.toActivityPubObject(this.name)
732 return Object.assign(obj, {
733 summary: this.description,
734 support: this.support,
737 type: 'Person' as 'Person',
738 id: this.Account.Actor.url
744 getLocalUrl (this: MAccountActor | MChannelActor) {
745 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
753 return this.Actor.isOutdated()
756 setAsUpdated (transaction?: Transaction) {
757 return setAsUpdated('videoChannel', this.id, transaction)