19 } from 'sequelize-typescript'
20 import { ActivityPubActor } from '../../../shared/models/activitypub'
21 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
23 isVideoChannelDescriptionValid,
24 isVideoChannelNameValid,
25 isVideoChannelSupportValid
26 } from '../../helpers/custom-validators/video-channels'
27 import { sendDeleteActor } from '../../lib/activitypub/send'
28 import { AccountModel, ScopeNames as AccountModelScopeNames } from '../account/account'
29 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
30 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
31 import { VideoModel } from './video'
32 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers'
33 import { ServerModel } from '../server/server'
34 import { DefineIndexesOptions } from 'sequelize'
35 import { AvatarModel } from '../avatar/avatar'
36 import { VideoPlaylistModel } from './video-playlist'
38 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
39 const indexes: DefineIndexesOptions[] = [
40 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
43 fields: [ 'accountId' ]
50 export enum ScopeNames {
51 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
52 WITH_ACCOUNT = 'WITH_ACCOUNT',
53 WITH_ACTOR = 'WITH_ACTOR',
54 WITH_VIDEOS = 'WITH_VIDEOS',
58 type AvailableForListOptions = {
65 model: () => ActorModel,
71 [ScopeNames.SUMMARY]: (withAccount = false) => {
72 const base: IFindOptions<VideoChannelModel> = {
73 attributes: [ 'name', 'description', 'id', 'actorId' ],
76 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
77 model: ActorModel.unscoped(),
81 attributes: [ 'host' ],
82 model: ServerModel.unscoped(),
86 model: AvatarModel.unscoped(),
94 if (withAccount === true) {
96 model: AccountModel.scope(AccountModelScopeNames.SUMMARY),
103 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
104 // Only list local channels OR channels that are on an instance followed by actorId
105 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
111 exclude: unusedActorAttributesForAPI
121 [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow)
133 exclude: unusedActorAttributesForAPI
135 model: ActorModel, // Default scope includes avatar and server
143 [ScopeNames.WITH_ACCOUNT]: {
146 model: () => AccountModel,
151 [ScopeNames.WITH_VIDEOS]: {
156 [ScopeNames.WITH_ACTOR]: {
163 tableName: 'videoChannel',
166 export class VideoChannelModel extends Model<VideoChannelModel> {
169 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
175 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
176 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
181 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
182 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
191 @ForeignKey(() => ActorModel)
195 @BelongsTo(() => ActorModel, {
203 @ForeignKey(() => AccountModel)
207 @BelongsTo(() => AccountModel, {
213 Account: AccountModel
215 @HasMany(() => VideoModel, {
225 @HasMany(() => VideoPlaylistModel, {
232 VideoPlaylists: VideoPlaylistModel[]
235 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
236 if (!instance.Actor) {
237 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
240 if (instance.Actor.isOwned()) {
241 return sendDeleteActor(instance.Actor, options.transaction)
247 static countByAccount (accountId: number) {
254 return VideoChannelModel.count(query)
257 static listForApi (actorId: number, start: number, count: number, sort: string) {
265 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ]
267 return VideoChannelModel
269 .findAndCountAll(query)
270 .then(({ rows, count }) => {
271 return { total: count, data: rows }
275 static listLocalsForSitemap (sort: string) {
279 order: getSort(sort),
282 attributes: [ 'preferredUsername', 'serverId' ],
283 model: ActorModel.unscoped(),
291 return VideoChannelModel
296 static searchForApi (options: {
303 const attributesInclude = []
304 const escapedSearch = VideoModel.sequelize.escape(options.search)
305 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
306 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
310 include: attributesInclude
312 offset: options.start,
313 limit: options.count,
314 order: getSort(options.sort),
318 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
321 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
328 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
330 return VideoChannelModel
332 .findAndCountAll(query)
333 .then(({ rows, count }) => {
334 return { total: count, data: rows }
338 static listByAccount (accountId: number) {
340 order: getSort('createdAt'),
352 return VideoChannelModel
353 .findAndCountAll(query)
354 .then(({ rows, count }) => {
355 return { total: count, data: rows }
359 static loadByIdAndPopulateAccount (id: number) {
360 return VideoChannelModel.unscoped()
361 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
365 static loadByIdAndAccount (id: number, accountId: number) {
373 return VideoChannelModel.unscoped()
374 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
378 static loadAndPopulateAccount (id: number) {
379 return VideoChannelModel.unscoped()
380 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
384 static loadByUUIDAndPopulateAccount (uuid: string) {
397 return VideoChannelModel
398 .scope([ ScopeNames.WITH_ACCOUNT ])
402 static loadByUrlAndPopulateAccount (url: string) {
415 return VideoChannelModel
416 .scope([ ScopeNames.WITH_ACCOUNT ])
420 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
421 const [ name, host ] = nameWithHost.split('@')
423 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
425 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
428 static loadLocalByNameAndPopulateAccount (name: string) {
435 preferredUsername: name,
442 return VideoChannelModel.unscoped()
443 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
447 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
454 preferredUsername: name
467 return VideoChannelModel.unscoped()
468 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
472 static loadAndPopulateAccountAndVideos (id: number) {
479 return VideoChannelModel.unscoped()
480 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
481 .findByPk(id, options)
484 toFormattedJSON (): VideoChannel {
485 const actor = this.Actor.toFormattedJSON()
486 const videoChannel = {
488 displayName: this.getDisplayName(),
489 description: this.description,
490 support: this.support,
491 isLocal: this.Actor.isOwned(),
492 createdAt: this.createdAt,
493 updatedAt: this.updatedAt,
494 ownerAccount: undefined
497 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
499 return Object.assign(actor, videoChannel)
502 toFormattedSummaryJSON (): VideoChannelSummary {
503 const actor = this.Actor.toFormattedJSON()
509 displayName: this.getDisplayName(),
516 toActivityPubObject (): ActivityPubActor {
517 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
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()