2 AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table,
3 UpdatedAt, Default, DataType
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'
17 import { CONSTRAINTS_FIELDS } from '../../initializers'
20 WITH_ACCOUNT = 'WITH_ACCOUNT',
21 WITH_ACTOR = 'WITH_ACTOR',
22 WITH_VIDEOS = 'WITH_VIDEOS'
28 model: () => ActorModel,
34 [ScopeNames.WITH_ACCOUNT]: {
37 model: () => AccountModel.unscoped(),
41 model: () => ActorModel.unscoped(),
48 [ScopeNames.WITH_VIDEOS]: {
53 [ScopeNames.WITH_ACTOR]: {
60 tableName: 'videoChannel',
63 fields: [ 'accountId' ]
67 export class VideoChannelModel extends Model<VideoChannelModel> {
70 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
76 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
77 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
82 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
83 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
92 @ForeignKey(() => ActorModel)
96 @BelongsTo(() => ActorModel, {
104 @ForeignKey(() => AccountModel)
108 @BelongsTo(() => AccountModel, {
114 Account: AccountModel
116 @HasMany(() => VideoModel, {
127 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
128 if (!instance.Actor) {
129 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
132 if (instance.Actor.isOwned()) {
133 logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
135 return sendDeleteActor(instance.Actor, options.transaction)
141 static countByAccount (accountId: number) {
148 return VideoChannelModel.count(query)
151 static listForApi (start: number, count: number, sort: string) {
158 return VideoChannelModel
159 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
160 .findAndCountAll(query)
161 .then(({ rows, count }) => {
162 return { total: count, data: rows }
166 static listByAccount (accountId: number) {
168 order: getSort('createdAt'),
180 return VideoChannelModel
181 .findAndCountAll(query)
182 .then(({ rows, count }) => {
183 return { total: count, data: rows }
187 static loadByIdAndAccount (id: number, accountId: number) {
195 return VideoChannelModel
196 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
200 static loadAndPopulateAccount (id: number) {
201 return VideoChannelModel
202 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
206 static loadByUUIDAndPopulateAccount (uuid: string) {
219 return VideoChannelModel
220 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
224 static loadAndPopulateAccountAndVideos (id: number) {
231 return VideoChannelModel
232 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
233 .findById(id, options)
236 toFormattedJSON (): VideoChannel {
237 const actor = this.Actor.toFormattedJSON()
238 const videoChannel = {
240 displayName: this.name,
241 description: this.description,
242 support: this.support,
243 isLocal: this.Actor.isOwned(),
244 createdAt: this.createdAt,
245 updatedAt: this.updatedAt,
246 ownerAccount: undefined,
250 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
252 return Object.assign(actor, videoChannel)
255 toActivityPubObject (): ActivityPubActor {
256 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
258 return Object.assign(obj, {
259 summary: this.description,
260 support: this.support,
263 type: 'Person' as 'Person',
264 id: this.Account.Actor.url