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