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'
32 import { ServerModel } from '../server/server'
35 WITH_ACCOUNT = 'WITH_ACCOUNT',
36 WITH_ACTOR = 'WITH_ACTOR',
37 WITH_VIDEOS = 'WITH_VIDEOS'
43 model: () => ActorModel,
49 [ScopeNames.WITH_ACCOUNT]: {
52 model: () => AccountModel.unscoped(),
56 model: () => ActorModel.unscoped(),
60 model: () => AvatarModel.unscoped(),
69 [ScopeNames.WITH_VIDEOS]: {
74 [ScopeNames.WITH_ACTOR]: {
81 tableName: 'videoChannel',
84 fields: [ 'accountId' ]
91 export class VideoChannelModel extends Model<VideoChannelModel> {
94 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
100 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
101 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
106 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
107 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
116 @ForeignKey(() => ActorModel)
120 @BelongsTo(() => ActorModel, {
128 @ForeignKey(() => AccountModel)
132 @BelongsTo(() => AccountModel, {
138 Account: AccountModel
140 @HasMany(() => VideoModel, {
151 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
152 if (!instance.Actor) {
153 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
156 if (instance.Actor.isOwned()) {
157 return sendDeleteActor(instance.Actor, options.transaction)
163 static countByAccount (accountId: number) {
170 return VideoChannelModel.count(query)
173 static listForApi (start: number, count: number, sort: string) {
180 return VideoChannelModel
181 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
182 .findAndCountAll(query)
183 .then(({ rows, count }) => {
184 return { total: count, data: rows }
188 static listByAccount (accountId: number) {
190 order: getSort('createdAt'),
202 return VideoChannelModel
203 .findAndCountAll(query)
204 .then(({ rows, count }) => {
205 return { total: count, data: rows }
209 static loadByIdAndAccount (id: number, accountId: number) {
217 return VideoChannelModel
218 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
222 static loadAndPopulateAccount (id: number) {
223 return VideoChannelModel
224 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
228 static loadByUUIDAndPopulateAccount (uuid: string) {
241 return VideoChannelModel
242 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
246 static loadLocalByNameAndPopulateAccount (name: string) {
253 preferredUsername: name,
260 return VideoChannelModel
261 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
265 static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
272 preferredUsername: name
285 return VideoChannelModel
286 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
290 static loadAndPopulateAccountAndVideos (id: number) {
297 return VideoChannelModel
298 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
299 .findById(id, options)
302 toFormattedJSON (): VideoChannel {
303 const actor = this.Actor.toFormattedJSON()
304 const videoChannel = {
306 displayName: this.getDisplayName(),
307 description: this.description,
308 support: this.support,
309 isLocal: this.Actor.isOwned(),
310 createdAt: this.createdAt,
311 updatedAt: this.updatedAt,
312 ownerAccount: undefined
315 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
317 return Object.assign(actor, videoChannel)
320 toActivityPubObject (): ActivityPubActor {
321 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
323 return Object.assign(obj, {
324 summary: this.description,
325 support: this.support,
328 type: 'Person' as 'Person',
329 id: this.Account.Actor.url