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 "actorFollow"."actorId" = ' + 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 listLocalsForSitemap (sort: string) {
240 order: getSort(sort),
243 attributes: [ 'preferredUsername', 'serverId' ],
244 model: ActorModel.unscoped(),
252 return VideoChannelModel
257 static searchForApi (options: {
264 const attributesInclude = []
265 const escapedSearch = VideoModel.sequelize.escape(options.search)
266 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
267 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
271 include: attributesInclude
273 offset: options.start,
274 limit: options.count,
275 order: getSort(options.sort),
279 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
282 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
289 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
291 return VideoChannelModel
293 .findAndCountAll(query)
294 .then(({ rows, count }) => {
295 return { total: count, data: rows }
299 static listByAccount (accountId: number) {
301 order: getSort('createdAt'),
313 return VideoChannelModel
314 .findAndCountAll(query)
315 .then(({ rows, count }) => {
316 return { total: count, data: rows }
320 static loadByIdAndPopulateAccount (id: number) {
321 return VideoChannelModel.unscoped()
322 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
326 static loadByIdAndAccount (id: number, accountId: number) {
334 return VideoChannelModel.unscoped()
335 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
339 static loadAndPopulateAccount (id: number) {
340 return VideoChannelModel.unscoped()
341 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
345 static loadByUUIDAndPopulateAccount (uuid: string) {
358 return VideoChannelModel
359 .scope([ ScopeNames.WITH_ACCOUNT ])
363 static loadByUrlAndPopulateAccount (url: string) {
376 return VideoChannelModel
377 .scope([ ScopeNames.WITH_ACCOUNT ])
381 static loadLocalByNameAndPopulateAccount (name: string) {
388 preferredUsername: name,
395 return VideoChannelModel.unscoped()
396 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
400 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
407 preferredUsername: name
420 return VideoChannelModel.unscoped()
421 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
425 static loadAndPopulateAccountAndVideos (id: number) {
432 return VideoChannelModel.unscoped()
433 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
434 .findById(id, options)
437 toFormattedJSON (): VideoChannel {
438 const actor = this.Actor.toFormattedJSON()
439 const videoChannel = {
441 displayName: this.getDisplayName(),
442 description: this.description,
443 support: this.support,
444 isLocal: this.Actor.isOwned(),
445 createdAt: this.createdAt,
446 updatedAt: this.updatedAt,
447 ownerAccount: undefined
450 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
452 return Object.assign(actor, videoChannel)
455 toActivityPubObject (): ActivityPubActor {
456 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
458 return Object.assign(obj, {
459 summary: this.description,
460 support: this.support,
463 type: 'Person' as 'Person',
464 id: this.Account.Actor.url