]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel.ts
b5bcbdc655b16f3487b263b5a4bb5dc8481d7fb1
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
1 import {
2 AllowNull,
3 BeforeDestroy,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 Default,
9 DefaultScope,
10 ForeignKey,
11 HasMany,
12 Is,
13 Model,
14 Scopes,
15 Sequelize,
16 Table,
17 UpdatedAt
18 } from 'sequelize-typescript'
19 import { ActivityPubActor } from '../../../shared/models/activitypub'
20 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
21 import {
22 isVideoChannelDescriptionValid,
23 isVideoChannelNameValid,
24 isVideoChannelSupportValid
25 } from '../../helpers/custom-validators/video-channels'
26 import { sendDeleteActor } from '../../lib/activitypub/send'
27 import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
28 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
29 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
30 import { VideoModel } from './video'
31 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
32 import { ServerModel } from '../server/server'
33 import { FindOptions, Op, literal, ScopeOptions } from 'sequelize'
34 import { AvatarModel } from '../avatar/avatar'
35 import { VideoPlaylistModel } from './video-playlist'
36 import * as Bluebird from 'bluebird'
37 import {
38 MChannelAccountDefault,
39 MChannelActor,
40 MChannelActorAccountDefaultVideos,
41 MChannelAP,
42 MChannelFormattable,
43 MChannelSummaryFormattable
44 } from '../../typings/models/video'
45
46 export enum ScopeNames {
47 FOR_API = 'FOR_API',
48 SUMMARY = 'SUMMARY',
49 WITH_ACCOUNT = 'WITH_ACCOUNT',
50 WITH_ACTOR = 'WITH_ACTOR',
51 WITH_VIDEOS = 'WITH_VIDEOS',
52 WITH_STATS = 'WITH_STATS'
53 }
54
55 type AvailableForListOptions = {
56 actorId: number
57 }
58
59 type AvailableWithStatsOptions = {
60 daysPrior: number
61 }
62
63 export type SummaryOptions = {
64 withAccount?: boolean // Default: false
65 withAccountBlockerIds?: number[]
66 }
67
68 @DefaultScope(() => ({
69 include: [
70 {
71 model: ActorModel,
72 required: true
73 }
74 ]
75 }))
76 @Scopes(() => ({
77 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
78 // Only list local channels OR channels that are on an instance followed by actorId
79 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
80
81 return {
82 include: [
83 {
84 attributes: {
85 exclude: unusedActorAttributesForAPI
86 },
87 model: ActorModel,
88 where: {
89 [Op.or]: [
90 {
91 serverId: null
92 },
93 {
94 serverId: {
95 [Op.in]: Sequelize.literal(inQueryInstanceFollow)
96 }
97 }
98 ]
99 }
100 },
101 {
102 model: AccountModel,
103 required: true,
104 include: [
105 {
106 attributes: {
107 exclude: unusedActorAttributesForAPI
108 },
109 model: ActorModel, // Default scope includes avatar and server
110 required: true
111 }
112 ]
113 }
114 ]
115 }
116 },
117 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
118 const base: FindOptions = {
119 attributes: [ 'id', 'name', 'description', 'actorId' ],
120 include: [
121 {
122 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
123 model: ActorModel.unscoped(),
124 required: true,
125 include: [
126 {
127 attributes: [ 'host' ],
128 model: ServerModel.unscoped(),
129 required: false
130 },
131 {
132 model: AvatarModel.unscoped(),
133 required: false
134 }
135 ]
136 }
137 ]
138 }
139
140 if (options.withAccount === true) {
141 base.include.push({
142 model: AccountModel.scope({
143 method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
144 }),
145 required: true
146 })
147 }
148
149 return base
150 },
151 [ScopeNames.WITH_ACCOUNT]: {
152 include: [
153 {
154 model: AccountModel,
155 required: true
156 }
157 ]
158 },
159 [ScopeNames.WITH_ACTOR]: {
160 include: [
161 ActorModel
162 ]
163 },
164 [ScopeNames.WITH_VIDEOS]: {
165 include: [
166 VideoModel
167 ]
168 },
169 [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
170 const daysPrior = parseInt(options.daysPrior + '', 10)
171
172 return {
173 attributes: {
174 include: [
175 [
176 literal(
177 '(' +
178 `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
179 'FROM ( ' +
180 'WITH ' +
181 'days AS ( ' +
182 `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
183 `date_trunc('day', now()), '1 day'::interval) AS day ` +
184 ') ' +
185 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
186 'FROM days ' +
187 'LEFT JOIN (' +
188 '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
189 'AND "video"."channelId" = "VideoChannelModel"."id"' +
190 `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
191 'GROUP BY day ' +
192 'ORDER BY day ' +
193 ') t' +
194 ')'
195 ),
196 'viewsPerDay'
197 ]
198 ]
199 }
200 }
201 }
202 }))
203 @Table({
204 tableName: 'videoChannel',
205 indexes: [
206 buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
207
208 {
209 fields: [ 'accountId' ]
210 },
211 {
212 fields: [ 'actorId' ]
213 }
214 ]
215 })
216 export class VideoChannelModel extends Model<VideoChannelModel> {
217
218 @AllowNull(false)
219 @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
220 @Column
221 name: string
222
223 @AllowNull(true)
224 @Default(null)
225 @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
226 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
227 description: string
228
229 @AllowNull(true)
230 @Default(null)
231 @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
232 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
233 support: string
234
235 @CreatedAt
236 createdAt: Date
237
238 @UpdatedAt
239 updatedAt: Date
240
241 @ForeignKey(() => ActorModel)
242 @Column
243 actorId: number
244
245 @BelongsTo(() => ActorModel, {
246 foreignKey: {
247 allowNull: false
248 },
249 onDelete: 'cascade'
250 })
251 Actor: ActorModel
252
253 @ForeignKey(() => AccountModel)
254 @Column
255 accountId: number
256
257 @BelongsTo(() => AccountModel, {
258 foreignKey: {
259 allowNull: false
260 },
261 hooks: true
262 })
263 Account: AccountModel
264
265 @HasMany(() => VideoModel, {
266 foreignKey: {
267 name: 'channelId',
268 allowNull: false
269 },
270 onDelete: 'CASCADE',
271 hooks: true
272 })
273 Videos: VideoModel[]
274
275 @HasMany(() => VideoPlaylistModel, {
276 foreignKey: {
277 allowNull: true
278 },
279 onDelete: 'CASCADE',
280 hooks: true
281 })
282 VideoPlaylists: VideoPlaylistModel[]
283
284 @BeforeDestroy
285 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
286 if (!instance.Actor) {
287 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
288 }
289
290 if (instance.Actor.isOwned()) {
291 return sendDeleteActor(instance.Actor, options.transaction)
292 }
293
294 return undefined
295 }
296
297 static countByAccount (accountId: number) {
298 const query = {
299 where: {
300 accountId
301 }
302 }
303
304 return VideoChannelModel.count(query)
305 }
306
307 static listForApi (actorId: number, start: number, count: number, sort: string) {
308 const query = {
309 offset: start,
310 limit: count,
311 order: getSort(sort)
312 }
313
314 const scopes = {
315 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
316 }
317 return VideoChannelModel
318 .scope(scopes)
319 .findAndCountAll(query)
320 .then(({ rows, count }) => {
321 return { total: count, data: rows }
322 })
323 }
324
325 static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> {
326 const query = {
327 attributes: [ ],
328 offset: 0,
329 order: getSort(sort),
330 include: [
331 {
332 attributes: [ 'preferredUsername', 'serverId' ],
333 model: ActorModel.unscoped(),
334 where: {
335 serverId: null
336 }
337 }
338 ]
339 }
340
341 return VideoChannelModel
342 .unscoped()
343 .findAll(query)
344 }
345
346 static searchForApi (options: {
347 actorId: number
348 search: string
349 start: number
350 count: number
351 sort: string
352 }) {
353 const attributesInclude = []
354 const escapedSearch = VideoModel.sequelize.escape(options.search)
355 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
356 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
357
358 const query = {
359 attributes: {
360 include: attributesInclude
361 },
362 offset: options.start,
363 limit: options.count,
364 order: getSort(options.sort),
365 where: {
366 [Op.or]: [
367 Sequelize.literal(
368 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
369 ),
370 Sequelize.literal(
371 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
372 )
373 ]
374 }
375 }
376
377 const scopes = {
378 method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
379 }
380 return VideoChannelModel
381 .scope(scopes)
382 .findAndCountAll(query)
383 .then(({ rows, count }) => {
384 return { total: count, data: rows }
385 })
386 }
387
388 static listByAccount (options: {
389 accountId: number
390 start: number
391 count: number
392 sort: string
393 withStats?: boolean
394 }) {
395 const query = {
396 offset: options.start,
397 limit: options.count,
398 order: getSort(options.sort),
399 include: [
400 {
401 model: AccountModel,
402 where: {
403 id: options.accountId
404 },
405 required: true
406 }
407 ]
408 }
409
410 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ]
411
412 if (options.withStats === true) {
413 scopes.push({
414 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
415 })
416 }
417
418 return VideoChannelModel
419 .scope(scopes)
420 .findAndCountAll(query)
421 .then(({ rows, count }) => {
422 return { total: count, data: rows }
423 })
424 }
425
426 static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
427 return VideoChannelModel.unscoped()
428 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
429 .findByPk(id)
430 }
431
432 static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelAccountDefault> {
433 const query = {
434 where: {
435 id,
436 accountId
437 }
438 }
439
440 return VideoChannelModel.unscoped()
441 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
442 .findOne(query)
443 }
444
445 static loadAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
446 return VideoChannelModel.unscoped()
447 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
448 .findByPk(id)
449 }
450
451 static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> {
452 const query = {
453 include: [
454 {
455 model: ActorModel,
456 required: true,
457 where: {
458 url
459 }
460 }
461 ]
462 }
463
464 return VideoChannelModel
465 .scope([ ScopeNames.WITH_ACCOUNT ])
466 .findOne(query)
467 }
468
469 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
470 const [ name, host ] = nameWithHost.split('@')
471
472 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
473
474 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
475 }
476
477 static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelAccountDefault> {
478 const query = {
479 include: [
480 {
481 model: ActorModel,
482 required: true,
483 where: {
484 preferredUsername: name,
485 serverId: null
486 }
487 }
488 ]
489 }
490
491 return VideoChannelModel.unscoped()
492 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
493 .findOne(query)
494 }
495
496 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelAccountDefault> {
497 const query = {
498 include: [
499 {
500 model: ActorModel,
501 required: true,
502 where: {
503 preferredUsername: name
504 },
505 include: [
506 {
507 model: ServerModel,
508 required: true,
509 where: { host }
510 }
511 ]
512 }
513 ]
514 }
515
516 return VideoChannelModel.unscoped()
517 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
518 .findOne(query)
519 }
520
521 static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> {
522 const options = {
523 include: [
524 VideoModel
525 ]
526 }
527
528 return VideoChannelModel.unscoped()
529 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
530 .findByPk(id, options)
531 }
532
533 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
534 const actor = this.Actor.toFormattedSummaryJSON()
535
536 return {
537 id: this.id,
538 name: actor.name,
539 displayName: this.getDisplayName(),
540 url: actor.url,
541 host: actor.host,
542 avatar: actor.avatar
543 }
544 }
545
546 toFormattedJSON (this: MChannelFormattable): VideoChannel {
547 const viewsPerDay = this.get('viewsPerDay') as string
548
549 const actor = this.Actor.toFormattedJSON()
550 const videoChannel = {
551 id: this.id,
552 displayName: this.getDisplayName(),
553 description: this.description,
554 support: this.support,
555 isLocal: this.Actor.isOwned(),
556 createdAt: this.createdAt,
557 updatedAt: this.updatedAt,
558 ownerAccount: undefined,
559 viewsPerDay: viewsPerDay
560 ? viewsPerDay.split(',').map(v => {
561 const o = v.split('|')
562 return {
563 date: new Date(o[0]),
564 views: +o[1]
565 }
566 })
567 : undefined
568 }
569
570 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
571
572 return Object.assign(actor, videoChannel)
573 }
574
575 toActivityPubObject (this: MChannelAP): ActivityPubActor {
576 const obj = this.Actor.toActivityPubObject(this.name)
577
578 return Object.assign(obj, {
579 summary: this.description,
580 support: this.support,
581 attributedTo: [
582 {
583 type: 'Person' as 'Person',
584 id: this.Account.Actor.url
585 }
586 ]
587 })
588 }
589
590 getDisplayName () {
591 return this.name
592 }
593
594 isOutdated () {
595 return this.Actor.isOutdated()
596 }
597 }