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