17 } from 'sequelize-typescript'
18 import { ActivityPubActor } from '../../../shared/models/activitypub'
19 import { VideoChannel } from '../../../shared/models/videos'
21 isVideoChannelDescriptionValid,
22 isVideoChannelNameValid,
23 isVideoChannelSupportValid
24 } from '../../helpers/custom-validators/video-channels'
25 import { sendDeleteActor } from '../../lib/activitypub/send'
26 import { AccountModel } from '../account/account'
27 import { ActorModel } from '../activitypub/actor'
28 import { getSort, throwIfNotValid } from '../utils'
29 import { VideoModel } from './video'
30 import { CONSTRAINTS_FIELDS } from '../../initializers'
31 import { AvatarModel } from '../avatar/avatar'
34 WITH_ACCOUNT = 'WITH_ACCOUNT',
35 WITH_ACTOR = 'WITH_ACTOR',
36 WITH_VIDEOS = 'WITH_VIDEOS'
42 model: () => ActorModel,
48 [ScopeNames.WITH_ACCOUNT]: {
51 model: () => AccountModel.unscoped(),
55 model: () => ActorModel.unscoped(),
59 model: () => AvatarModel.unscoped(),
68 [ScopeNames.WITH_VIDEOS]: {
73 [ScopeNames.WITH_ACTOR]: {
80 tableName: 'videoChannel',
83 fields: [ 'accountId' ]
90 export class VideoChannelModel extends Model<VideoChannelModel> {
93 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
99 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
100 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
105 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
106 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
115 @ForeignKey(() => ActorModel)
119 @BelongsTo(() => ActorModel, {
127 @ForeignKey(() => AccountModel)
131 @BelongsTo(() => AccountModel, {
137 Account: AccountModel
139 @HasMany(() => VideoModel, {
150 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
151 if (!instance.Actor) {
152 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
155 if (instance.Actor.isOwned()) {
156 return sendDeleteActor(instance.Actor, options.transaction)
162 static countByAccount (accountId: number) {
169 return VideoChannelModel.count(query)
172 static listForApi (start: number, count: number, sort: string) {
179 return VideoChannelModel
180 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
181 .findAndCountAll(query)
182 .then(({ rows, count }) => {
183 return { total: count, data: rows }
187 static listByAccount (accountId: number) {
189 order: getSort('createdAt'),
201 return VideoChannelModel
202 .findAndCountAll(query)
203 .then(({ rows, count }) => {
204 return { total: count, data: rows }
208 static loadByIdAndAccount (id: number, accountId: number) {
216 return VideoChannelModel
217 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
221 static loadAndPopulateAccount (id: number) {
222 return VideoChannelModel
223 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
227 static loadByUUIDAndPopulateAccount (uuid: string) {
240 return VideoChannelModel
241 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
245 static loadAndPopulateAccountAndVideos (id: number) {
252 return VideoChannelModel
253 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
254 .findById(id, options)
257 static loadLocalByName (name: string) {
264 preferredUsername: name,
271 return VideoChannelModel.findOne(query)
274 toFormattedJSON (): VideoChannel {
275 const actor = this.Actor.toFormattedJSON()
276 const videoChannel = {
278 displayName: this.getDisplayName(),
279 description: this.description,
280 support: this.support,
281 isLocal: this.Actor.isOwned(),
282 createdAt: this.createdAt,
283 updatedAt: this.updatedAt,
284 ownerAccount: undefined
287 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
289 return Object.assign(actor, videoChannel)
292 toActivityPubObject (): ActivityPubActor {
293 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
295 return Object.assign(obj, {
296 summary: this.description,
297 support: this.support,
300 type: 'Person' as 'Person',
301 id: this.Account.Actor.url