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'
47 import { CONFIG } from '@server/initializers/config'
49 export enum ScopeNames {
52 WITH_ACCOUNT = 'WITH_ACCOUNT',
53 WITH_ACTOR = 'WITH_ACTOR',
54 WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
55 WITH_VIDEOS = 'WITH_VIDEOS',
56 WITH_STATS = 'WITH_STATS'
59 type AvailableForListOptions = {
66 type AvailableWithStatsOptions = {
70 export type SummaryOptions = {
71 actorRequired?: boolean // Default: true
72 withAccount?: boolean // Default: false
73 withAccountBlockerIds?: number[]
76 @DefaultScope(() => ({
85 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
86 // Only list local channels OR channels that are on an instance followed by actorId
87 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
89 const whereActorAnd: WhereOptions[] = [
97 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
104 let serverRequired = false
105 let whereServer: WhereOptions
107 if (options.host && options.host !== WEBSERVER.HOST) {
108 serverRequired = true
109 whereServer = { host: options.host }
112 if (options.host === WEBSERVER.HOST) {
118 let rootWhere: WhereOptions
119 if (options.handles) {
120 const or: WhereOptions[] = []
122 for (const handle of options.handles || []) {
123 const [ preferredUsername, host ] = handle.split('@')
125 if (!host || host === WEBSERVER.HOST) {
127 '$Actor.preferredUsername$': preferredUsername,
128 '$Actor.serverId$': null
132 '$Actor.preferredUsername$': preferredUsername,
133 '$Actor.Server.host$': host
148 exclude: unusedActorAttributesForAPI
152 [Op.and]: whereActorAnd
157 required: serverRequired,
161 model: ActorImageModel,
166 model: ActorImageModel,
178 exclude: unusedActorAttributesForAPI
180 model: ActorModel, // Default scope includes avatar and server
188 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
189 const include: Includeable[] = [
191 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
192 model: ActorModel.unscoped(),
193 required: options.actorRequired ?? true,
196 attributes: [ 'host' ],
197 model: ServerModel.unscoped(),
201 model: ActorImageModel.unscoped(),
209 const base: FindOptions = {
210 attributes: [ 'id', 'name', 'description', 'actorId' ]
213 if (options.withAccount === true) {
215 model: AccountModel.scope({
216 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
222 base.include = include
226 [ScopeNames.WITH_ACCOUNT]: {
234 [ScopeNames.WITH_ACTOR]: {
239 [ScopeNames.WITH_ACTOR_BANNER]: {
245 model: ActorImageModel,
253 [ScopeNames.WITH_VIDEOS]: {
258 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
259 const daysPrior = parseInt(options.daysPrior + '', 10)
265 literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
271 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
275 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
276 `date_trunc('day', now()), '1 day'::interval) AS day ` +
278 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
281 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
282 'AND "video"."channelId" = "VideoChannelModel"."id"' +
283 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
297 tableName: 'videoChannel',
299 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
302 fields: [ 'accountId' ]
305 fields: [ 'actorId' ]
309 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
312 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
318 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
319 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
324 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
325 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
334 @ForeignKey(() => ActorModel)
338 @BelongsTo(() => ActorModel, {
346 @ForeignKey(() => AccountModel)
350 @BelongsTo(() => AccountModel, {
355 Account: AccountModel
357 @HasMany(() => VideoModel, {
367 @HasMany(() => VideoPlaylistModel, {
374 VideoPlaylists: VideoPlaylistModel[]
377 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
378 if (!instance.Actor) {
379 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
382 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
384 if (instance.Actor.isOwned()) {
385 return sendDeleteActor(instance.Actor, options.transaction)
391 static countByAccount (accountId: number) {
398 return VideoChannelModel.count(query)
401 static async getStats () {
403 function getActiveVideoChannels (days: number) {
405 type: QueryTypes.SELECT as QueryTypes.SELECT,
410 SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
411 FROM "videoChannel" AS "VideoChannelModel"
412 INNER JOIN "video" AS "Videos"
413 ON "VideoChannelModel"."id" = "Videos"."channelId"
414 AND ("Videos"."publishedAt" > Now() - interval '${days}d')
415 INNER JOIN "account" AS "Account"
416 ON "VideoChannelModel"."accountId" = "Account"."id"
417 INNER JOIN "actor" AS "Account->Actor"
418 ON "Account"."actorId" = "Account->Actor"."id"
419 AND "Account->Actor"."serverId" IS NULL
420 LEFT OUTER JOIN "server" AS "Account->Actor->Server"
421 ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
423 return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
424 .then(r => parseInt(r[0].count, 10))
427 const totalLocalVideoChannels = await VideoChannelModel.count()
428 const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
429 const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
430 const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
431 const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)
434 totalLocalVideoChannels,
435 totalLocalDailyActiveVideoChannels,
436 totalLocalWeeklyActiveVideoChannels,
437 totalLocalMonthlyActiveVideoChannels,
438 totalHalfYearActiveVideoChannels
442 static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
446 order: getSort(sort),
449 attributes: [ 'preferredUsername', 'serverId' ],
450 model: ActorModel.unscoped(),
458 return VideoChannelModel
463 static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
468 const { actorId } = parameters
471 offset: parameters.start,
472 limit: parameters.count,
473 order: getSort(parameters.sort)
476 return VideoChannelModel
478 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
480 .findAndCountAll(query)
481 .then(({ rows, count }) => {
482 return { total: count, data: rows }
486 static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
491 let attributesInclude: any[] = [ literal('0 as similarity') ]
492 let where: WhereOptions
494 if (options.search) {
495 const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
496 const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
497 attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
502 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
505 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
513 include: attributesInclude
515 offset: options.start,
516 limit: options.count,
517 order: getSort(options.sort),
521 return VideoChannelModel
523 method: [ ScopeNames.FOR_API, pick(options, [ 'actorId', 'host', 'handles' ]) as AvailableForListOptions ]
525 .findAndCountAll(query)
526 .then(({ rows, count }) => {
527 return { total: count, data: rows }
531 static listByAccountForAPI (options: {
539 const escapedSearch = VideoModel.sequelize.escape(options.search)
540 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
541 const where = options.search
545 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
548 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
555 offset: options.start,
556 limit: options.count,
557 order: getSort(options.sort),
562 id: options.accountId
570 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
572 if (options.withStats === true) {
574 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
578 return VideoChannelModel
580 .findAndCountAll(query)
581 .then(({ rows, count }) => {
582 return { total: count, data: rows }
586 static listAllByAccount (accountId: number) {
588 limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
601 return VideoChannelModel.findAll(query)
604 static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
605 return VideoChannelModel.unscoped()
606 .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
607 .findByPk(id, { transaction })
610 static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
621 model: ActorImageModel,
630 return VideoChannelModel
631 .scope([ ScopeNames.WITH_ACCOUNT ])
635 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
636 const [ name, host ] = nameWithHost.split('@')
638 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
640 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
643 static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
650 preferredUsername: name,
655 model: ActorImageModel,
664 return VideoChannelModel.unscoped()
665 .scope([ ScopeNames.WITH_ACCOUNT ])
669 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
676 preferredUsername: name
685 model: ActorImageModel,
694 return VideoChannelModel.unscoped()
695 .scope([ ScopeNames.WITH_ACCOUNT ])
699 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
700 const actor = this.Actor.toFormattedSummaryJSON()
705 displayName: this.getDisplayName(),
712 toFormattedJSON (this: MChannelFormattable): VideoChannel {
713 const viewsPerDayString = this.get('viewsPerDay') as string
714 const videosCount = this.get('videosCount') as number
716 let viewsPerDay: { date: Date, views: number }[]
718 if (viewsPerDayString) {
719 viewsPerDay = viewsPerDayString.split(',')
721 const [ dateString, amount ] = v.split('|')
724 date: new Date(dateString),
730 const actor = this.Actor.toFormattedJSON()
731 const videoChannel = {
733 displayName: this.getDisplayName(),
734 description: this.description,
735 support: this.support,
736 isLocal: this.Actor.isOwned(),
737 updatedAt: this.updatedAt,
738 ownerAccount: undefined,
743 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
745 return Object.assign(actor, videoChannel)
748 toActivityPubObject (this: MChannelAP): ActivityPubActor {
749 const obj = this.Actor.toActivityPubObject(this.name)
751 return Object.assign(obj, {
752 summary: this.description,
753 support: this.support,
756 type: 'Person' as 'Person',
757 id: this.Account.Actor.url
763 getLocalUrl (this: MAccountActor | MChannelActor) {
764 return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
772 return this.Actor.isOutdated()
775 setAsUpdated (transaction?: Transaction) {
776 return setAsUpdated('videoChannel', this.id, transaction)