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 return sendDeleteActor(instance.Actor, options.transaction)
149 static countByAccount (accountId: number) {
156 return VideoChannelModel.count(query)
159 static listForApi (start: number, count: number, sort: string) {
166 return VideoChannelModel
167 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
168 .findAndCountAll(query)
169 .then(({ rows, count }) => {
170 return { total: count, data: rows }
174 static listByAccount (accountId: number) {
176 order: getSort('createdAt'),
188 return VideoChannelModel
189 .findAndCountAll(query)
190 .then(({ rows, count }) => {
191 return { total: count, data: rows }
195 static loadByIdAndAccount (id: number, accountId: number) {
203 return VideoChannelModel
204 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
208 static loadAndPopulateAccount (id: number) {
209 return VideoChannelModel
210 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
214 static loadByUUIDAndPopulateAccount (uuid: string) {
227 return VideoChannelModel
228 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
232 static loadAndPopulateAccountAndVideos (id: number) {
239 return VideoChannelModel
240 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
241 .findById(id, options)
244 toFormattedJSON (): VideoChannel {
245 const actor = this.Actor.toFormattedJSON()
246 const videoChannel = {
248 displayName: this.getDisplayName(),
249 description: this.description,
250 support: this.support,
251 isLocal: this.Actor.isOwned(),
252 createdAt: this.createdAt,
253 updatedAt: this.updatedAt,
254 ownerAccount: undefined,
258 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
260 return Object.assign(actor, videoChannel)
263 toActivityPubObject (): ActivityPubActor {
264 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
266 return Object.assign(obj, {
267 summary: this.description,
268 support: this.support,
271 type: 'Person' as 'Person',
272 id: this.Account.Actor.url