2 AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table,
4 } from 'sequelize-typescript'
5 import { ActivityPubActor } from '../../../shared/models/activitypub'
6 import { VideoChannel } from '../../../shared/models/videos'
8 isVideoChannelDescriptionValid, isVideoChannelNameValid,
9 isVideoChannelSupportValid
10 } from '../../helpers/custom-validators/video-channels'
11 import { logger } from '../../helpers/logger'
12 import { sendDeleteActor } from '../../lib/activitypub/send'
13 import { AccountModel } from '../account/account'
14 import { ActorModel } from '../activitypub/actor'
15 import { getSort, throwIfNotValid } from '../utils'
16 import { VideoModel } from './video'
19 WITH_ACCOUNT = 'WITH_ACCOUNT',
20 WITH_ACTOR = 'WITH_ACTOR',
21 WITH_VIDEOS = 'WITH_VIDEOS'
27 model: () => ActorModel,
33 [ScopeNames.WITH_ACCOUNT]: {
36 model: () => AccountModel,
40 model: () => ActorModel,
47 [ScopeNames.WITH_VIDEOS]: {
52 [ScopeNames.WITH_ACTOR]: {
59 tableName: 'videoChannel',
62 fields: [ 'accountId' ]
66 export class VideoChannelModel extends Model<VideoChannelModel> {
69 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
75 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
81 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
91 @ForeignKey(() => ActorModel)
95 @BelongsTo(() => ActorModel, {
103 @ForeignKey(() => AccountModel)
107 @BelongsTo(() => AccountModel, {
113 Account: AccountModel
115 @HasMany(() => VideoModel, {
126 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
127 if (!instance.Actor) {
128 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
131 if (instance.Actor.isOwned()) {
132 logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
134 return sendDeleteActor(instance.Actor, options.transaction)
140 static countByAccount (accountId: number) {
147 return VideoChannelModel.count(query)
150 static listForApi (start: number, count: number, sort: string) {
157 return VideoChannelModel
158 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
159 .findAndCountAll(query)
160 .then(({ rows, count }) => {
161 return { total: count, data: rows }
165 static listByAccount (accountId: number) {
167 order: getSort('createdAt'),
179 return VideoChannelModel
180 .findAndCountAll(query)
181 .then(({ rows, count }) => {
182 return { total: count, data: rows }
186 static loadByIdAndAccount (id: number, accountId: number) {
194 return VideoChannelModel
195 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
199 static loadAndPopulateAccount (id: number) {
200 return VideoChannelModel
201 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
205 static loadByUUIDAndPopulateAccount (uuid: string) {
218 return VideoChannelModel
219 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
223 static loadAndPopulateAccountAndVideos (id: number) {
230 return VideoChannelModel
231 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
232 .findById(id, options)
235 toFormattedJSON (): VideoChannel {
236 const actor = this.Actor.toFormattedJSON()
239 displayName: this.name,
240 description: this.description,
241 support: this.support,
242 isLocal: this.Actor.isOwned(),
243 createdAt: this.createdAt,
244 updatedAt: this.updatedAt
247 return Object.assign(actor, account)
250 toActivityPubObject (): ActivityPubActor {
251 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
253 return Object.assign(obj, {
254 summary: this.description,
255 support: this.support,
258 type: 'Person' as 'Person',
259 id: this.Account.Actor.url