]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-channel.ts
harmonize search for libraries
[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 }) {
319 const { actorId } = parameters
320
321 const query = {
322 offset: parameters.start,
323 limit: parameters.count,
324 order: getSort(parameters.sort)
325 }
326
327 const scopes = {
328 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
329 }
330 return VideoChannelModel
331 .scope(scopes)
332 .findAndCountAll(query)
333 .then(({ rows, count }) => {
334 return { total: count, data: rows }
335 })
336 }
337
338 static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> {
339 const query = {
340 attributes: [ ],
341 offset: 0,
342 order: getSort(sort),
343 include: [
344 {
345 attributes: [ 'preferredUsername', 'serverId' ],
346 model: ActorModel.unscoped(),
347 where: {
348 serverId: null
349 }
350 }
351 ]
352 }
353
354 return VideoChannelModel
355 .unscoped()
356 .findAll(query)
357 }
358
359 static searchForApi (options: {
360 actorId: number
361 search: string
362 start: number
363 count: number
364 sort: string
365 }) {
366 const attributesInclude = []
367 const escapedSearch = VideoModel.sequelize.escape(options.search)
368 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
369 attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
370
371 const query = {
372 attributes: {
373 include: attributesInclude
374 },
375 offset: options.start,
376 limit: options.count,
377 order: getSort(options.sort),
378 where: {
379 [Op.or]: [
380 Sequelize.literal(
381 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
382 ),
383 Sequelize.literal(
384 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
385 )
386 ]
387 }
388 }
389
390 const scopes = {
391 method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
392 }
393 return VideoChannelModel
394 .scope(scopes)
395 .findAndCountAll(query)
396 .then(({ rows, count }) => {
397 return { total: count, data: rows }
398 })
399 }
400
401 static listByAccount (options: {
402 accountId: number
403 start: number
404 count: number
405 sort: string
406 withStats?: boolean
407 search?: string
408 }) {
409 const escapedSearch = VideoModel.sequelize.escape(options.search)
410 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
411 const where = options.search
412 ? {
413 [Op.or]: [
414 Sequelize.literal(
415 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
416 ),
417 Sequelize.literal(
418 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
419 )
420 ]
421 }
422 : null
423
424 const query = {
425 offset: options.start,
426 limit: options.count,
427 order: getSort(options.sort),
428 include: [
429 {
430 model: AccountModel,
431 where: {
432 id: options.accountId
433 },
434 required: true
435 }
436 ],
437 where
438 }
439
440 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ]
441
442 if (options.withStats === true) {
443 scopes.push({
444 method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
445 })
446 }
447
448 return VideoChannelModel
449 .scope(scopes)
450 .findAndCountAll(query)
451 .then(({ rows, count }) => {
452 return { total: count, data: rows }
453 })
454 }
455
456 static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
457 return VideoChannelModel.unscoped()
458 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
459 .findByPk(id)
460 }
461
462 static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelAccountDefault> {
463 const query = {
464 where: {
465 id,
466 accountId
467 }
468 }
469
470 return VideoChannelModel.unscoped()
471 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
472 .findOne(query)
473 }
474
475 static loadAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> {
476 return VideoChannelModel.unscoped()
477 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
478 .findByPk(id)
479 }
480
481 static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> {
482 const query = {
483 include: [
484 {
485 model: ActorModel,
486 required: true,
487 where: {
488 url
489 }
490 }
491 ]
492 }
493
494 return VideoChannelModel
495 .scope([ ScopeNames.WITH_ACCOUNT ])
496 .findOne(query)
497 }
498
499 static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
500 const [ name, host ] = nameWithHost.split('@')
501
502 if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
503
504 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
505 }
506
507 static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelAccountDefault> {
508 const query = {
509 include: [
510 {
511 model: ActorModel,
512 required: true,
513 where: {
514 preferredUsername: name,
515 serverId: null
516 }
517 }
518 ]
519 }
520
521 return VideoChannelModel.unscoped()
522 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
523 .findOne(query)
524 }
525
526 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelAccountDefault> {
527 const query = {
528 include: [
529 {
530 model: ActorModel,
531 required: true,
532 where: {
533 preferredUsername: name
534 },
535 include: [
536 {
537 model: ServerModel,
538 required: true,
539 where: { host }
540 }
541 ]
542 }
543 ]
544 }
545
546 return VideoChannelModel.unscoped()
547 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
548 .findOne(query)
549 }
550
551 static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> {
552 const options = {
553 include: [
554 VideoModel
555 ]
556 }
557
558 return VideoChannelModel.unscoped()
559 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
560 .findByPk(id, options)
561 }
562
563 toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
564 const actor = this.Actor.toFormattedSummaryJSON()
565
566 return {
567 id: this.id,
568 name: actor.name,
569 displayName: this.getDisplayName(),
570 url: actor.url,
571 host: actor.host,
572 avatar: actor.avatar
573 }
574 }
575
576 toFormattedJSON (this: MChannelFormattable): VideoChannel {
577 const viewsPerDayString = this.get('viewsPerDay') as string
578 const videosCount = this.get('videosCount') as number
579
580 let viewsPerDay: { date: Date, views: number }[]
581
582 if (viewsPerDayString) {
583 viewsPerDay = viewsPerDayString.split(',')
584 .map(v => {
585 const [ dateString, amount ] = v.split('|')
586
587 return {
588 date: new Date(dateString),
589 views: +amount
590 }
591 })
592 }
593
594 const actor = this.Actor.toFormattedJSON()
595 const videoChannel = {
596 id: this.id,
597 displayName: this.getDisplayName(),
598 description: this.description,
599 support: this.support,
600 isLocal: this.Actor.isOwned(),
601 createdAt: this.createdAt,
602 updatedAt: this.updatedAt,
603 ownerAccount: undefined,
604 videosCount,
605 viewsPerDay
606 }
607
608 if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
609
610 return Object.assign(actor, videoChannel)
611 }
612
613 toActivityPubObject (this: MChannelAP): ActivityPubActor {
614 const obj = this.Actor.toActivityPubObject(this.name)
615
616 return Object.assign(obj, {
617 summary: this.description,
618 support: this.support,
619 attributedTo: [
620 {
621 type: 'Person' as 'Person',
622 id: this.Account.Actor.url
623 }
624 ]
625 })
626 }
627
628 getDisplayName () {
629 return this.name
630 }
631
632 isOutdated () {
633 return this.Actor.isOutdated()
634 }
635 }