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