1 import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
19 } from 'sequelize-typescript'
20 import { CONFIG } from '@server/initializers/config'
21 import { MAccountActor } from '@server/types/models'
22 import { forceNumber, 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'
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'
37 MChannelBannerAccountDefault,
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'
47 buildServerIdsFollowedBy,
48 buildTrigramSearchIndex,
49 createSimilarityAttribute,
54 import { VideoModel } from './video'
55 import { VideoPlaylistModel } from './video-playlist'
57 export enum ScopeNames {
60 WITH_ACCOUNT = 'WITH_ACCOUNT',
61 WITH_ACTOR = 'WITH_ACTOR',
62 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
63 WITH_VIDEOS = 'WITH_VIDEOS',
64 WITH_STATS = 'WITH_STATS'
67 type AvailableForListOptions = {
75 type AvailableWithStatsOptions = {
79 export type SummaryOptions = {
80 actorRequired?: boolean // Default: true
81 withAccount?: boolean // Default: false
82 withAccountBlockerIds?: number[]
85 @DefaultScope(() => ({
94 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
95 // Only list local channels OR channels that are on an instance followed by actorId
96 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
98 const whereActorAnd: WhereOptions[] = [
106 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
113 let serverRequired = false
114 let whereServer: WhereOptions
116 if (options.host && options.host !== WEBSERVER.HOST) {
117 serverRequired = true
118 whereServer = { host: options.host }
121 if (options.host === WEBSERVER.HOST) {
127 if (Array.isArray(options.handles) && options.handles.length !== 0) {
128 const or: string[] = []
130 for (const handle of options.handles || []) {
131 const [ preferredUsername, host ] = handle.split('@')
133 const sanitizedPreferredUsername = VideoChannelModel.sequelize.escape(preferredUsername.toLowerCase())
134 const sanitizedHost = VideoChannelModel.sequelize.escape(host)
136 if (!host || host === WEBSERVER.HOST) {
137 or.push(`(LOWER("preferredUsername") = ${sanitizedPreferredUsername} AND "serverId" IS NULL)`)
141 `LOWER("preferredUsername") = ${sanitizedPreferredUsername} ` +
142 `AND "host" = ${sanitizedHost}` +
150 [Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
155 const channelActorInclude: Includeable[] = []
156 const accountActorInclude: Includeable[] = []
158 if (options.forCount !== true) {
159 accountActorInclude.push({
164 accountActorInclude.push({
165 model: ActorImageModel,
170 channelActorInclude.push({
171 model: ActorImageModel,
176 channelActorInclude.push({
177 model: ActorImageModel,
183 if (options.forCount !== true || serverRequired) {
184 channelActorInclude.push({
187 required: serverRequired,
196 exclude: unusedActorAttributesForAPI
198 model: ActorModel.unscoped(),
200 [Op.and]: whereActorAnd
202 include: channelActorInclude
205 model: AccountModel.unscoped(),
210 exclude: unusedActorAttributesForAPI
212 model: ActorModel.unscoped(),
214 include: accountActorInclude
221 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
222 const include: Includeable[] = [
224 attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
225 model: ActorModel.unscoped(),
226 required: options.actorRequired ?? true,
229 attributes: [ 'host' ],
230 model: ServerModel.unscoped(),
234 model: ActorImageModel,
242 const base: FindOptions = {
243 attributes: [ 'id', 'name', 'description', 'actorId' ]
246 if (options.withAccount === true) {
248 model: AccountModel.scope({
249 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
255 base.include = include
259 [ScopeNames.WITH_ACCOUNT]: {
267 [ScopeNames.WITH_ACTOR]: {
272 [ScopeNames.WITH_ACTOR_BANNER]: {
278 model: ActorImageModel,
286 [ScopeNames.WITH_VIDEOS]: {
291 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
292 const daysPrior = forceNumber(options.daysPrior)
298 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
304 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
308 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
309 `date_trunc('day', now()), '1 day'::interval) AS day ` +
311 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
314 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
315 'AND "video"."channelId" = "VideoChannelModel"."id"' +
316 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
327 'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' +
329 'WHERE "video"."channelId" = "VideoChannelModel"."id"' +
340 tableName: 'videoChannel',
342 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
345 fields: [ 'accountId' ]
348 fields: [ 'actorId' ]
352 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
355 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
361 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
362 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
367 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
368 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
377 @ForeignKey(() => ActorModel)
381 @BelongsTo(() => ActorModel, {
389 @ForeignKey(() => AccountModel)
393 @BelongsTo(() => AccountModel, {
398 Account: AccountModel
400 @HasMany(() => VideoModel, {
410 @HasMany(() => VideoPlaylistModel, {
417 VideoPlaylists: VideoPlaylistModel[]
420 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
421 if (!instance.Actor) {
422 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
425 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
427 if (instance.Actor.isOwned()) {
428 return sendDeleteActor(instance.Actor, options.transaction)
434 static countByAccount (accountId: number) {
441 return VideoChannelModel.unscoped().count(query)
444 static async getStats () {
446 function getLocalVideoChannelStats (days?: number) {
448 type: QueryTypes.SELECT as QueryTypes.SELECT,
452 const videoJoin = days
453 ? `INNER JOIN "video" AS "Videos" ON "VideoChannelModel"."id" = "Videos"."channelId" ` +
454 `AND ("Videos"."publishedAt" > Now() - interval '${days}d')`
458 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
459 FROM "videoChannel" AS "VideoChannelModel"
461 INNER JOIN "account" AS "Account" ON "VideoChannelModel"."accountId" = "Account"."id"
462 INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
463 AND "Account->Actor"."serverId" IS NULL`
465 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
466 .then(r => parseInt(r[0].count, 10))
469 const totalLocalVideoChannels = await getLocalVideoChannelStats()
470 const totalLocalDailyActiveVideoChannels = await getLocalVideoChannelStats(1)
471 const totalLocalWeeklyActiveVideoChannels = await getLocalVideoChannelStats(7)
472 const totalLocalMonthlyActiveVideoChannels = await getLocalVideoChannelStats(30)
473 const totalLocalHalfYearActiveVideoChannels = await getLocalVideoChannelStats(180)
476 totalLocalVideoChannels,
477 totalLocalDailyActiveVideoChannels,
478 totalLocalWeeklyActiveVideoChannels,
479 totalLocalMonthlyActiveVideoChannels,
480 totalLocalHalfYearActiveVideoChannels
484 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
488 order: getSort(sort),
491 attributes: [ 'preferredUsername', 'serverId' ],
492 model: ActorModel.unscoped(),
500 return VideoChannelModel
505 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
510 const { actorId } = parameters
513 offset: parameters.start,
514 limit: parameters.count,
515 order: getSort(parameters.sort)
518 const getScope = (forCount: boolean) => {
519 return { method: [ ScopeNames.FOR_API, { actorId, forCount } as AvailableForListOptions ] }
523 VideoChannelModel.scope(getScope(true)).count(),
524 VideoChannelModel.scope(getScope(false)).findAll(query)
525 ]).then(([ total, data ]) => ({ total, data }))
528 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
533 let attributesInclude: any[] = [ literal('0 as similarity') ]
534 let where: WhereOptions
536 if (options.search) {
537 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
538 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
539 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
544 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
547 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
555 include: attributesInclude
557 offset: options.start,
558 limit: options.count,
559 order: getSort(options.sort),
563 const getScope = (forCount: boolean) => {
566 ScopeNames.FOR_API, {
567 ...pick(options, [ 'actorId', 'host', 'handles' ]),
570 } as AvailableForListOptions
576 VideoChannelModel.scope(getScope(true)).count(query),
577 VideoChannelModel.scope(getScope(false)).findAll(query)
578 ]).then(([ total, data ]) => ({ total, data }))
581 static listByAccountForAPI (options: {
589 const escapedSearch = VideoModel.sequelize.escape(options.search)
590 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
591 const where = options.search
595 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
598 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
604 const getQuery = (forCount: boolean) => {
605 const accountModel = forCount
606 ? AccountModel.unscoped()
610 offset: options.start,
611 limit: options.count,
612 order: getSort(options.sort),
617 id: options.accountId
626 const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
628 if (options.withStats === true) {
630 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
635 VideoChannelModel.unscoped().count(getQuery(true)),
636 VideoChannelModel.scope(findScopes).findAll(getQuery(false))
637 ]).then(([ total, data ]) => ({ total, data }))
640 static listAllByAccount (accountId: number): Promise<MChannel[]> {
642 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
646 model: AccountModel.unscoped(),
655 return VideoChannelModel.findAll(query)
658 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
659 return VideoChannelModel.unscoped()
660 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
661 .findByPk(id, { transaction })
664 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
675 model: ActorImageModel,
684 return VideoChannelModel
685 .scope([ ScopeNames.WITH_ACCOUNT ])
689 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
690 const [ name, host ] = nameWithHost.split('@')
692 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
694 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
697 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
705 ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
711 model: ActorImageModel,
720 return VideoChannelModel.unscoped()
721 .scope([ ScopeNames.WITH_ACCOUNT ])
725 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
731 where: ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
739 model: ActorImageModel,
748 return VideoChannelModel.unscoped()
749 .scope([ ScopeNames.WITH_ACCOUNT ])
753 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
754 const actor = this.Actor.toFormattedSummaryJSON()
759 displayName: this.getDisplayName(),
762 avatars: actor.avatars,
764 // TODO: remove, deprecated in 4.2
769 toFormattedJSON (this: MChannelFormattable): VideoChannel {
770 const viewsPerDayString = this.get('viewsPerDay') as string
771 const videosCount = this.get('videosCount') as number
773 let viewsPerDay: { date: Date, views: number }[]
775 if (viewsPerDayString) {
776 viewsPerDay = viewsPerDayString.split(',')
778 const [ dateString, amount ] = v.split('|')
781 date: new Date(dateString),
787 const totalViews = this.get('totalViews') as number
789 const actor = this.Actor.toFormattedJSON()
790 const videoChannel = {
792 displayName: this.getDisplayName(),
793 description: this.description,
794 support: this.support,
795 isLocal: this.Actor.isOwned(),
796 updatedAt: this.updatedAt,
798 ownerAccount: undefined,
804 avatars: actor.avatars,
806 // TODO: remove, deprecated in 4.2
810 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
812 return Object.assign(actor, videoChannel)
815 async toActivityPubObject (this: MChannelAP): Promise<ActivityPubActor> {
816 const obj = await this.Actor.toActivityPubObject(this.name)
818 return Object.assign(obj, {
819 summary: this.description,
820 support: this.support,
823 type: 'Person' as 'Person',
824 id: this.Account.Actor.url
830 getLocalUrl (this: MAccountActor | MChannelActor) {
831 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
839 return this.Actor.isOutdated()
842 setAsUpdated (transaction?: Transaction) {
843 return setAsUpdated({ sequelize: this.sequelize, table: 'videoChannel', id: this.id, transaction })