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