]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel.ts
6567b00d69475bc9037218442740eff6a1f2345c
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
1 import {
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'
7 import {
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'
19
20 enum ScopeNames {
21 WITH_ACCOUNT = 'WITH_ACCOUNT',
22 WITH_ACTOR = 'WITH_ACTOR',
23 WITH_VIDEOS = 'WITH_VIDEOS'
24 }
25
26 @DefaultScope({
27 include: [
28 {
29 model: () => ActorModel,
30 required: true
31 }
32 ]
33 })
34 @Scopes({
35 [ScopeNames.WITH_ACCOUNT]: {
36 include: [
37 {
38 model: () => AccountModel.unscoped(),
39 required: true,
40 include: [
41 {
42 model: () => ActorModel.unscoped(),
43 required: true,
44 include: [
45 {
46 model: () => AvatarModel.unscoped(),
47 required: false
48 }
49 ]
50 }
51 ]
52 }
53 ]
54 },
55 [ScopeNames.WITH_VIDEOS]: {
56 include: [
57 () => VideoModel
58 ]
59 },
60 [ScopeNames.WITH_ACTOR]: {
61 include: [
62 () => ActorModel
63 ]
64 }
65 })
66 @Table({
67 tableName: 'videoChannel',
68 indexes: [
69 {
70 fields: [ 'accountId' ]
71 },
72 {
73 fields: [ 'actorId' ]
74 }
75 ]
76 })
77 export class VideoChannelModel extends Model<VideoChannelModel> {
78
79 @AllowNull(false)
80 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
81 @Column
82 name: string
83
84 @AllowNull(true)
85 @Default(null)
86 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
88 description: string
89
90 @AllowNull(true)
91 @Default(null)
92 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
93 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
94 support: string
95
96 @CreatedAt
97 createdAt: Date
98
99 @UpdatedAt
100 updatedAt: Date
101
102 @ForeignKey(() => ActorModel)
103 @Column
104 actorId: number
105
106 @BelongsTo(() => ActorModel, {
107 foreignKey: {
108 allowNull: false
109 },
110 onDelete: 'cascade'
111 })
112 Actor: ActorModel
113
114 @ForeignKey(() => AccountModel)
115 @Column
116 accountId: number
117
118 @BelongsTo(() => AccountModel, {
119 foreignKey: {
120 allowNull: false
121 },
122 hooks: true
123 })
124 Account: AccountModel
125
126 @HasMany(() => VideoModel, {
127 foreignKey: {
128 name: 'channelId',
129 allowNull: false
130 },
131 onDelete: 'CASCADE',
132 hooks: true
133 })
134 Videos: VideoModel[]
135
136 @BeforeDestroy
137 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
138 if (!instance.Actor) {
139 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
140 }
141
142 if (instance.Actor.isOwned()) {
143 logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
144
145 return sendDeleteActor(instance.Actor, options.transaction)
146 }
147
148 return undefined
149 }
150
151 static countByAccount (accountId: number) {
152 const query = {
153 where: {
154 accountId
155 }
156 }
157
158 return VideoChannelModel.count(query)
159 }
160
161 static listForApi (start: number, count: number, sort: string) {
162 const query = {
163 offset: start,
164 limit: count,
165 order: getSort(sort)
166 }
167
168 return VideoChannelModel
169 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
170 .findAndCountAll(query)
171 .then(({ rows, count }) => {
172 return { total: count, data: rows }
173 })
174 }
175
176 static listByAccount (accountId: number) {
177 const query = {
178 order: getSort('createdAt'),
179 include: [
180 {
181 model: AccountModel,
182 where: {
183 id: accountId
184 },
185 required: true
186 }
187 ]
188 }
189
190 return VideoChannelModel
191 .findAndCountAll(query)
192 .then(({ rows, count }) => {
193 return { total: count, data: rows }
194 })
195 }
196
197 static loadByIdAndAccount (id: number, accountId: number) {
198 const options = {
199 where: {
200 id,
201 accountId
202 }
203 }
204
205 return VideoChannelModel
206 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
207 .findOne(options)
208 }
209
210 static loadAndPopulateAccount (id: number) {
211 return VideoChannelModel
212 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
213 .findById(id)
214 }
215
216 static loadByUUIDAndPopulateAccount (uuid: string) {
217 const options = {
218 include: [
219 {
220 model: ActorModel,
221 required: true,
222 where: {
223 uuid
224 }
225 }
226 ]
227 }
228
229 return VideoChannelModel
230 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
231 .findOne(options)
232 }
233
234 static loadAndPopulateAccountAndVideos (id: number) {
235 const options = {
236 include: [
237 VideoModel
238 ]
239 }
240
241 return VideoChannelModel
242 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
243 .findById(id, options)
244 }
245
246 toFormattedJSON (): VideoChannel {
247 const actor = this.Actor.toFormattedJSON()
248 const videoChannel = {
249 id: this.id,
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,
257 videos: undefined
258 }
259
260 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
261
262 return Object.assign(actor, videoChannel)
263 }
264
265 toActivityPubObject (): ActivityPubActor {
266 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
267
268 return Object.assign(obj, {
269 summary: this.description,
270 support: this.support,
271 attributedTo: [
272 {
273 type: 'Person' as 'Person',
274 id: this.Account.Actor.url
275 }
276 ]
277 })
278 }
279
280 getDisplayName () {
281 return this.name
282 }
283 }