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'
18 import { AvatarModel } from '../avatar/avatar'
21 WITH_ACCOUNT = 'WITH_ACCOUNT',
22 WITH_ACTOR = 'WITH_ACTOR',
23 WITH_VIDEOS = 'WITH_VIDEOS'
29 model: () => ActorModel,
35 [ScopeNames.WITH_ACCOUNT]: {
38 model: () => AccountModel.unscoped(),
42 model: () => ActorModel.unscoped(),
46 model: () => AvatarModel.unscoped(),
55 [ScopeNames.WITH_VIDEOS]: {
60 [ScopeNames.WITH_ACTOR]: {
67 tableName: 'videoChannel',
70 fields: [ 'accountId' ]
74 export class VideoChannelModel extends Model<VideoChannelModel> {
77 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
83 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
84 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
89 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
90 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
99 @ForeignKey(() => ActorModel)
103 @BelongsTo(() => ActorModel, {
111 @ForeignKey(() => AccountModel)
115 @BelongsTo(() => AccountModel, {
121 Account: AccountModel
123 @HasMany(() => VideoModel, {
134 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
135 if (!instance.Actor) {
136 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
139 if (instance.Actor.isOwned()) {
140 logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
142 return sendDeleteActor(instance.Actor, options.transaction)
148 static countByAccount (accountId: number) {
155 return VideoChannelModel.count(query)
158 static listForApi (start: number, count: number, sort: string) {
165 return VideoChannelModel
166 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
167 .findAndCountAll(query)
168 .then(({ rows, count }) => {
169 return { total: count, data: rows }
173 static listByAccount (accountId: number) {
175 order: getSort('createdAt'),
187 return VideoChannelModel
188 .findAndCountAll(query)
189 .then(({ rows, count }) => {
190 return { total: count, data: rows }
194 static loadByIdAndAccount (id: number, accountId: number) {
202 return VideoChannelModel
203 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
207 static loadAndPopulateAccount (id: number) {
208 return VideoChannelModel
209 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
213 static loadByUUIDAndPopulateAccount (uuid: string) {
226 return VideoChannelModel
227 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
231 static loadAndPopulateAccountAndVideos (id: number) {
238 return VideoChannelModel
239 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
240 .findById(id, options)
243 toFormattedJSON (): VideoChannel {
244 const actor = this.Actor.toFormattedJSON()
245 const videoChannel = {
247 displayName: this.getDisplayName(),
248 description: this.description,
249 support: this.support,
250 isLocal: this.Actor.isOwned(),
251 createdAt: this.createdAt,
252 updatedAt: this.updatedAt,
253 ownerAccount: undefined,
257 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
259 return Object.assign(actor, videoChannel)
262 toActivityPubObject (): ActivityPubActor {
263 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
265 return Object.assign(obj, {
266 summary: this.description,
267 support: this.support,
270 type: 'Person' as 'Person',
271 id: this.Account.Actor.url