View stats for channels
[oweals/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     attributes: {
171       include: [
172         [
173           literal(
174             '(' +
175             `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
176             'FROM ( ' +
177               'WITH ' +
178                 'days AS ( ' +
179                   `SELECT generate_series(date_trunc('day', now()) - '${options.daysPrior} day'::interval, ` +
180                          `date_trunc('day', now()), '1 day'::interval) AS day ` +
181                 '), ' +
182                 'views AS ( ' +
183                   'SELECT * ' +
184                   'FROM "videoView" ' +
185                   'WHERE "videoView"."videoId" IN ( ' +
186                     'SELECT "video"."id" ' +
187                     'FROM "video" ' +
188                     'WHERE "video"."channelId" = "VideoChannelModel"."id" ' +
189                   ') ' +
190                 ') ' +
191               'SELECT days.day AS day, ' +
192                      'COALESCE(SUM(views.views), 0) AS views ' +
193               'FROM days ' +
194               `LEFT JOIN views ON date_trunc('day', "views"."createdAt") = days.day ` +
195               'GROUP BY 1 ' +
196               'ORDER BY day ' +
197             ') t' +
198             ')'
199           ),
200           'viewsPerDay'
201         ]
202       ]
203     }
204   })
205 }))
206 @Table({
207   tableName: 'videoChannel',
208   indexes: [
209     buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
210
211     {
212       fields: [ 'accountId' ]
213     },
214     {
215       fields: [ 'actorId' ]
216     }
217   ]
218 })
219 export class VideoChannelModel extends Model<VideoChannelModel> {
220
221   @AllowNull(false)
222   @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
223   @Column
224   name: string
225
226   @AllowNull(true)
227   @Default(null)
228   @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
229   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
230   description: string
231
232   @AllowNull(true)
233   @Default(null)
234   @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
235   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
236   support: string
237
238   @CreatedAt
239   createdAt: Date
240
241   @UpdatedAt
242   updatedAt: Date
243
244   @ForeignKey(() => ActorModel)
245   @Column
246   actorId: number
247
248   @BelongsTo(() => ActorModel, {
249     foreignKey: {
250       allowNull: false
251     },
252     onDelete: 'cascade'
253   })
254   Actor: ActorModel
255
256   @ForeignKey(() => AccountModel)
257   @Column
258   accountId: number
259
260   @BelongsTo(() => AccountModel, {
261     foreignKey: {
262       allowNull: false
263     },
264     hooks: true
265   })
266   Account: AccountModel
267
268   @HasMany(() => VideoModel, {
269     foreignKey: {
270       name: 'channelId',
271       allowNull: false
272     },
273     onDelete: 'CASCADE',
274     hooks: true
275   })
276   Videos: VideoModel[]
277
278   @HasMany(() => VideoPlaylistModel, {
279     foreignKey: {
280       allowNull: true
281     },
282     onDelete: 'CASCADE',
283     hooks: true
284   })
285   VideoPlaylists: VideoPlaylistModel[]
286
287   @BeforeDestroy
288   static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
289     if (!instance.Actor) {
290       instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
291     }
292
293     if (instance.Actor.isOwned()) {
294       return sendDeleteActor(instance.Actor, options.transaction)
295     }
296
297     return undefined
298   }
299
300   static countByAccount (accountId: number) {
301     const query = {
302       where: {
303         accountId
304       }
305     }
306
307     return VideoChannelModel.count(query)
308   }
309
310   static listForApi (actorId: number, start: number, count: number, sort: string) {
311     const query = {
312       offset: start,
313       limit: count,
314       order: getSort(sort)
315     }
316
317     const scopes = {
318       method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
319     }
320     return VideoChannelModel
321       .scope(scopes)
322       .findAndCountAll(query)
323       .then(({ rows, count }) => {
324         return { total: count, data: rows }
325       })
326   }
327
328   static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> {
329     const query = {
330       attributes: [ ],
331       offset: 0,
332       order: getSort(sort),
333       include: [
334         {
335           attributes: [ 'preferredUsername', 'serverId' ],
336           model: ActorModel.unscoped(),
337           where: {
338             serverId: null
339           }
340         }
341       ]
342     }
343
344     return VideoChannelModel
345       .unscoped()
346       .findAll(query)
347   }
348
349   static searchForApi (options: {
350     actorId: number
351     search: string
352     start: number
353     count: number
354     sort: string
355   }) {
356     const attributesInclude = []
357     const escapedSearch = VideoModel.sequelize.escape(options.search)
358     const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
359     attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
360
361     const query = {
362       attributes: {
363         include: attributesInclude
364       },
365       offset: options.start,
366       limit: options.count,
367       order: getSort(options.sort),
368       where: {
369         [Op.or]: [
370           Sequelize.literal(
371             'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
372           ),
373           Sequelize.literal(
374             'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
375           )
376         ]
377       }
378     }
379
380     const scopes = {
381       method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
382     }
383     return VideoChannelModel
384       .scope(scopes)
385       .findAndCountAll(query)
386       .then(({ rows, count }) => {
387         return { total: count, data: rows }
388       })
389   }
390
391   static listByAccount (options: {
392     accountId: number
393     start: number
394     count: number
395     sort: string
396     withStats?: boolean
397   }) {
398     const query = {
399       offset: options.start,
400       limit: options.count,
401       order: getSort(options.sort),
402       include: [
403         {
404           model: AccountModel,
405           where: {
406             id: options.accountId
407           },
408           required: true
409         }
410       ]
411     }
412
413     const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ]
414
415     options.withStats = true // TODO: remove beyond after initial tests
416     if (options.withStats) {
417       scopes.push({
418         method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
419       })
420     }
421
422     return VideoChannelModel
423       .scope(scopes)
424       .findAndCountAll(query)
425       .then(({ rows, count }) => {
426         return { total: count, data: rows }
427       })
428   }
429
430   static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
431     return VideoChannelModel.unscoped()
432       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
433       .findByPk(id)
434   }
435
436   static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelAccountDefault> {
437     const query = {
438       where: {
439         id,
440         accountId
441       }
442     }
443
444     return VideoChannelModel.unscoped()
445       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
446       .findOne(query)
447   }
448
449   static loadAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
450     return VideoChannelModel.unscoped()
451       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
452       .findByPk(id)
453   }
454
455   static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> {
456     const query = {
457       include: [
458         {
459           model: ActorModel,
460           required: true,
461           where: {
462             url
463           }
464         }
465       ]
466     }
467
468     return VideoChannelModel
469       .scope([ ScopeNames.WITH_ACCOUNT ])
470       .findOne(query)
471   }
472
473   static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
474     const [ name, host ] = nameWithHost.split('@')
475
476     if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
477
478     return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
479   }
480
481   static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelAccountDefault> {
482     const query = {
483       include: [
484         {
485           model: ActorModel,
486           required: true,
487           where: {
488             preferredUsername: name,
489             serverId: null
490           }
491         }
492       ]
493     }
494
495     return VideoChannelModel.unscoped()
496       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
497       .findOne(query)
498   }
499
500   static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelAccountDefault> {
501     const query = {
502       include: [
503         {
504           model: ActorModel,
505           required: true,
506           where: {
507             preferredUsername: name
508           },
509           include: [
510             {
511               model: ServerModel,
512               required: true,
513               where: { host }
514             }
515           ]
516         }
517       ]
518     }
519
520     return VideoChannelModel.unscoped()
521       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
522       .findOne(query)
523   }
524
525   static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> {
526     const options = {
527       include: [
528         VideoModel
529       ]
530     }
531
532     return VideoChannelModel.unscoped()
533       .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
534       .findByPk(id, options)
535   }
536
537   toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
538     const actor = this.Actor.toFormattedSummaryJSON()
539
540     return {
541       id: this.id,
542       name: actor.name,
543       displayName: this.getDisplayName(),
544       url: actor.url,
545       host: actor.host,
546       avatar: actor.avatar
547     }
548   }
549
550   toFormattedJSON (this: MChannelFormattable): VideoChannel {
551     const viewsPerDay = this.get('viewsPerDay') as string
552
553     const actor = this.Actor.toFormattedJSON()
554     const videoChannel = {
555       id: this.id,
556       displayName: this.getDisplayName(),
557       description: this.description,
558       support: this.support,
559       isLocal: this.Actor.isOwned(),
560       createdAt: this.createdAt,
561       updatedAt: this.updatedAt,
562       ownerAccount: undefined,
563       viewsPerDay: viewsPerDay !== undefined
564         ? viewsPerDay.split(',').map(v => {
565           const o = v.split('|')
566           return {
567             date: new Date(o[0]),
568             views: +o[1]
569           }
570         })
571         : undefined
572     }
573
574     if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
575
576     return Object.assign(actor, videoChannel)
577   }
578
579   toActivityPubObject (this: MChannelAP): ActivityPubActor {
580     const obj = this.Actor.toActivityPubObject(this.name)
581
582     return Object.assign(obj, {
583       summary: this.description,
584       support: this.support,
585       attributedTo: [
586         {
587           type: 'Person' as 'Person',
588           id: this.Account.Actor.url
589         }
590       ]
591     })
592   }
593
594   getDisplayName () {
595     return this.name
596   }
597
598   isOutdated () {
599     return this.Actor.isOutdated()
600   }
601 }