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'
36 MChannelBannerAccountDefault,
38 MChannelSummaryFormattable
39 } from '../../types/models/video'
40 import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
41 import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
42 import { ActorFollowModel } from '../actor/actor-follow'
43 import { ActorImageModel } from '../actor/actor-image'
44 import { ServerModel } from '../server/server'
45 import { setAsUpdated } from '../shared'
46 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
47 import { VideoModel } from './video'
48 import { VideoPlaylistModel } from './video-playlist'
50 export enum ScopeNames {
53 WITH_ACCOUNT = 'WITH_ACCOUNT',
54 WITH_ACTOR = 'WITH_ACTOR',
55 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
56 WITH_VIDEOS = 'WITH_VIDEOS',
57 WITH_STATS = 'WITH_STATS'
60 type AvailableForListOptions = {
67 type AvailableWithStatsOptions = {
71 export type SummaryOptions = {
72 actorRequired?: boolean // Default: true
73 withAccount?: boolean // Default: false
74 withAccountBlockerIds?: number[]
77 @DefaultScope(() => ({
86 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
87 // Only list local channels OR channels that are on an instance followed by actorId
88 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
90 const whereActorAnd: WhereOptions[] = [
98 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
105 let serverRequired = false
106 let whereServer: WhereOptions
108 if (options.host && options.host !== WEBSERVER.HOST) {
109 serverRequired = true
110 whereServer = { host: options.host }
113 if (options.host === WEBSERVER.HOST) {
119 let rootWhere: WhereOptions
120 if (options.handles) {
121 const or: WhereOptions[] = []
123 for (const handle of options.handles || []) {
124 const [ preferredUsername, host ] = handle.split('@')
126 if (!host || host === WEBSERVER.HOST) {
128 '$Actor.preferredUsername$': preferredUsername,
129 '$Actor.serverId$': null
133 '$Actor.preferredUsername$': preferredUsername,
134 '$Actor.Server.host$': host
149 exclude: unusedActorAttributesForAPI
153 [Op.and]: whereActorAnd
158 required: serverRequired,
162 model: ActorImageModel,
167 model: ActorImageModel,
179 exclude: unusedActorAttributesForAPI
181 model: ActorModel, // Default scope includes avatar and server
189 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
190 const include: Includeable[] = [
192 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
193 model: ActorModel.unscoped(),
194 required: options.actorRequired ?? true,
197 attributes: [ 'host' ],
198 model: ServerModel.unscoped(),
202 model: ActorImageModel.unscoped(),
210 const base: FindOptions = {
211 attributes: [ 'id', 'name', 'description', 'actorId' ]
214 if (options.withAccount === true) {
216 model: AccountModel.scope({
217 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
223 base.include = include
227 [ScopeNames.WITH_ACCOUNT]: {
235 [ScopeNames.WITH_ACTOR]: {
240 [ScopeNames.WITH_ACTOR_BANNER]: {
246 model: ActorImageModel,
254 [ScopeNames.WITH_VIDEOS]: {
259 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
260 const daysPrior = parseInt(options.daysPrior + '', 10)
266 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
272 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
276 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
277 `date_trunc('day', now()), '1 day'::interval) AS day ` +
279 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
282 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
283 'AND "video"."channelId" = "VideoChannelModel"."id"' +
284 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
298 tableName: 'videoChannel',
300 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
303 fields: [ 'accountId' ]
306 fields: [ 'actorId' ]
310 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
313 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
319 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
320 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
325 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
326 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
335 @ForeignKey(() => ActorModel)
339 @BelongsTo(() => ActorModel, {
347 @ForeignKey(() => AccountModel)
351 @BelongsTo(() => AccountModel, {
356 Account: AccountModel
358 @HasMany(() => VideoModel, {
368 @HasMany(() => VideoPlaylistModel, {
375 VideoPlaylists: VideoPlaylistModel[]
378 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
379 if (!instance.Actor) {
380 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
383 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
385 if (instance.Actor.isOwned()) {
386 return sendDeleteActor(instance.Actor, options.transaction)
392 static countByAccount (accountId: number) {
399 return VideoChannelModel.count(query)
402 static async getStats () {
404 function getActiveVideoChannels (days: number) {
406 type: QueryTypes.SELECT as QueryTypes.SELECT,
411 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
412 FROM "videoChannel" AS "VideoChannelModel"
413 INNER JOIN "video" AS "Videos"
414 ON "VideoChannelModel"."id" = "Videos"."channelId"
415 AND ("Videos"."publishedAt" > Now() - interval '${days}d')
416 INNER JOIN "account" AS "Account"
417 ON "VideoChannelModel"."accountId" = "Account"."id"
418 INNER JOIN "actor" AS "Account->Actor"
419 ON "Account"."actorId" = "Account->Actor"."id"
420 AND "Account->Actor"."serverId" IS NULL
421 LEFT OUTER JOIN "server" AS "Account->Actor->Server"
422 ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
424 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
425 .then(r => parseInt(r[0].count, 10))
428 const totalLocalVideoChannels = await VideoChannelModel.count()
429 const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
430 const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
431 const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
432 const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)
435 totalLocalVideoChannels,
436 totalLocalDailyActiveVideoChannels,
437 totalLocalWeeklyActiveVideoChannels,
438 totalLocalMonthlyActiveVideoChannels,
439 totalHalfYearActiveVideoChannels
443 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
447 order: getSort(sort),
450 attributes: [ 'preferredUsername', 'serverId' ],
451 model: ActorModel.unscoped(),
459 return VideoChannelModel
464 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
469 const { actorId } = parameters
472 offset: parameters.start,
473 limit: parameters.count,
474 order: getSort(parameters.sort)
477 return VideoChannelModel
479 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
481 .findAndCountAll(query)
482 .then(({ rows, count }) => {
483 return { total: count, data: rows }
487 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
492 let attributesInclude: any[] = [ literal('0 as similarity') ]
493 let where: WhereOptions
495 if (options.search) {
496 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
497 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
498 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
503 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
506 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
514 include: attributesInclude
516 offset: options.start,
517 limit: options.count,
518 order: getSort(options.sort),
522 return VideoChannelModel
524 method: [ ScopeNames.FOR_API, pick(options, [ 'actorId', 'host', 'handles' ]) as AvailableForListOptions ]
526 .findAndCountAll(query)
527 .then(({ rows, count }) => {
528 return { total: count, data: rows }
532 static listByAccountForAPI (options: {
540 const escapedSearch = VideoModel.sequelize.escape(options.search)
541 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
542 const where = options.search
546 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
549 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
556 offset: options.start,
557 limit: options.count,
558 order: getSort(options.sort),
563 id: options.accountId
571 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
573 if (options.withStats === true) {
575 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
579 return VideoChannelModel
581 .findAndCountAll(query)
582 .then(({ rows, count }) => {
583 return { total: count, data: rows }
587 static listAllByAccount (accountId: number) {
589 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
602 return VideoChannelModel.findAll(query)
605 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
606 return VideoChannelModel.unscoped()
607 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
608 .findByPk(id, { transaction })
611 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
622 model: ActorImageModel,
631 return VideoChannelModel
632 .scope([ ScopeNames.WITH_ACCOUNT ])
636 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
637 const [ name, host ] = nameWithHost.split('@')
639 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
641 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
644 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
651 preferredUsername: name,
656 model: ActorImageModel,
665 return VideoChannelModel.unscoped()
666 .scope([ ScopeNames.WITH_ACCOUNT ])
670 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
677 preferredUsername: name
686 model: ActorImageModel,
695 return VideoChannelModel.unscoped()
696 .scope([ ScopeNames.WITH_ACCOUNT ])
700 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
701 const actor = this.Actor.toFormattedSummaryJSON()
706 displayName: this.getDisplayName(),
713 toFormattedJSON (this: MChannelFormattable): VideoChannel {
714 const viewsPerDayString = this.get('viewsPerDay') as string
715 const videosCount = this.get('videosCount') as number
717 let viewsPerDay: { date: Date, views: number }[]
719 if (viewsPerDayString) {
720 viewsPerDay = viewsPerDayString.split(',')
722 const [ dateString, amount ] = v.split('|')
725 date: new Date(dateString),
731 const actor = this.Actor.toFormattedJSON()
732 const videoChannel = {
734 displayName: this.getDisplayName(),
735 description: this.description,
736 support: this.support,
737 isLocal: this.Actor.isOwned(),
738 updatedAt: this.updatedAt,
739 ownerAccount: undefined,
744 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
746 return Object.assign(actor, videoChannel)
749 toActivityPubObject (this: MChannelAP): ActivityPubActor {
750 const obj = this.Actor.toActivityPubObject(this.name)
752 return Object.assign(obj, {
753 summary: this.description,
754 support: this.support,
757 type: 'Person' as 'Person',
758 id: this.Account.Actor.url
764 getLocalUrl (this: MAccountActor | MChannelActor) {
765 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
773 return this.Actor.isOutdated()
776 setAsUpdated (transaction?: Transaction) {
777 return setAsUpdated('videoChannel', this.id, transaction)