X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-channel.ts;h=4251afce994d00ab7999b2fb92420b36b8d7bec1;hb=f4001cf408a99049d01a356bfb20a62342de06ea;hp=7c161c8642b295ed77e5c9c6d14f4c66a28cfdd7;hpb=f05a1c30c15d2ae35c11e241ca039a72eeb7d6ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 7c161c864..4251afce9 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -1,15 +1,21 @@ import { AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table, - UpdatedAt + UpdatedAt, Default, DataType } from 'sequelize-typescript' import { ActivityPubActor } from '../../../shared/models/activitypub' -import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' +import { VideoChannel } from '../../../shared/models/videos' +import { + isVideoChannelDescriptionValid, isVideoChannelNameValid, + isVideoChannelSupportValid +} from '../../helpers/custom-validators/video-channels' import { logger } from '../../helpers/logger' import { sendDeleteActor } from '../../lib/activitypub/send' import { AccountModel } from '../account/account' import { ActorModel } from '../activitypub/actor' import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' +import { CONSTRAINTS_FIELDS } from '../../initializers' +import { AvatarModel } from '../avatar/avatar' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -29,12 +35,18 @@ enum ScopeNames { [ScopeNames.WITH_ACCOUNT]: { include: [ { - model: () => AccountModel, + model: () => AccountModel.unscoped(), required: true, include: [ { - model: () => ActorModel, - required: true + model: () => ActorModel.unscoped(), + required: true, + include: [ + { + model: () => AvatarModel.unscoped(), + required: false + } + ] } ] } @@ -67,10 +79,17 @@ export class VideoChannelModel extends Model { name: string @AllowNull(true) + @Default(null) @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description')) - @Column + @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) description: string + @AllowNull(true) + @Default(null) + @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) + support: string + @CreatedAt createdAt: Date @@ -97,7 +116,7 @@ export class VideoChannelModel extends Model { foreignKey: { allowNull: false }, - onDelete: 'CASCADE' + hooks: true }) Account: AccountModel @@ -140,7 +159,7 @@ export class VideoChannelModel extends Model { const query = { offset: start, limit: count, - order: [ getSort(sort) ] + order: getSort(sort) } return VideoChannelModel @@ -153,7 +172,7 @@ export class VideoChannelModel extends Model { static listByAccount (accountId: number) { const query = { - order: [ getSort('createdAt') ], + order: getSort('createdAt'), include: [ { model: AccountModel, @@ -221,18 +240,23 @@ export class VideoChannelModel extends Model { .findById(id, options) } - toFormattedJSON () { + toFormattedJSON (): VideoChannel { const actor = this.Actor.toFormattedJSON() - const account = { + const videoChannel = { id: this.id, - displayName: this.name, + displayName: this.getDisplayName(), description: this.description, + support: this.support, isLocal: this.Actor.isOwned(), createdAt: this.createdAt, - updatedAt: this.updatedAt + updatedAt: this.updatedAt, + ownerAccount: undefined, + videos: undefined } - return Object.assign(actor, account) + if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() + + return Object.assign(actor, videoChannel) } toActivityPubObject (): ActivityPubActor { @@ -240,6 +264,7 @@ export class VideoChannelModel extends Model { return Object.assign(obj, { summary: this.description, + support: this.support, attributedTo: [ { type: 'Person' as 'Person', @@ -248,4 +273,8 @@ export class VideoChannelModel extends Model { ] }) } + + getDisplayName () { + return this.name + } }