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' ]
77 export class VideoChannelModel extends Model<VideoChannelModel> {
80 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
86 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
92 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
93 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
102 @ForeignKey(() => ActorModel)
106 @BelongsTo(() => ActorModel, {
114 @ForeignKey(() => AccountModel)
118 @BelongsTo(() => AccountModel, {
124 Account: AccountModel
126 @HasMany(() => VideoModel, {
137 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
138 if (!instance.Actor) {
139 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
142 if (instance.Actor.isOwned()) {
143 logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
145 return sendDeleteActor(instance.Actor, options.transaction)
151 static countByAccount (accountId: number) {
158 return VideoChannelModel.count(query)
161 static listForApi (start: number, count: number, sort: string) {
168 return VideoChannelModel
169 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
170 .findAndCountAll(query)
171 .then(({ rows, count }) => {
172 return { total: count, data: rows }
176 static listByAccount (accountId: number) {
178 order: getSort('createdAt'),
190 return VideoChannelModel
191 .findAndCountAll(query)
192 .then(({ rows, count }) => {
193 return { total: count, data: rows }
197 static loadByIdAndAccount (id: number, accountId: number) {
205 return VideoChannelModel
206 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
210 static loadAndPopulateAccount (id: number) {
211 return VideoChannelModel
212 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
216 static loadByUUIDAndPopulateAccount (uuid: string) {
229 return VideoChannelModel
230 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
234 static loadAndPopulateAccountAndVideos (id: number) {
241 return VideoChannelModel
242 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
243 .findById(id, options)
246 toFormattedJSON (): VideoChannel {
247 const actor = this.Actor.toFormattedJSON()
248 const videoChannel = {
250 displayName: this.getDisplayName(),
251 description: this.description,
252 support: this.support,
253 isLocal: this.Actor.isOwned(),
254 createdAt: this.createdAt,
255 updatedAt: this.updatedAt,
256 ownerAccount: undefined,
260 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
262 return Object.assign(actor, videoChannel)
265 toActivityPubObject (): ActivityPubActor {
266 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
268 return Object.assign(obj, {
269 summary: this.description,
270 support: this.support,
273 type: 'Person' as 'Person',
274 id: this.Account.Actor.url