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 } 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, ModelIndexesOptions, Op } from 'sequelize'
34 import { AvatarModel } from '../avatar/avatar'
35 import { VideoPlaylistModel } from './video-playlist'
37 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
38 const indexes: ModelIndexesOptions[] = [
39 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
42 fields: [ 'accountId' ]
49 export enum ScopeNames {
50 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
51 WITH_ACCOUNT = 'WITH_ACCOUNT',
52 WITH_ACTOR = 'WITH_ACTOR',
53 WITH_VIDEOS = 'WITH_VIDEOS',
57 type AvailableForListOptions = {
61 @DefaultScope(() => ({
70 [ScopeNames.SUMMARY]: (withAccount = false) => {
71 const base: FindOptions = {
72 attributes: [ 'name', 'description', 'id', 'actorId' ],
75 attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ],
76 model: ActorModel.unscoped(),
80 attributes: [ 'host' ],
81 model: ServerModel.unscoped(),
85 model: AvatarModel.unscoped(),
93 if (withAccount === true) {
95 model: AccountModel.scope(AccountModelScopeNames.SUMMARY),
102 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
103 // Only list local channels OR channels that are on an instance followed by actorId
104 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
110 exclude: unusedActorAttributesForAPI
120 [ Op.in ]: Sequelize.literal(inQueryInstanceFollow)
132 exclude: unusedActorAttributesForAPI
134 model: ActorModel, // Default scope includes avatar and server
142 [ScopeNames.WITH_ACCOUNT]: {
150 [ScopeNames.WITH_VIDEOS]: {
155 [ScopeNames.WITH_ACTOR]: {
162 tableName: 'videoChannel',
165 export class VideoChannelModel extends Model<VideoChannelModel> {
168 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
174 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
180 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
181 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
190 @ForeignKey(() => ActorModel)
194 @BelongsTo(() => ActorModel, {
202 @ForeignKey(() => AccountModel)
206 @BelongsTo(() => AccountModel, {
212 Account: AccountModel
214 @HasMany(() => VideoModel, {
224 @HasMany(() => VideoPlaylistModel, {
231 VideoPlaylists: VideoPlaylistModel[]
234 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
235 if (!instance.Actor) {
236 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
239 if (instance.Actor.isOwned()) {
240 return sendDeleteActor(instance.Actor, options.transaction)
246 static countByAccount (accountId: number) {
253 return VideoChannelModel.count(query)
256 static listForApi (actorId: number, start: number, count: number, sort: string) {
264 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ]
266 return VideoChannelModel
268 .findAndCountAll(query)
269 .then(({ rows, count }) => {
270 return { total: count, data: rows }
274 static listLocalsForSitemap (sort: string) {
278 order: getSort(sort),
281 attributes: [ 'preferredUsername', 'serverId' ],
282 model: ActorModel.unscoped(),
290 return VideoChannelModel
295 static searchForApi (options: {
302 const attributesInclude = []
303 const escapedSearch = VideoModel.sequelize.escape(options.search)
304 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
305 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
309 include: attributesInclude
311 offset: options.start,
312 limit: options.count,
313 order: getSort(options.sort),
317 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
320 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
327 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
329 return VideoChannelModel
331 .findAndCountAll(query)
332 .then(({ rows, count }) => {
333 return { total: count, data: rows }
337 static listByAccount (options: {
344 offset: options.start,
345 limit: options.count,
346 order: getSort(options.sort),
351 id: options.accountId
358 return VideoChannelModel
359 .findAndCountAll(query)
360 .then(({ rows, count }) => {
361 return { total: count, data: rows }
365 static loadByIdAndPopulateAccount (id: number) {
366 return VideoChannelModel.unscoped()
367 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
371 static loadByIdAndAccount (id: number, accountId: number) {
379 return VideoChannelModel.unscoped()
380 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
384 static loadAndPopulateAccount (id: number) {
385 return VideoChannelModel.unscoped()
386 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
390 static loadByUrlAndPopulateAccount (url: string) {
403 return VideoChannelModel
404 .scope([ ScopeNames.WITH_ACCOUNT ])
408 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
409 const [ name, host ] = nameWithHost.split('@')
411 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
413 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
416 static loadLocalByNameAndPopulateAccount (name: string) {
423 preferredUsername: name,
430 return VideoChannelModel.unscoped()
431 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
435 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
442 preferredUsername: name
455 return VideoChannelModel.unscoped()
456 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
460 static loadAndPopulateAccountAndVideos (id: number) {
467 return VideoChannelModel.unscoped()
468 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
469 .findByPk(id, options)
472 toFormattedJSON (): VideoChannel {
473 const actor = this.Actor.toFormattedJSON()
474 const videoChannel = {
476 displayName: this.getDisplayName(),
477 description: this.description,
478 support: this.support,
479 isLocal: this.Actor.isOwned(),
480 createdAt: this.createdAt,
481 updatedAt: this.updatedAt,
482 ownerAccount: undefined
485 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
487 return Object.assign(actor, videoChannel)
490 toFormattedSummaryJSON (): VideoChannelSummary {
491 const actor = this.Actor.toFormattedJSON()
496 displayName: this.getDisplayName(),
503 toActivityPubObject (): ActivityPubActor {
504 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
506 return Object.assign(obj, {
507 summary: this.description,
508 support: this.support,
511 type: 'Person' as 'Person',
512 id: this.Account.Actor.url
523 return this.Actor.isOutdated()