]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
06a05d5f C |
2 | AllowNull, |
3 | BeforeDestroy, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | Default, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
6dd9de95 | 11 | HasMany, |
06a05d5f C |
12 | Is, |
13 | Model, | |
14 | Scopes, | |
f37dc0dd | 15 | Sequelize, |
06a05d5f C |
16 | Table, |
17 | UpdatedAt | |
3fd3ab2d | 18 | } from 'sequelize-typescript' |
50d6de9c | 19 | import { ActivityPubActor } from '../../../shared/models/activitypub' |
418d092a | 20 | import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' |
2422c46b | 21 | import { |
06a05d5f C |
22 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | |
2422c46b C |
24 | isVideoChannelSupportValid |
25 | } from '../../helpers/custom-validators/video-channels' | |
c5a893d5 | 26 | import { sendDeleteActor } from '../../lib/activitypub/send' |
bfbd9128 | 27 | import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account' |
f37dc0dd | 28 | import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' |
418d092a | 29 | import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils' |
3fd3ab2d | 30 | import { VideoModel } from './video' |
74dc3bca | 31 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
8a19bee1 | 32 | import { ServerModel } from '../server/server' |
8165d00a | 33 | import { FindOptions, Op, literal, ScopeOptions } from 'sequelize' |
418d092a C |
34 | import { AvatarModel } from '../avatar/avatar' |
35 | import { VideoPlaylistModel } from './video-playlist' | |
453e83ea C |
36 | import * as Bluebird from 'bluebird' |
37 | import { | |
38 | MChannelAccountDefault, | |
39 | MChannelActor, | |
b5fecbf4 C |
40 | MChannelActorAccountDefaultVideos, |
41 | MChannelAP, | |
42 | MChannelFormattable, | |
43 | MChannelSummaryFormattable | |
26d6bf65 | 44 | } from '../../types/models/video' |
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', |
418d092a | 51 | WITH_VIDEOS = 'WITH_VIDEOS', |
8165d00a | 52 | WITH_STATS = 'WITH_STATS' |
d48ff09d C |
53 | } |
54 | ||
f37dc0dd C |
55 | type AvailableForListOptions = { |
56 | actorId: number | |
bc99dfe5 | 57 | search?: string |
f37dc0dd C |
58 | } |
59 | ||
8165d00a RK |
60 | type AvailableWithStatsOptions = { |
61 | daysPrior: number | |
62 | } | |
63 | ||
bfbd9128 | 64 | export type SummaryOptions = { |
4f32032f | 65 | actorRequired?: boolean // Default: true |
bfbd9128 C |
66 | withAccount?: boolean // Default: false |
67 | withAccountBlockerIds?: number[] | |
68 | } | |
69 | ||
3acc5084 | 70 | @DefaultScope(() => ({ |
50d6de9c C |
71 | include: [ |
72 | { | |
3acc5084 | 73 | model: ActorModel, |
50d6de9c C |
74 | required: true |
75 | } | |
76 | ] | |
3acc5084 C |
77 | })) |
78 | @Scopes(() => ({ | |
453e83ea | 79 | [ScopeNames.FOR_API]: (options: AvailableForListOptions) => { |
f37dc0dd | 80 | // Only list local channels OR channels that are on an instance followed by actorId |
418d092a | 81 | const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId) |
f37dc0dd C |
82 | |
83 | return { | |
84 | include: [ | |
85 | { | |
86 | attributes: { | |
87 | exclude: unusedActorAttributesForAPI | |
88 | }, | |
89 | model: ActorModel, | |
90 | where: { | |
1735c825 | 91 | [Op.or]: [ |
c305467c | 92 | { |
f37dc0dd C |
93 | serverId: null |
94 | }, | |
95 | { | |
96 | serverId: { | |
a1587156 | 97 | [Op.in]: Sequelize.literal(inQueryInstanceFollow) |
f37dc0dd | 98 | } |
c305467c C |
99 | } |
100 | ] | |
50d6de9c | 101 | } |
f37dc0dd C |
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 | }, | |
8165d00a RK |
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(), | |
4f32032f | 126 | required: options.actorRequired ?? true, |
8165d00a RK |
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 | }, | |
f37dc0dd C |
153 | [ScopeNames.WITH_ACCOUNT]: { |
154 | include: [ | |
155 | { | |
3acc5084 | 156 | model: AccountModel, |
f37dc0dd | 157 | required: true |
d48ff09d C |
158 | } |
159 | ] | |
160 | }, | |
8165d00a | 161 | [ScopeNames.WITH_ACTOR]: { |
d48ff09d | 162 | include: [ |
8165d00a | 163 | ActorModel |
d48ff09d | 164 | ] |
50d6de9c | 165 | }, |
8165d00a | 166 | [ScopeNames.WITH_VIDEOS]: { |
50d6de9c | 167 | include: [ |
8165d00a | 168 | VideoModel |
50d6de9c | 169 | ] |
8165d00a | 170 | }, |
3d527ba1 RK |
171 | [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => { |
172 | const daysPrior = parseInt(options.daysPrior + '', 10) | |
173 | ||
174 | return { | |
175 | attributes: { | |
176 | include: [ | |
1ba471c5 C |
177 | [ |
178 | literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'), | |
179 | 'videosCount' | |
180 | ], | |
3d527ba1 RK |
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 ` + | |
8165d00a | 190 | ') ' + |
5a61ffbb C |
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' + | |
3d527ba1 RK |
200 | ')' |
201 | ), | |
202 | 'viewsPerDay' | |
203 | ] | |
8165d00a | 204 | ] |
3d527ba1 | 205 | } |
8165d00a | 206 | } |
3d527ba1 | 207 | } |
3acc5084 | 208 | })) |
3fd3ab2d C |
209 | @Table({ |
210 | tableName: 'videoChannel', | |
0374b6b5 C |
211 | indexes: [ |
212 | buildTrigramSearchIndex('video_channel_name_trigram', 'name'), | |
213 | ||
214 | { | |
215 | fields: [ 'accountId' ] | |
216 | }, | |
217 | { | |
218 | fields: [ 'actorId' ] | |
219 | } | |
220 | ] | |
3fd3ab2d C |
221 | }) |
222 | export class VideoChannelModel extends Model<VideoChannelModel> { | |
72c7248b | 223 | |
3fd3ab2d C |
224 | @AllowNull(false) |
225 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
226 | @Column | |
227 | name: string | |
72c7248b | 228 | |
3fd3ab2d | 229 | @AllowNull(true) |
2422c46b | 230 | @Default(null) |
1735c825 | 231 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true)) |
a10fc78b | 232 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) |
3fd3ab2d | 233 | description: string |
72c7248b | 234 | |
2422c46b C |
235 | @AllowNull(true) |
236 | @Default(null) | |
1735c825 | 237 | @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true)) |
a10fc78b | 238 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) |
2422c46b C |
239 | support: string |
240 | ||
3fd3ab2d C |
241 | @CreatedAt |
242 | createdAt: Date | |
72c7248b | 243 | |
3fd3ab2d C |
244 | @UpdatedAt |
245 | updatedAt: Date | |
4e50b6a1 | 246 | |
fadf619a C |
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 | ||
3fd3ab2d C |
259 | @ForeignKey(() => AccountModel) |
260 | @Column | |
261 | accountId: number | |
4e50b6a1 | 262 | |
3fd3ab2d C |
263 | @BelongsTo(() => AccountModel, { |
264 | foreignKey: { | |
265 | allowNull: false | |
266 | }, | |
6b738c7a | 267 | hooks: true |
3fd3ab2d C |
268 | }) |
269 | Account: AccountModel | |
72c7248b | 270 | |
3fd3ab2d | 271 | @HasMany(() => VideoModel, { |
72c7248b | 272 | foreignKey: { |
3fd3ab2d | 273 | name: 'channelId', |
72c7248b C |
274 | allowNull: false |
275 | }, | |
f05a1c30 C |
276 | onDelete: 'CASCADE', |
277 | hooks: true | |
72c7248b | 278 | }) |
3fd3ab2d | 279 | Videos: VideoModel[] |
72c7248b | 280 | |
418d092a C |
281 | @HasMany(() => VideoPlaylistModel, { |
282 | foreignKey: { | |
07b1a18a | 283 | allowNull: true |
418d092a | 284 | }, |
df0b219d | 285 | onDelete: 'CASCADE', |
418d092a C |
286 | hooks: true |
287 | }) | |
288 | VideoPlaylists: VideoPlaylistModel[] | |
289 | ||
f05a1c30 C |
290 | @BeforeDestroy |
291 | static async sendDeleteIfOwned (instance: VideoChannelModel, options) { | |
292 | if (!instance.Actor) { | |
e6122097 | 293 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) |
f05a1c30 C |
294 | } |
295 | ||
c5a893d5 | 296 | if (instance.Actor.isOwned()) { |
c5a893d5 C |
297 | return sendDeleteActor(instance.Actor, options.transaction) |
298 | } | |
299 | ||
300 | return undefined | |
3fd3ab2d | 301 | } |
72c7248b | 302 | |
3fd3ab2d C |
303 | static countByAccount (accountId: number) { |
304 | const query = { | |
305 | where: { | |
306 | accountId | |
307 | } | |
72c7248b | 308 | } |
3fd3ab2d C |
309 | |
310 | return VideoChannelModel.count(query) | |
72c7248b C |
311 | } |
312 | ||
bc99dfe5 RK |
313 | static listForApi (parameters: { |
314 | actorId: number | |
315 | start: number | |
316 | count: number | |
317 | sort: string | |
bc99dfe5 | 318 | }) { |
4f5d0459 | 319 | const { actorId } = parameters |
bc99dfe5 | 320 | |
3fd3ab2d | 321 | const query = { |
bc99dfe5 RK |
322 | offset: parameters.start, |
323 | limit: parameters.count, | |
324 | order: getSort(parameters.sort) | |
3fd3ab2d | 325 | } |
72c7248b | 326 | |
f37dc0dd | 327 | const scopes = { |
4f5d0459 | 328 | method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] |
f37dc0dd | 329 | } |
50d6de9c | 330 | return VideoChannelModel |
f37dc0dd C |
331 | .scope(scopes) |
332 | .findAndCountAll(query) | |
333 | .then(({ rows, count }) => { | |
334 | return { total: count, data: rows } | |
335 | }) | |
336 | } | |
337 | ||
453e83ea | 338 | static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> { |
2feebf3e C |
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 | ||
f37dc0dd C |
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: { | |
1735c825 | 379 | [Op.or]: [ |
c3c2ab1c C |
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 + '))' | |
f37dc0dd | 385 | ) |
c3c2ab1c | 386 | ] |
f37dc0dd C |
387 | } |
388 | } | |
389 | ||
390 | const scopes = { | |
453e83ea | 391 | method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ] |
f37dc0dd C |
392 | } |
393 | return VideoChannelModel | |
394 | .scope(scopes) | |
50d6de9c | 395 | .findAndCountAll(query) |
3fd3ab2d C |
396 | .then(({ rows, count }) => { |
397 | return { total: count, data: rows } | |
398 | }) | |
72c7248b C |
399 | } |
400 | ||
91b66319 | 401 | static listByAccount (options: { |
a1587156 C |
402 | accountId: number |
403 | start: number | |
404 | count: number | |
91b66319 | 405 | sort: string |
8165d00a | 406 | withStats?: boolean |
4f5d0459 | 407 | search?: string |
91b66319 | 408 | }) { |
4f5d0459 RK |
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 | ||
3fd3ab2d | 424 | const query = { |
91b66319 C |
425 | offset: options.start, |
426 | limit: options.count, | |
427 | order: getSort(options.sort), | |
3fd3ab2d C |
428 | include: [ |
429 | { | |
430 | model: AccountModel, | |
431 | where: { | |
91b66319 | 432 | id: options.accountId |
3fd3ab2d | 433 | }, |
50d6de9c | 434 | required: true |
3fd3ab2d | 435 | } |
4f5d0459 RK |
436 | ], |
437 | where | |
3fd3ab2d | 438 | } |
72c7248b | 439 | |
8165d00a RK |
440 | const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ] |
441 | ||
5a61ffbb | 442 | if (options.withStats === true) { |
8165d00a RK |
443 | scopes.push({ |
444 | method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ] | |
445 | }) | |
446 | } | |
447 | ||
50d6de9c | 448 | return VideoChannelModel |
8165d00a | 449 | .scope(scopes) |
50d6de9c | 450 | .findAndCountAll(query) |
3fd3ab2d C |
451 | .then(({ rows, count }) => { |
452 | return { total: count, data: rows } | |
453 | }) | |
72c7248b C |
454 | } |
455 | ||
0283eaac | 456 | static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> { |
5cf84858 C |
457 | return VideoChannelModel.unscoped() |
458 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
9b39106d | 459 | .findByPk(id) |
5cf84858 C |
460 | } |
461 | ||
0283eaac | 462 | static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelAccountDefault> { |
8a19bee1 | 463 | const query = { |
3fd3ab2d C |
464 | where: { |
465 | id, | |
466 | accountId | |
d48ff09d | 467 | } |
571389d4 | 468 | } |
3fd3ab2d | 469 | |
5cf84858 | 470 | return VideoChannelModel.unscoped() |
50d6de9c | 471 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
8a19bee1 | 472 | .findOne(query) |
0d0e8dd0 C |
473 | } |
474 | ||
0283eaac | 475 | static loadAndPopulateAccount (id: number): Bluebird<MChannelAccountDefault> { |
5cf84858 | 476 | return VideoChannelModel.unscoped() |
50d6de9c | 477 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
9b39106d | 478 | .findByPk(id) |
3fd3ab2d | 479 | } |
0d0e8dd0 | 480 | |
453e83ea | 481 | static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> { |
f37dc0dd C |
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 ]) | |
8a19bee1 | 496 | .findOne(query) |
72c7248b C |
497 | } |
498 | ||
92bf2f62 C |
499 | static loadByNameWithHostAndPopulateAccount (nameWithHost: string) { |
500 | const [ name, host ] = nameWithHost.split('@') | |
501 | ||
6dd9de95 | 502 | if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name) |
92bf2f62 C |
503 | |
504 | return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) | |
505 | } | |
506 | ||
0283eaac | 507 | static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelAccountDefault> { |
8a19bee1 | 508 | const query = { |
3fd3ab2d | 509 | include: [ |
8a19bee1 C |
510 | { |
511 | model: ActorModel, | |
512 | required: true, | |
513 | where: { | |
514 | preferredUsername: name, | |
515 | serverId: null | |
516 | } | |
517 | } | |
3fd3ab2d C |
518 | ] |
519 | } | |
72c7248b | 520 | |
5cf84858 | 521 | return VideoChannelModel.unscoped() |
8a19bee1 C |
522 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
523 | .findOne(query) | |
72c7248b C |
524 | } |
525 | ||
0283eaac | 526 | static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelAccountDefault> { |
06a05d5f C |
527 | const query = { |
528 | include: [ | |
529 | { | |
530 | model: ActorModel, | |
531 | required: true, | |
532 | where: { | |
8a19bee1 C |
533 | preferredUsername: name |
534 | }, | |
535 | include: [ | |
536 | { | |
537 | model: ServerModel, | |
538 | required: true, | |
539 | where: { host } | |
540 | } | |
541 | ] | |
06a05d5f C |
542 | } |
543 | ] | |
544 | } | |
545 | ||
5cf84858 | 546 | return VideoChannelModel.unscoped() |
8a19bee1 C |
547 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
548 | .findOne(query) | |
549 | } | |
550 | ||
453e83ea | 551 | static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> { |
8a19bee1 C |
552 | const options = { |
553 | include: [ | |
554 | VideoModel | |
555 | ] | |
556 | } | |
557 | ||
5cf84858 | 558 | return VideoChannelModel.unscoped() |
8a19bee1 | 559 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) |
9b39106d | 560 | .findByPk(id, options) |
06a05d5f C |
561 | } |
562 | ||
1ca9f7c3 C |
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 { | |
1ba471c5 C |
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 | } | |
8165d00a | 593 | |
50d6de9c | 594 | const actor = this.Actor.toFormattedJSON() |
6b738c7a | 595 | const videoChannel = { |
3fd3ab2d | 596 | id: this.id, |
749c7247 | 597 | displayName: this.getDisplayName(), |
3fd3ab2d | 598 | description: this.description, |
2422c46b | 599 | support: this.support, |
50d6de9c | 600 | isLocal: this.Actor.isOwned(), |
3fd3ab2d | 601 | createdAt: this.createdAt, |
6b738c7a | 602 | updatedAt: this.updatedAt, |
8165d00a | 603 | ownerAccount: undefined, |
1ba471c5 C |
604 | videosCount, |
605 | viewsPerDay | |
6b738c7a C |
606 | } |
607 | ||
a4f99a76 | 608 | if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() |
72c7248b | 609 | |
6b738c7a | 610 | return Object.assign(actor, videoChannel) |
72c7248b C |
611 | } |
612 | ||
b5fecbf4 | 613 | toActivityPubObject (this: MChannelAP): ActivityPubActor { |
8424c402 | 614 | const obj = this.Actor.toActivityPubObject(this.name) |
50d6de9c C |
615 | |
616 | return Object.assign(obj, { | |
617 | summary: this.description, | |
2422c46b | 618 | support: this.support, |
50d6de9c C |
619 | attributedTo: [ |
620 | { | |
621 | type: 'Person' as 'Person', | |
622 | id: this.Account.Actor.url | |
623 | } | |
624 | ] | |
625 | }) | |
72c7248b | 626 | } |
749c7247 C |
627 | |
628 | getDisplayName () { | |
629 | return this.name | |
630 | } | |
744d0eca C |
631 | |
632 | isOutdated () { | |
633 | return this.Actor.isOutdated() | |
634 | } | |
72c7248b | 635 | } |