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 "actor" ' +
73 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = actor.id ' +
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 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),
257 [ Sequelize.Op.in ]: Sequelize.literal(
259 'SELECT id FROM "videoChannel" WHERE ' +
260 'lower(immutable_unaccent("name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
261 'lower(immutable_unaccent("name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
269 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
271 return VideoChannelModel
273 .findAndCountAll(query)
274 .then(({ rows, count }) => {
275 return { total: count, data: rows }
279 static listByAccount (accountId: number) {
281 order: getSort('createdAt'),
293 return VideoChannelModel
294 .findAndCountAll(query)
295 .then(({ rows, count }) => {
296 return { total: count, data: rows }
300 static loadByIdAndAccount (id: number, accountId: number) {
308 return VideoChannelModel
309 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
313 static loadAndPopulateAccount (id: number) {
314 return VideoChannelModel
315 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
319 static loadByUUIDAndPopulateAccount (uuid: string) {
332 return VideoChannelModel
333 .scope([ ScopeNames.WITH_ACCOUNT ])
337 static loadByUrlAndPopulateAccount (url: string) {
350 return VideoChannelModel
351 .scope([ ScopeNames.WITH_ACCOUNT ])
355 static loadLocalByNameAndPopulateAccount (name: string) {
362 preferredUsername: name,
369 return VideoChannelModel
370 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
374 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
381 preferredUsername: name
394 return VideoChannelModel
395 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
399 static loadAndPopulateAccountAndVideos (id: number) {
406 return VideoChannelModel
407 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
408 .findById(id, options)
411 toFormattedJSON (): VideoChannel {
412 const actor = this.Actor.toFormattedJSON()
413 const videoChannel = {
415 displayName: this.getDisplayName(),
416 description: this.description,
417 support: this.support,
418 isLocal: this.Actor.isOwned(),
419 createdAt: this.createdAt,
420 updatedAt: this.updatedAt,
421 ownerAccount: undefined
424 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
426 return Object.assign(actor, videoChannel)
429 toActivityPubObject (): ActivityPubActor {
430 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
432 return Object.assign(obj, {
433 summary: this.description,
434 support: this.support,
437 type: 'Person' as 'Person',
438 id: this.Account.Actor.url