1 import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
22 } from 'sequelize-typescript'
23 import { CONFIG } from '@server/initializers/config'
24 import { InternalEventEmitter } from '@server/lib/internal-event-emitter'
25 import { MAccountHost } from '@server/types/models'
26 import { forceNumber, pick } from '@shared/core-utils'
27 import { AttributesOnly } from '@shared/typescript-utils'
28 import { ActivityPubActor } from '../../../shared/models/activitypub'
29 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
31 isVideoChannelDescriptionValid,
32 isVideoChannelDisplayNameValid,
33 isVideoChannelSupportValid
34 } from '../../helpers/custom-validators/video-channels'
35 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
36 import { sendDeleteActor } from '../../lib/activitypub/send'
41 MChannelBannerAccountDefault,
44 MChannelSummaryFormattable
45 } from '../../types/models/video'
46 import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
47 import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
48 import { ActorFollowModel } from '../actor/actor-follow'
49 import { ActorImageModel } from '../actor/actor-image'
50 import { ServerModel } from '../server/server'
52 buildServerIdsFollowedBy,
53 buildTrigramSearchIndex,
54 createSimilarityAttribute,
59 import { VideoModel } from './video'
60 import { VideoPlaylistModel } from './video-playlist'
62 export enum ScopeNames {
65 WITH_ACCOUNT = 'WITH_ACCOUNT',
66 WITH_ACTOR = 'WITH_ACTOR',
67 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
68 WITH_VIDEOS = 'WITH_VIDEOS',
69 WITH_STATS = 'WITH_STATS'
72 type AvailableForListOptions = {
80 type AvailableWithStatsOptions = {
84 export type SummaryOptions = {
85 actorRequired?: boolean // Default: true
86 withAccount?: boolean // Default: false
87 withAccountBlockerIds?: number[]
90 @DefaultScope(() => ({
99 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
100 // Only list local channels OR channels that are on an instance followed by actorId
101 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
103 const whereActorAnd: WhereOptions[] = [
111 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
118 let serverRequired = false
119 let whereServer: WhereOptions
121 if (options.host && options.host !== WEBSERVER.HOST) {
122 serverRequired = true
123 whereServer = { host: options.host }
126 if (options.host === WEBSERVER.HOST) {
132 if (Array.isArray(options.handles) && options.handles.length !== 0) {
133 const or: string[] = []
135 for (const handle of options.handles || []) {
136 const [ preferredUsername, host ] = handle.split('@')
138 const sanitizedPreferredUsername = VideoChannelModel.sequelize.escape(preferredUsername.toLowerCase())
139 const sanitizedHost = VideoChannelModel.sequelize.escape(host)
141 if (!host || host === WEBSERVER.HOST) {
142 or.push(`(LOWER("preferredUsername") = ${sanitizedPreferredUsername} AND "serverId" IS NULL)`)
146 `LOWER("preferredUsername") = ${sanitizedPreferredUsername} ` +
147 `AND "host" = ${sanitizedHost}` +
155 [Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
160 const channelActorInclude: Includeable[] = []
161 const accountActorInclude: Includeable[] = []
163 if (options.forCount !== true) {
164 accountActorInclude.push({
169 accountActorInclude.push({
170 model: ActorImageModel,
175 channelActorInclude.push({
176 model: ActorImageModel,
181 channelActorInclude.push({
182 model: ActorImageModel,
188 if (options.forCount !== true || serverRequired) {
189 channelActorInclude.push({
192 required: serverRequired,
201 exclude: unusedActorAttributesForAPI
203 model: ActorModel.unscoped(),
205 [Op.and]: whereActorAnd
207 include: channelActorInclude
210 model: AccountModel.unscoped(),
215 exclude: unusedActorAttributesForAPI
217 model: ActorModel.unscoped(),
219 include: accountActorInclude
226 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
227 const include: Includeable[] = [
229 attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
230 model: ActorModel.unscoped(),
231 required: options.actorRequired ?? true,
234 attributes: [ 'host' ],
235 model: ServerModel.unscoped(),
239 model: ActorImageModel,
247 const base: FindOptions = {
248 attributes: [ 'id', 'name', 'description', 'actorId' ]
251 if (options.withAccount === true) {
253 model: AccountModel.scope({
254 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
260 base.include = include
264 [ScopeNames.WITH_ACCOUNT]: {
272 [ScopeNames.WITH_ACTOR]: {
277 [ScopeNames.WITH_ACTOR_BANNER]: {
283 model: ActorImageModel,
291 [ScopeNames.WITH_VIDEOS]: {
296 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
297 const daysPrior = forceNumber(options.daysPrior)
303 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
309 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
313 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
314 `date_trunc('day', now()), '1 day'::interval) AS day ` +
316 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
319 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
320 'AND "video"."channelId" = "VideoChannelModel"."id"' +
321 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
332 'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' +
334 'WHERE "video"."channelId" = "VideoChannelModel"."id"' +
345 tableName: 'videoChannel',
347 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
350 fields: [ 'accountId' ]
353 fields: [ 'actorId' ]
357 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
360 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
366 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
367 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
372 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
373 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
382 @ForeignKey(() => ActorModel)
386 @BelongsTo(() => ActorModel, {
394 @ForeignKey(() => AccountModel)
398 @BelongsTo(() => AccountModel, {
403 Account: AccountModel
405 @HasMany(() => VideoModel, {
415 @HasMany(() => VideoPlaylistModel, {
422 VideoPlaylists: VideoPlaylistModel[]
425 static notifyCreate (channel: MChannel) {
426 InternalEventEmitter.Instance.emit('channel-created', { channel })
430 static notifyUpdate (channel: MChannel) {
431 InternalEventEmitter.Instance.emit('channel-updated', { channel })
435 static notifyDestroy (channel: MChannel) {
436 InternalEventEmitter.Instance.emit('channel-deleted', { channel })
440 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
441 if (!instance.Actor) {
442 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
445 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
447 if (instance.Actor.isOwned()) {
448 return sendDeleteActor(instance.Actor, options.transaction)
454 static countByAccount (accountId: number) {
461 return VideoChannelModel.unscoped().count(query)
464 static async getStats () {
466 function getLocalVideoChannelStats (days?: number) {
468 type: QueryTypes.SELECT as QueryTypes.SELECT,
472 const videoJoin = days
473 ? `INNER JOIN "video" AS "Videos" ON "VideoChannelModel"."id" = "Videos"."channelId" ` +
474 `AND ("Videos"."publishedAt" > Now() - interval '${days}d')`
478 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
479 FROM "videoChannel" AS "VideoChannelModel"
481 INNER JOIN "account" AS "Account" ON "VideoChannelModel"."accountId" = "Account"."id"
482 INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
483 AND "Account->Actor"."serverId" IS NULL`
485 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
486 .then(r => parseInt(r[0].count, 10))
489 const totalLocalVideoChannels = await getLocalVideoChannelStats()
490 const totalLocalDailyActiveVideoChannels = await getLocalVideoChannelStats(1)
491 const totalLocalWeeklyActiveVideoChannels = await getLocalVideoChannelStats(7)
492 const totalLocalMonthlyActiveVideoChannels = await getLocalVideoChannelStats(30)
493 const totalLocalHalfYearActiveVideoChannels = await getLocalVideoChannelStats(180)
496 totalLocalVideoChannels,
497 totalLocalDailyActiveVideoChannels,
498 totalLocalWeeklyActiveVideoChannels,
499 totalLocalMonthlyActiveVideoChannels,
500 totalLocalHalfYearActiveVideoChannels
504 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
508 order: getSort(sort),
511 attributes: [ 'preferredUsername', 'serverId' ],
512 model: ActorModel.unscoped(),
520 return VideoChannelModel
525 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
530 const { actorId } = parameters
533 offset: parameters.start,
534 limit: parameters.count,
535 order: getSort(parameters.sort)
538 const getScope = (forCount: boolean) => {
539 return { method: [ ScopeNames.FOR_API, { actorId, forCount } as AvailableForListOptions ] }
543 VideoChannelModel.scope(getScope(true)).count(),
544 VideoChannelModel.scope(getScope(false)).findAll(query)
545 ]).then(([ total, data ]) => ({ total, data }))
548 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
553 let attributesInclude: any[] = [ literal('0 as similarity') ]
554 let where: WhereOptions
556 if (options.search) {
557 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
558 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
559 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
564 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
567 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
575 include: attributesInclude
577 offset: options.start,
578 limit: options.count,
579 order: getSort(options.sort),
583 const getScope = (forCount: boolean) => {
586 ScopeNames.FOR_API, {
587 ...pick(options, [ 'actorId', 'host', 'handles' ]),
590 } as AvailableForListOptions
596 VideoChannelModel.scope(getScope(true)).count(query),
597 VideoChannelModel.scope(getScope(false)).findAll(query)
598 ]).then(([ total, data ]) => ({ total, data }))
601 static listByAccountForAPI (options: {
609 const escapedSearch = VideoModel.sequelize.escape(options.search)
610 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
611 const where = options.search
615 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
618 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
624 const getQuery = (forCount: boolean) => {
625 const accountModel = forCount
626 ? AccountModel.unscoped()
630 offset: options.start,
631 limit: options.count,
632 order: getSort(options.sort),
637 id: options.accountId
646 const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
648 if (options.withStats === true) {
650 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
655 VideoChannelModel.unscoped().count(getQuery(true)),
656 VideoChannelModel.scope(findScopes).findAll(getQuery(false))
657 ]).then(([ total, data ]) => ({ total, data }))
660 static listAllByAccount (accountId: number): Promise<MChannel[]> {
662 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
666 model: AccountModel.unscoped(),
675 return VideoChannelModel.findAll(query)
678 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
679 return VideoChannelModel.unscoped()
680 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
681 .findByPk(id, { transaction })
684 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
695 model: ActorImageModel,
704 return VideoChannelModel
705 .scope([ ScopeNames.WITH_ACCOUNT ])
709 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
710 const [ name, host ] = nameWithHost.split('@')
712 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
714 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
717 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
725 ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
731 model: ActorImageModel,
740 return VideoChannelModel.unscoped()
741 .scope([ ScopeNames.WITH_ACCOUNT ])
745 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
751 where: ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
759 model: ActorImageModel,
768 return VideoChannelModel.unscoped()
769 .scope([ ScopeNames.WITH_ACCOUNT ])
773 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
774 const actor = this.Actor.toFormattedSummaryJSON()
779 displayName: this.getDisplayName(),
782 avatars: actor.avatars,
784 // TODO: remove, deprecated in 4.2
789 toFormattedJSON (this: MChannelFormattable): VideoChannel {
790 const viewsPerDayString = this.get('viewsPerDay') as string
791 const videosCount = this.get('videosCount') as number
793 let viewsPerDay: { date: Date, views: number }[]
795 if (viewsPerDayString) {
796 viewsPerDay = viewsPerDayString.split(',')
798 const [ dateString, amount ] = v.split('|')
801 date: new Date(dateString),
807 const totalViews = this.get('totalViews') as number
809 const actor = this.Actor.toFormattedJSON()
810 const videoChannel = {
812 displayName: this.getDisplayName(),
813 description: this.description,
814 support: this.support,
815 isLocal: this.Actor.isOwned(),
816 updatedAt: this.updatedAt,
818 ownerAccount: undefined,
824 avatars: actor.avatars,
826 // TODO: remove, deprecated in 4.2
830 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
832 return Object.assign(actor, videoChannel)
835 async toActivityPubObject (this: MChannelAP): Promise<ActivityPubActor> {
836 const obj = await this.Actor.toActivityPubObject(this.name)
838 return Object.assign(obj, {
839 summary: this.description,
840 support: this.support,
843 type: 'Person' as 'Person',
844 id: this.Account.Actor.url
850 // Avoid error when running this method on MAccount... | MChannel...
851 getClientUrl (this: MAccountHost | MChannelHost) {
852 return WEBSERVER.URL + '/c/' + this.Actor.getIdentifier()
860 return this.Actor.isOutdated()
863 setAsUpdated (transaction?: Transaction) {
864 return setAsUpdated({ sequelize: this.sequelize, table: 'videoChannel', id: this.id, transaction })