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 { 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'
46 import { setAsUpdated } from '../shared'
47 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
48 import { VideoModel } from './video'
49 import { VideoPlaylistModel } from './video-playlist'
51 export enum ScopeNames {
54 WITH_ACCOUNT = 'WITH_ACCOUNT',
55 WITH_ACTOR = 'WITH_ACTOR',
56 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
57 WITH_VIDEOS = 'WITH_VIDEOS',
58 WITH_STATS = 'WITH_STATS'
61 type AvailableForListOptions = {
69 type AvailableWithStatsOptions = {
73 export type SummaryOptions = {
74 actorRequired?: boolean // Default: true
75 withAccount?: boolean // Default: false
76 withAccountBlockerIds?: number[]
79 @DefaultScope(() => ({
88 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
89 // Only list local channels OR channels that are on an instance followed by actorId
90 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
92 const whereActorAnd: WhereOptions[] = [
100 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
107 let serverRequired = false
108 let whereServer: WhereOptions
110 if (options.host && options.host !== WEBSERVER.HOST) {
111 serverRequired = true
112 whereServer = { host: options.host }
115 if (options.host === WEBSERVER.HOST) {
121 if (Array.isArray(options.handles) && options.handles.length !== 0) {
122 const or: string[] = []
124 for (const handle of options.handles || []) {
125 const [ preferredUsername, host ] = handle.split('@')
127 if (!host || host === WEBSERVER.HOST) {
128 or.push(`("preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} AND "serverId" IS NULL)`)
132 `"preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} ` +
133 `AND "host" = ${VideoChannelModel.sequelize.escape(host)}` +
141 [Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
146 const channelActorInclude: Includeable[] = []
147 const accountActorInclude: Includeable[] = []
149 if (options.forCount !== true) {
150 accountActorInclude.push({
155 accountActorInclude.push({
156 model: ActorImageModel,
161 channelActorInclude.push({
162 model: ActorImageModel,
167 channelActorInclude.push({
168 model: ActorImageModel,
174 if (options.forCount !== true || serverRequired) {
175 channelActorInclude.push({
178 required: serverRequired,
187 exclude: unusedActorAttributesForAPI
189 model: ActorModel.unscoped(),
191 [Op.and]: whereActorAnd
193 include: channelActorInclude
196 model: AccountModel.unscoped(),
201 exclude: unusedActorAttributesForAPI
203 model: ActorModel.unscoped(),
205 include: accountActorInclude
212 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
213 const include: Includeable[] = [
215 attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
216 model: ActorModel.unscoped(),
217 required: options.actorRequired ?? true,
220 attributes: [ 'host' ],
221 model: ServerModel.unscoped(),
225 model: ActorImageModel,
233 const base: FindOptions = {
234 attributes: [ 'id', 'name', 'description', 'actorId' ]
237 if (options.withAccount === true) {
239 model: AccountModel.scope({
240 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
246 base.include = include
250 [ScopeNames.WITH_ACCOUNT]: {
258 [ScopeNames.WITH_ACTOR]: {
263 [ScopeNames.WITH_ACTOR_BANNER]: {
269 model: ActorImageModel,
277 [ScopeNames.WITH_VIDEOS]: {
282 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
283 const daysPrior = parseInt(options.daysPrior + '', 10)
289 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
295 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
299 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
300 `date_trunc('day', now()), '1 day'::interval) AS day ` +
302 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
305 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
306 'AND "video"."channelId" = "VideoChannelModel"."id"' +
307 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
321 tableName: 'videoChannel',
323 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
326 fields: [ 'accountId' ]
329 fields: [ 'actorId' ]
333 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
336 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
342 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
343 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
348 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
349 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
358 @ForeignKey(() => ActorModel)
362 @BelongsTo(() => ActorModel, {
370 @ForeignKey(() => AccountModel)
374 @BelongsTo(() => AccountModel, {
379 Account: AccountModel
381 @HasMany(() => VideoModel, {
391 @HasMany(() => VideoPlaylistModel, {
398 VideoPlaylists: VideoPlaylistModel[]
401 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
402 if (!instance.Actor) {
403 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
406 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
408 if (instance.Actor.isOwned()) {
409 return sendDeleteActor(instance.Actor, options.transaction)
415 static countByAccount (accountId: number) {
422 return VideoChannelModel.unscoped().count(query)
425 static async getStats () {
427 function getActiveVideoChannels (days: number) {
429 type: QueryTypes.SELECT as QueryTypes.SELECT,
434 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
435 FROM "videoChannel" AS "VideoChannelModel"
436 INNER JOIN "video" AS "Videos"
437 ON "VideoChannelModel"."id" = "Videos"."channelId"
438 AND ("Videos"."publishedAt" > Now() - interval '${days}d')
439 INNER JOIN "account" AS "Account"
440 ON "VideoChannelModel"."accountId" = "Account"."id"
441 INNER JOIN "actor" AS "Account->Actor"
442 ON "Account"."actorId" = "Account->Actor"."id"
443 AND "Account->Actor"."serverId" IS NULL
444 LEFT OUTER JOIN "server" AS "Account->Actor->Server"
445 ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
447 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
448 .then(r => parseInt(r[0].count, 10))
451 const totalLocalVideoChannels = await VideoChannelModel.count()
452 const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
453 const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
454 const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
455 const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)
458 totalLocalVideoChannels,
459 totalLocalDailyActiveVideoChannels,
460 totalLocalWeeklyActiveVideoChannels,
461 totalLocalMonthlyActiveVideoChannels,
462 totalHalfYearActiveVideoChannels
466 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
470 order: getSort(sort),
473 attributes: [ 'preferredUsername', 'serverId' ],
474 model: ActorModel.unscoped(),
482 return VideoChannelModel
487 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
492 const { actorId } = parameters
495 offset: parameters.start,
496 limit: parameters.count,
497 order: getSort(parameters.sort)
500 const getScope = (forCount: boolean) => {
501 return { method: [ ScopeNames.FOR_API, { actorId, forCount } as AvailableForListOptions ] }
505 VideoChannelModel.scope(getScope(true)).count(),
506 VideoChannelModel.scope(getScope(false)).findAll(query)
507 ]).then(([ total, data ]) => ({ total, data }))
510 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
515 let attributesInclude: any[] = [ literal('0 as similarity') ]
516 let where: WhereOptions
518 if (options.search) {
519 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
520 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
521 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
526 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
529 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
537 include: attributesInclude
539 offset: options.start,
540 limit: options.count,
541 order: getSort(options.sort),
545 const getScope = (forCount: boolean) => {
548 ScopeNames.FOR_API, {
549 ...pick(options, [ 'actorId', 'host', 'handles' ]),
552 } as AvailableForListOptions
558 VideoChannelModel.scope(getScope(true)).count(query),
559 VideoChannelModel.scope(getScope(false)).findAll(query)
560 ]).then(([ total, data ]) => ({ total, data }))
563 static listByAccountForAPI (options: {
571 const escapedSearch = VideoModel.sequelize.escape(options.search)
572 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
573 const where = options.search
577 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
580 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
586 const getQuery = (forCount: boolean) => {
587 const accountModel = forCount
588 ? AccountModel.unscoped()
592 offset: options.start,
593 limit: options.count,
594 order: getSort(options.sort),
599 id: options.accountId
608 const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
610 if (options.withStats === true) {
612 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
617 VideoChannelModel.unscoped().count(getQuery(true)),
618 VideoChannelModel.scope(findScopes).findAll(getQuery(false))
619 ]).then(([ total, data ]) => ({ total, data }))
622 static listAllByAccount (accountId: number): Promise<MChannel[]> {
624 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
628 model: AccountModel.unscoped(),
637 return VideoChannelModel.findAll(query)
640 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
641 return VideoChannelModel.unscoped()
642 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
643 .findByPk(id, { transaction })
646 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
657 model: ActorImageModel,
666 return VideoChannelModel
667 .scope([ ScopeNames.WITH_ACCOUNT ])
671 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
672 const [ name, host ] = nameWithHost.split('@')
674 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
676 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
679 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
686 preferredUsername: name,
691 model: ActorImageModel,
700 return VideoChannelModel.unscoped()
701 .scope([ ScopeNames.WITH_ACCOUNT ])
705 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
712 preferredUsername: name
721 model: ActorImageModel,
730 return VideoChannelModel.unscoped()
731 .scope([ ScopeNames.WITH_ACCOUNT ])
735 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
736 const actor = this.Actor.toFormattedSummaryJSON()
741 displayName: this.getDisplayName(),
744 avatars: actor.avatars,
746 // TODO: remove, deprecated in 4.2
751 toFormattedJSON (this: MChannelFormattable): VideoChannel {
752 const viewsPerDayString = this.get('viewsPerDay') as string
753 const videosCount = this.get('videosCount') as number
755 let viewsPerDay: { date: Date, views: number }[]
757 if (viewsPerDayString) {
758 viewsPerDay = viewsPerDayString.split(',')
760 const [ dateString, amount ] = v.split('|')
763 date: new Date(dateString),
769 const actor = this.Actor.toFormattedJSON()
770 const videoChannel = {
772 displayName: this.getDisplayName(),
773 description: this.description,
774 support: this.support,
775 isLocal: this.Actor.isOwned(),
776 updatedAt: this.updatedAt,
778 ownerAccount: undefined,
783 avatars: actor.avatars,
785 // TODO: remove, deprecated in 4.2
789 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
791 return Object.assign(actor, videoChannel)
794 toActivityPubObject (this: MChannelAP): ActivityPubActor {
795 const obj = this.Actor.toActivityPubObject(this.name)
797 return Object.assign(obj, {
798 summary: this.description,
799 support: this.support,
802 type: 'Person' as 'Person',
803 id: this.Account.Actor.url
809 getLocalUrl (this: MAccountActor | MChannelActor) {
810 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
818 return this.Actor.isOutdated()
821 setAsUpdated (transaction?: Transaction) {
822 return setAsUpdated('videoChannel', this.id, transaction)