18 } from 'sequelize-typescript'
19 import { ActivityPubActor } from '../../../shared/models/activitypub'
20 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
22 isVideoChannelDescriptionValid,
23 isVideoChannelNameValid,
24 isVideoChannelSupportValid
25 } from '../../helpers/custom-validators/video-channels'
26 import { sendDeleteActor } from '../../lib/activitypub/send'
27 import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
28 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
29 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
30 import { VideoModel } from './video'
31 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
32 import { ServerModel } from '../server/server'
33 import { FindOptions, Op } from 'sequelize'
34 import { AvatarModel } from '../avatar/avatar'
35 import { VideoPlaylistModel } from './video-playlist'
36 import * as Bluebird from 'bluebird'
38 MChannelAccountDefault,
40 MChannelActorAccountDefaultVideos,
43 MChannelSummaryFormattable
44 } from '../../typings/models/video'
46 export enum ScopeNames {
48 WITH_ACCOUNT = 'WITH_ACCOUNT',
49 WITH_ACTOR = 'WITH_ACTOR',
50 WITH_VIDEOS = 'WITH_VIDEOS',
54 type AvailableForListOptions = {
58 export type SummaryOptions = {
59 withAccount?: boolean // Default: false
60 withAccountBlockerIds?: number[]
63 @DefaultScope(() => ({
72 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
73 const base: FindOptions = {
74 attributes: [ 'id', 'name', 'description', 'actorId' ],
77 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
78 model: ActorModel.unscoped(),
82 attributes: [ 'host' ],
83 model: ServerModel.unscoped(),
87 model: AvatarModel.unscoped(),
95 if (options.withAccount === true) {
97 model: AccountModel.scope({
98 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
106 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
107 // Only list local channels OR channels that are on an instance followed by actorId
108 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
114 exclude: unusedActorAttributesForAPI
124 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
136 exclude: unusedActorAttributesForAPI
138 model: ActorModel, // Default scope includes avatar and server
146 [ScopeNames.WITH_ACCOUNT]: {
154 [ScopeNames.WITH_VIDEOS]: {
159 [ScopeNames.WITH_ACTOR]: {
166 tableName: 'videoChannel',
168 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
171 fields: [ 'accountId' ]
174 fields: [ 'actorId' ]
178 export class VideoChannelModel extends Model<VideoChannelModel> {
181 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
187 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
188 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
193 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
194 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
203 @ForeignKey(() => ActorModel)
207 @BelongsTo(() => ActorModel, {
215 @ForeignKey(() => AccountModel)
219 @BelongsTo(() => AccountModel, {
225 Account: AccountModel
227 @HasMany(() => VideoModel, {
237 @HasMany(() => VideoPlaylistModel, {
244 VideoPlaylists: VideoPlaylistModel[]
247 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
248 if (!instance.Actor) {
249 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
252 if (instance.Actor.isOwned()) {
253 return sendDeleteActor(instance.Actor, options.transaction)
259 static countByAccount (accountId: number) {
266 return VideoChannelModel.count(query)
269 static listForApi (actorId: number, start: number, count: number, sort: string) {
277 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
279 return VideoChannelModel
281 .findAndCountAll(query)
282 .then(({ rows, count }) => {
283 return { total: count, data: rows }
287 static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> {
291 order: getSort(sort),
294 attributes: [ 'preferredUsername', 'serverId' ],
295 model: ActorModel.unscoped(),
303 return VideoChannelModel
308 static searchForApi (options: {
315 const attributesInclude = []
316 const escapedSearch = VideoModel.sequelize.escape(options.search)
317 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
318 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
322 include: attributesInclude
324 offset: options.start,
325 limit: options.count,
326 order: getSort(options.sort),
330 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
333 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
340 method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
342 return VideoChannelModel
344 .findAndCountAll(query)
345 .then(({ rows, count }) => {
346 return { total: count, data: rows }
350 static listByAccount (options: {
357 offset: options.start,
358 limit: options.count,
359 order: getSort(options.sort),
364 id: options.accountId
371 return VideoChannelModel
372 .findAndCountAll(query)
373 .then(({ rows, count }) => {
374 return { total: count, data: rows }
378 static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
379 return VideoChannelModel.unscoped()
380 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
384 static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelAccountDefault> {
392 return VideoChannelModel.unscoped()
393 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
397 static loadAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
398 return VideoChannelModel.unscoped()
399 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
403 static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> {
416 return VideoChannelModel
417 .scope([ ScopeNames.WITH_ACCOUNT ])
421 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
422 const [ name, host ] = nameWithHost.split('@')
424 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
426 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
429 static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelAccountDefault> {
436 preferredUsername: name,
443 return VideoChannelModel.unscoped()
444 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
448 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelAccountDefault> {
455 preferredUsername: name
468 return VideoChannelModel.unscoped()
469 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
473 static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> {
480 return VideoChannelModel.unscoped()
481 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
482 .findByPk(id, options)
485 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
486 const actor = this.Actor.toFormattedSummaryJSON()
491 displayName: this.getDisplayName(),
498 toFormattedJSON (this: MChannelFormattable): VideoChannel {
499 const actor = this.Actor.toFormattedJSON()
500 const videoChannel = {
502 displayName: this.getDisplayName(),
503 description: this.description,
504 support: this.support,
505 isLocal: this.Actor.isOwned(),
506 createdAt: this.createdAt,
507 updatedAt: this.updatedAt,
508 ownerAccount: undefined
511 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
513 return Object.assign(actor, videoChannel)
516 toActivityPubObject (this: MChannelAP): ActivityPubActor {
517 const obj = this.Actor.toActivityPubObject(this.name)
519 return Object.assign(obj, {
520 summary: this.description,
521 support: this.support,
524 type: 'Person' as 'Person',
525 id: this.Account.Actor.url
536 return this.Actor.isOutdated()