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 { AccountModel } from '../account/account'
26 import { ActorModel } from '../activitypub/actor'
27 import { getSort, throwIfNotValid } from '../utils'
28 import { VideoModel } from './video'
29 import { CONSTRAINTS_FIELDS } from '../../initializers'
30 import { AvatarModel } from '../avatar/avatar'
33 WITH_ACCOUNT = 'WITH_ACCOUNT',
34 WITH_ACTOR = 'WITH_ACTOR',
35 WITH_VIDEOS = 'WITH_VIDEOS'
41 model: () => ActorModel,
47 [ScopeNames.WITH_ACCOUNT]: {
50 model: () => AccountModel.unscoped(),
54 model: () => ActorModel.unscoped(),
58 model: () => AvatarModel.unscoped(),
67 [ScopeNames.WITH_VIDEOS]: {
72 [ScopeNames.WITH_ACTOR]: {
79 tableName: 'videoChannel',
82 fields: [ 'accountId' ]
89 export class VideoChannelModel extends Model<VideoChannelModel> {
92 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
98 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
99 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
104 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
105 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
114 @ForeignKey(() => ActorModel)
118 @BelongsTo(() => ActorModel, {
126 @ForeignKey(() => AccountModel)
130 @BelongsTo(() => AccountModel, {
136 Account: AccountModel
138 @HasMany(() => VideoModel, {
149 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
150 if (!instance.Actor) {
151 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
154 return instance.Actor.destroy({ transaction: options.transaction })
157 static countByAccount (accountId: number) {
164 return VideoChannelModel.count(query)
167 static listForApi (start: number, count: number, sort: string) {
174 return VideoChannelModel
175 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
176 .findAndCountAll(query)
177 .then(({ rows, count }) => {
178 return { total: count, data: rows }
182 static listByAccount (accountId: number) {
184 order: getSort('createdAt'),
196 return VideoChannelModel
197 .findAndCountAll(query)
198 .then(({ rows, count }) => {
199 return { total: count, data: rows }
203 static loadByIdAndAccount (id: number, accountId: number) {
211 return VideoChannelModel
212 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
216 static loadAndPopulateAccount (id: number) {
217 return VideoChannelModel
218 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
222 static loadByUUIDAndPopulateAccount (uuid: string) {
235 return VideoChannelModel
236 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
240 static loadAndPopulateAccountAndVideos (id: number) {
247 return VideoChannelModel
248 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
249 .findById(id, options)
252 toFormattedJSON (): VideoChannel {
253 const actor = this.Actor.toFormattedJSON()
254 const videoChannel = {
256 displayName: this.getDisplayName(),
257 description: this.description,
258 support: this.support,
259 isLocal: this.Actor.isOwned(),
260 createdAt: this.createdAt,
261 updatedAt: this.updatedAt,
262 ownerAccount: undefined,
266 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
268 return Object.assign(actor, videoChannel)
271 toActivityPubObject (): ActivityPubActor {
272 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
274 return Object.assign(obj, {
275 summary: this.description,
276 support: this.support,
279 type: 'Person' as 'Person',
280 id: this.Account.Actor.url