18 } from 'sequelize-typescript'
19 import { ActivityPubActor } from '../../../shared/models/activitypub'
20 import { VideoChannel } 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 } from '../account/account'
28 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
29 import { buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
30 import { VideoModel } from './video'
31 import { CONSTRAINTS_FIELDS } from '../../initializers'
32 import { ServerModel } from '../server/server'
33 import { DefineIndexesOptions } from 'sequelize'
35 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
36 const indexes: DefineIndexesOptions[] = [
37 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
40 fields: [ 'accountId' ]
48 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
49 WITH_ACCOUNT = 'WITH_ACCOUNT',
50 WITH_ACTOR = 'WITH_ACTOR',
51 WITH_VIDEOS = 'WITH_VIDEOS'
54 type AvailableForListOptions = {
61 model: () => ActorModel,
67 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
68 const actorIdNumber = parseInt(options.actorId + '', 10)
70 // Only list local channels OR channels that are on an instance followed by actorId
71 const inQueryInstanceFollow = '(' +
72 'SELECT "actor"."serverId" FROM "actorFollow" ' +
73 'INNER JOIN "actor" ON actor.id= "actorFollow"."targetActorId" ' +
74 'WHERE "actor"."id" = ' + actorIdNumber +
81 exclude: unusedActorAttributesForAPI
91 [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow)
103 exclude: unusedActorAttributesForAPI
105 model: ActorModel, // Default scope includes avatar and server
113 [ScopeNames.WITH_ACCOUNT]: {
116 model: () => AccountModel,
121 [ScopeNames.WITH_VIDEOS]: {
126 [ScopeNames.WITH_ACTOR]: {
133 tableName: 'videoChannel',
136 export class VideoChannelModel extends Model<VideoChannelModel> {
139 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
145 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
146 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
151 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
152 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
161 @ForeignKey(() => ActorModel)
165 @BelongsTo(() => ActorModel, {
173 @ForeignKey(() => AccountModel)
177 @BelongsTo(() => AccountModel, {
183 Account: AccountModel
185 @HasMany(() => VideoModel, {
196 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
197 if (!instance.Actor) {
198 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
201 if (instance.Actor.isOwned()) {
202 return sendDeleteActor(instance.Actor, options.transaction)
208 static countByAccount (accountId: number) {
215 return VideoChannelModel.count(query)
218 static listForApi (actorId: number, start: number, count: number, sort: string) {
226 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ]
228 return VideoChannelModel
230 .findAndCountAll(query)
231 .then(({ rows, count }) => {
232 return { total: count, data: rows }
236 static searchForApi (options: {
243 const attributesInclude = []
244 const escapedSearch = VideoModel.sequelize.escape(options.search)
245 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
246 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
250 include: attributesInclude
252 offset: options.start,
253 limit: options.count,
254 order: getSort(options.sort),
258 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
261 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
268 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
270 return VideoChannelModel
272 .findAndCountAll(query)
273 .then(({ rows, count }) => {
274 return { total: count, data: rows }
278 static listByAccount (accountId: number) {
280 order: getSort('createdAt'),
292 return VideoChannelModel
293 .findAndCountAll(query)
294 .then(({ rows, count }) => {
295 return { total: count, data: rows }
299 static loadByIdAndAccount (id: number, accountId: number) {
307 return VideoChannelModel
308 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
312 static loadAndPopulateAccount (id: number) {
313 return VideoChannelModel
314 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
318 static loadByUUIDAndPopulateAccount (uuid: string) {
331 return VideoChannelModel
332 .scope([ ScopeNames.WITH_ACCOUNT ])
336 static loadByUrlAndPopulateAccount (url: string) {
349 return VideoChannelModel
350 .scope([ ScopeNames.WITH_ACCOUNT ])
354 static loadLocalByNameAndPopulateAccount (name: string) {
361 preferredUsername: name,
368 return VideoChannelModel
369 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
373 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
380 preferredUsername: name
393 return VideoChannelModel
394 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
398 static loadAndPopulateAccountAndVideos (id: number) {
405 return VideoChannelModel
406 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
407 .findById(id, options)
410 toFormattedJSON (): VideoChannel {
411 const actor = this.Actor.toFormattedJSON()
412 const videoChannel = {
414 displayName: this.getDisplayName(),
415 description: this.description,
416 support: this.support,
417 isLocal: this.Actor.isOwned(),
418 createdAt: this.createdAt,
419 updatedAt: this.updatedAt,
420 ownerAccount: undefined
423 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
425 return Object.assign(actor, videoChannel)
428 toActivityPubObject (): ActivityPubActor {
429 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
431 return Object.assign(obj, {
432 summary: this.description,
433 support: this.support,
436 type: 'Person' as 'Person',
437 id: this.Account.Actor.url