]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel.ts
Add import.video.torrent configuration
[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 return sendDeleteActor(instance.Actor, options.transaction)
144 }
145
146 return undefined
147 }
148
149 static countByAccount (accountId: number) {
150 const query = {
151 where: {
152 accountId
153 }
154 }
155
156 return VideoChannelModel.count(query)
157 }
158
159 static listForApi (start: number, count: number, sort: string) {
160 const query = {
161 offset: start,
162 limit: count,
163 order: getSort(sort)
164 }
165
166 return VideoChannelModel
167 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
168 .findAndCountAll(query)
169 .then(({ rows, count }) => {
170 return { total: count, data: rows }
171 })
172 }
173
174 static listByAccount (accountId: number) {
175 const query = {
176 order: getSort('createdAt'),
177 include: [
178 {
179 model: AccountModel,
180 where: {
181 id: accountId
182 },
183 required: true
184 }
185 ]
186 }
187
188 return VideoChannelModel
189 .findAndCountAll(query)
190 .then(({ rows, count }) => {
191 return { total: count, data: rows }
192 })
193 }
194
195 static loadByIdAndAccount (id: number, accountId: number) {
196 const options = {
197 where: {
198 id,
199 accountId
200 }
201 }
202
203 return VideoChannelModel
204 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
205 .findOne(options)
206 }
207
208 static loadAndPopulateAccount (id: number) {
209 return VideoChannelModel
210 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
211 .findById(id)
212 }
213
214 static loadByUUIDAndPopulateAccount (uuid: string) {
215 const options = {
216 include: [
217 {
218 model: ActorModel,
219 required: true,
220 where: {
221 uuid
222 }
223 }
224 ]
225 }
226
227 return VideoChannelModel
228 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
229 .findOne(options)
230 }
231
232 static loadAndPopulateAccountAndVideos (id: number) {
233 const options = {
234 include: [
235 VideoModel
236 ]
237 }
238
239 return VideoChannelModel
240 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
241 .findById(id, options)
242 }
243
244 toFormattedJSON (): VideoChannel {
245 const actor = this.Actor.toFormattedJSON()
246 const videoChannel = {
247 id: this.id,
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,
255 videos: undefined
256 }
257
258 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
259
260 return Object.assign(actor, videoChannel)
261 }
262
263 toActivityPubObject (): ActivityPubActor {
264 const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
265
266 return Object.assign(obj, {
267 summary: this.description,
268 support: this.support,
269 attributedTo: [
270 {
271 type: 'Person' as 'Person',
272 id: this.Account.Actor.url
273 }
274 ]
275 })
276 }
277
278 getDisplayName () {
279 return this.name
280 }
281 }