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, SummaryOptions as AccountSummaryOptions } 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 export type SummaryOptions = {
62 withAccount?: boolean // Default: false
63 withAccountBlockerIds?: number[]
66 @DefaultScope(() => ({
75 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
76 const base: FindOptions = {
77 attributes: [ 'name', 'description', 'id', 'actorId' ],
80 attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ],
81 model: ActorModel.unscoped(),
85 attributes: [ 'host' ],
86 model: ServerModel.unscoped(),
90 model: AvatarModel.unscoped(),
98 if (options.withAccount === true) {
100 model: AccountModel.scope({
101 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
109 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
110 // Only list local channels OR channels that are on an instance followed by actorId
111 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
117 exclude: unusedActorAttributesForAPI
127 [ Op.in ]: Sequelize.literal(inQueryInstanceFollow)
139 exclude: unusedActorAttributesForAPI
141 model: ActorModel, // Default scope includes avatar and server
149 [ScopeNames.WITH_ACCOUNT]: {
157 [ScopeNames.WITH_VIDEOS]: {
162 [ScopeNames.WITH_ACTOR]: {
169 tableName: 'videoChannel',
172 export class VideoChannelModel extends Model<VideoChannelModel> {
175 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
181 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
182 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
187 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
188 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
197 @ForeignKey(() => ActorModel)
201 @BelongsTo(() => ActorModel, {
209 @ForeignKey(() => AccountModel)
213 @BelongsTo(() => AccountModel, {
219 Account: AccountModel
221 @HasMany(() => VideoModel, {
231 @HasMany(() => VideoPlaylistModel, {
238 VideoPlaylists: VideoPlaylistModel[]
241 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
242 if (!instance.Actor) {
243 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
246 if (instance.Actor.isOwned()) {
247 return sendDeleteActor(instance.Actor, options.transaction)
253 static countByAccount (accountId: number) {
260 return VideoChannelModel.count(query)
263 static listForApi (actorId: number, start: number, count: number, sort: string) {
271 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ]
273 return VideoChannelModel
275 .findAndCountAll(query)
276 .then(({ rows, count }) => {
277 return { total: count, data: rows }
281 static listLocalsForSitemap (sort: string) {
285 order: getSort(sort),
288 attributes: [ 'preferredUsername', 'serverId' ],
289 model: ActorModel.unscoped(),
297 return VideoChannelModel
302 static searchForApi (options: {
309 const attributesInclude = []
310 const escapedSearch = VideoModel.sequelize.escape(options.search)
311 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
312 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
316 include: attributesInclude
318 offset: options.start,
319 limit: options.count,
320 order: getSort(options.sort),
324 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
327 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
334 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
336 return VideoChannelModel
338 .findAndCountAll(query)
339 .then(({ rows, count }) => {
340 return { total: count, data: rows }
344 static listByAccount (options: {
351 offset: options.start,
352 limit: options.count,
353 order: getSort(options.sort),
358 id: options.accountId
365 return VideoChannelModel
366 .findAndCountAll(query)
367 .then(({ rows, count }) => {
368 return { total: count, data: rows }
372 static loadByIdAndPopulateAccount (id: number) {
373 return VideoChannelModel.unscoped()
374 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
378 static loadByIdAndAccount (id: number, accountId: number) {
386 return VideoChannelModel.unscoped()
387 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
391 static loadAndPopulateAccount (id: number) {
392 return VideoChannelModel.unscoped()
393 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
397 static loadByUrlAndPopulateAccount (url: string) {
410 return VideoChannelModel
411 .scope([ ScopeNames.WITH_ACCOUNT ])
415 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
416 const [ name, host ] = nameWithHost.split('@')
418 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
420 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
423 static loadLocalByNameAndPopulateAccount (name: string) {
430 preferredUsername: name,
437 return VideoChannelModel.unscoped()
438 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
442 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
449 preferredUsername: name
462 return VideoChannelModel.unscoped()
463 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
467 static loadAndPopulateAccountAndVideos (id: number) {
474 return VideoChannelModel.unscoped()
475 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
476 .findByPk(id, options)
479 toFormattedJSON (): VideoChannel {
480 const actor = this.Actor.toFormattedJSON()
481 const videoChannel = {
483 displayName: this.getDisplayName(),
484 description: this.description,
485 support: this.support,
486 isLocal: this.Actor.isOwned(),
487 createdAt: this.createdAt,
488 updatedAt: this.updatedAt,
489 ownerAccount: undefined
492 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
494 return Object.assign(actor, videoChannel)
497 toFormattedSummaryJSON (): VideoChannelSummary {
498 const actor = this.Actor.toFormattedJSON()
503 displayName: this.getDisplayName(),
510 toActivityPubObject (): ActivityPubActor {
511 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
513 return Object.assign(obj, {
514 summary: this.description,
515 support: this.support,
518 type: 'Person' as 'Person',
519 id: this.Account.Actor.url
530 return this.Actor.isOutdated()