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