15 } from 'sequelize-typescript'
16 import { ActivityPubActor } from '../../../shared/models/activitypub'
17 import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
18 import { sendDeleteActor } from '../../lib/activitypub/send'
19 import { AccountModel } from '../account/account'
20 import { ActorModel } from '../activitypub/actor'
21 import { getSort, throwIfNotValid } from '../utils'
22 import { VideoModel } from './video'
25 WITH_ACCOUNT = 'WITH_ACCOUNT',
26 WITH_ACTOR = 'WITH_ACTOR',
27 WITH_VIDEOS = 'WITH_VIDEOS'
33 model: () => ActorModel,
39 [ScopeNames.WITH_ACCOUNT]: {
42 model: () => AccountModel,
46 model: () => ActorModel,
53 [ScopeNames.WITH_VIDEOS]: {
58 [ScopeNames.WITH_ACTOR]: {
65 tableName: 'videoChannel',
68 fields: [ 'accountId' ]
72 export class VideoChannelModel extends Model<VideoChannelModel> {
75 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
80 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
90 @ForeignKey(() => ActorModel)
94 @BelongsTo(() => ActorModel, {
102 @ForeignKey(() => AccountModel)
106 @BelongsTo(() => AccountModel, {
112 Account: AccountModel
114 @HasMany(() => VideoModel, {
124 static sendDeleteIfOwned (instance: VideoChannelModel) {
125 if (instance.Actor.isOwned()) {
126 return sendDeleteActor(instance.Actor, undefined)
132 static countByAccount (accountId: number) {
139 return VideoChannelModel.count(query)
142 static listForApi (start: number, count: number, sort: string) {
146 order: [ getSort(sort) ]
149 return VideoChannelModel
150 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
151 .findAndCountAll(query)
152 .then(({ rows, count }) => {
153 return { total: count, data: rows }
157 static listByAccount (accountId: number) {
159 order: [ getSort('createdAt') ],
171 return VideoChannelModel
172 .findAndCountAll(query)
173 .then(({ rows, count }) => {
174 return { total: count, data: rows }
178 static loadByIdAndAccount (id: number, accountId: number) {
186 return VideoChannelModel
187 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
191 static loadAndPopulateAccount (id: number) {
192 return VideoChannelModel
193 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
197 static loadByUUIDAndPopulateAccount (uuid: string) {
210 return VideoChannelModel
211 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
215 static loadAndPopulateAccountAndVideos (id: number) {
222 return VideoChannelModel
223 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
224 .findById(id, options)
228 const actor = this.Actor.toFormattedJSON()
232 description: this.description,
233 isLocal: this.Actor.isOwned(),
234 createdAt: this.createdAt,
235 updatedAt: this.updatedAt
238 return Object.assign(actor, account)
241 toActivityPubObject (): ActivityPubActor {
242 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
244 return Object.assign(obj, {
245 summary: this.description,
248 type: 'Person' as 'Person',
249 id: this.Account.Actor.url