]>
Commit | Line | Data |
---|---|---|
fe19f600 | 1 | import { FindOptions, Includeable, literal, Op, QueryTypes, 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 | ] | |
213e30ef C |
102 | }, |
103 | include: [ | |
104 | { | |
105 | model: ActorImageModel, | |
106 | as: 'Banner', | |
107 | required: false | |
108 | } | |
109 | ] | |
f37dc0dd C |
110 | }, |
111 | { | |
112 | model: AccountModel, | |
113 | required: true, | |
114 | include: [ | |
115 | { | |
116 | attributes: { | |
117 | exclude: unusedActorAttributesForAPI | |
118 | }, | |
119 | model: ActorModel, // Default scope includes avatar and server | |
120 | required: true | |
121 | } | |
122 | ] | |
123 | } | |
124 | ] | |
125 | } | |
126 | }, | |
8165d00a | 127 | [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { |
b49f22d8 C |
128 | const include: Includeable[] = [ |
129 | { | |
130 | attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], | |
131 | model: ActorModel.unscoped(), | |
132 | required: options.actorRequired ?? true, | |
133 | include: [ | |
134 | { | |
135 | attributes: [ 'host' ], | |
136 | model: ServerModel.unscoped(), | |
137 | required: false | |
138 | }, | |
139 | { | |
f4796856 C |
140 | model: ActorImageModel.unscoped(), |
141 | as: 'Avatar', | |
b49f22d8 C |
142 | required: false |
143 | } | |
144 | ] | |
145 | } | |
146 | ] | |
147 | ||
8165d00a | 148 | const base: FindOptions = { |
b49f22d8 | 149 | attributes: [ 'id', 'name', 'description', 'actorId' ] |
8165d00a RK |
150 | } |
151 | ||
152 | if (options.withAccount === true) { | |
b49f22d8 | 153 | include.push({ |
8165d00a RK |
154 | model: AccountModel.scope({ |
155 | method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ] | |
156 | }), | |
157 | required: true | |
158 | }) | |
159 | } | |
160 | ||
b49f22d8 C |
161 | base.include = include |
162 | ||
8165d00a RK |
163 | return base |
164 | }, | |
f37dc0dd C |
165 | [ScopeNames.WITH_ACCOUNT]: { |
166 | include: [ | |
167 | { | |
3acc5084 | 168 | model: AccountModel, |
f37dc0dd | 169 | required: true |
d48ff09d C |
170 | } |
171 | ] | |
172 | }, | |
8165d00a | 173 | [ScopeNames.WITH_ACTOR]: { |
d48ff09d | 174 | include: [ |
8165d00a | 175 | ActorModel |
d48ff09d | 176 | ] |
50d6de9c | 177 | }, |
2cb03dc1 C |
178 | [ScopeNames.WITH_ACTOR_BANNER]: { |
179 | include: [ | |
180 | { | |
181 | model: ActorModel, | |
182 | include: [ | |
183 | { | |
184 | model: ActorImageModel, | |
185 | required: false, | |
186 | as: 'Banner' | |
187 | } | |
188 | ] | |
189 | } | |
190 | ] | |
191 | }, | |
8165d00a | 192 | [ScopeNames.WITH_VIDEOS]: { |
50d6de9c | 193 | include: [ |
8165d00a | 194 | VideoModel |
50d6de9c | 195 | ] |
8165d00a | 196 | }, |
3d527ba1 RK |
197 | [ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => { |
198 | const daysPrior = parseInt(options.daysPrior + '', 10) | |
199 | ||
200 | return { | |
201 | attributes: { | |
202 | include: [ | |
1ba471c5 C |
203 | [ |
204 | literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'), | |
205 | 'videosCount' | |
206 | ], | |
3d527ba1 RK |
207 | [ |
208 | literal( | |
209 | '(' + | |
210 | `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` + | |
211 | 'FROM ( ' + | |
212 | 'WITH ' + | |
213 | 'days AS ( ' + | |
214 | `SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` + | |
215 | `date_trunc('day', now()), '1 day'::interval) AS day ` + | |
8165d00a | 216 | ') ' + |
5a61ffbb C |
217 | 'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' + |
218 | 'FROM days ' + | |
219 | 'LEFT JOIN (' + | |
220 | '"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' + | |
221 | 'AND "video"."channelId" = "VideoChannelModel"."id"' + | |
222 | `) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` + | |
223 | 'GROUP BY day ' + | |
224 | 'ORDER BY day ' + | |
225 | ') t' + | |
3d527ba1 RK |
226 | ')' |
227 | ), | |
228 | 'viewsPerDay' | |
229 | ] | |
8165d00a | 230 | ] |
3d527ba1 | 231 | } |
8165d00a | 232 | } |
3d527ba1 | 233 | } |
3acc5084 | 234 | })) |
3fd3ab2d C |
235 | @Table({ |
236 | tableName: 'videoChannel', | |
0374b6b5 C |
237 | indexes: [ |
238 | buildTrigramSearchIndex('video_channel_name_trigram', 'name'), | |
239 | ||
240 | { | |
241 | fields: [ 'accountId' ] | |
242 | }, | |
243 | { | |
244 | fields: [ 'actorId' ] | |
245 | } | |
246 | ] | |
3fd3ab2d | 247 | }) |
b49f22d8 | 248 | export class VideoChannelModel extends Model { |
72c7248b | 249 | |
3fd3ab2d C |
250 | @AllowNull(false) |
251 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
252 | @Column | |
253 | name: string | |
72c7248b | 254 | |
3fd3ab2d | 255 | @AllowNull(true) |
2422c46b | 256 | @Default(null) |
1735c825 | 257 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true)) |
a10fc78b | 258 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) |
3fd3ab2d | 259 | description: string |
72c7248b | 260 | |
2422c46b C |
261 | @AllowNull(true) |
262 | @Default(null) | |
1735c825 | 263 | @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true)) |
a10fc78b | 264 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) |
2422c46b C |
265 | support: string |
266 | ||
3fd3ab2d C |
267 | @CreatedAt |
268 | createdAt: Date | |
72c7248b | 269 | |
3fd3ab2d C |
270 | @UpdatedAt |
271 | updatedAt: Date | |
4e50b6a1 | 272 | |
fadf619a C |
273 | @ForeignKey(() => ActorModel) |
274 | @Column | |
275 | actorId: number | |
276 | ||
277 | @BelongsTo(() => ActorModel, { | |
278 | foreignKey: { | |
279 | allowNull: false | |
280 | }, | |
281 | onDelete: 'cascade' | |
282 | }) | |
283 | Actor: ActorModel | |
284 | ||
3fd3ab2d C |
285 | @ForeignKey(() => AccountModel) |
286 | @Column | |
287 | accountId: number | |
4e50b6a1 | 288 | |
3fd3ab2d C |
289 | @BelongsTo(() => AccountModel, { |
290 | foreignKey: { | |
291 | allowNull: false | |
292 | }, | |
6b738c7a | 293 | hooks: true |
3fd3ab2d C |
294 | }) |
295 | Account: AccountModel | |
72c7248b | 296 | |
3fd3ab2d | 297 | @HasMany(() => VideoModel, { |
72c7248b | 298 | foreignKey: { |
3fd3ab2d | 299 | name: 'channelId', |
72c7248b C |
300 | allowNull: false |
301 | }, | |
f05a1c30 C |
302 | onDelete: 'CASCADE', |
303 | hooks: true | |
72c7248b | 304 | }) |
3fd3ab2d | 305 | Videos: VideoModel[] |
72c7248b | 306 | |
418d092a C |
307 | @HasMany(() => VideoPlaylistModel, { |
308 | foreignKey: { | |
07b1a18a | 309 | allowNull: true |
418d092a | 310 | }, |
df0b219d | 311 | onDelete: 'CASCADE', |
418d092a C |
312 | hooks: true |
313 | }) | |
314 | VideoPlaylists: VideoPlaylistModel[] | |
315 | ||
f05a1c30 C |
316 | @BeforeDestroy |
317 | static async sendDeleteIfOwned (instance: VideoChannelModel, options) { | |
318 | if (!instance.Actor) { | |
e6122097 | 319 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) |
f05a1c30 C |
320 | } |
321 | ||
2af337c8 C |
322 | await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) |
323 | ||
c5a893d5 | 324 | if (instance.Actor.isOwned()) { |
c5a893d5 C |
325 | return sendDeleteActor(instance.Actor, options.transaction) |
326 | } | |
327 | ||
328 | return undefined | |
3fd3ab2d | 329 | } |
72c7248b | 330 | |
3fd3ab2d C |
331 | static countByAccount (accountId: number) { |
332 | const query = { | |
333 | where: { | |
334 | accountId | |
335 | } | |
72c7248b | 336 | } |
3fd3ab2d C |
337 | |
338 | return VideoChannelModel.count(query) | |
72c7248b C |
339 | } |
340 | ||
fe19f600 RK |
341 | static async getStats () { |
342 | ||
343 | function getActiveVideoChannels (days: number) { | |
344 | const options = { | |
345 | type: QueryTypes.SELECT as QueryTypes.SELECT, | |
346 | raw: true | |
347 | } | |
348 | ||
349 | const query = ` | |
350 | SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count" | |
351 | FROM "videoChannel" AS "VideoChannelModel" | |
352 | INNER JOIN "video" AS "Videos" | |
353 | ON "VideoChannelModel"."id" = "Videos"."channelId" | |
354 | AND ("Videos"."publishedAt" > Now() - interval '${days}d') | |
355 | INNER JOIN "account" AS "Account" | |
356 | ON "VideoChannelModel"."accountId" = "Account"."id" | |
357 | INNER JOIN "actor" AS "Account->Actor" | |
358 | ON "Account"."actorId" = "Account->Actor"."id" | |
359 | AND "Account->Actor"."serverId" IS NULL | |
360 | LEFT OUTER JOIN "server" AS "Account->Actor->Server" | |
361 | ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` | |
362 | ||
363 | return VideoChannelModel.sequelize.query<{ count: string }>(query, options) | |
364 | .then(r => parseInt(r[0].count, 10)) | |
365 | } | |
366 | ||
367 | const totalLocalVideoChannels = await VideoChannelModel.count() | |
368 | const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1) | |
369 | const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7) | |
370 | const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30) | |
371 | const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180) | |
372 | ||
373 | return { | |
374 | totalLocalVideoChannels, | |
375 | totalLocalDailyActiveVideoChannels, | |
376 | totalLocalWeeklyActiveVideoChannels, | |
377 | totalLocalMonthlyActiveVideoChannels, | |
378 | totalHalfYearActiveVideoChannels | |
379 | } | |
380 | } | |
381 | ||
bc99dfe5 RK |
382 | static listForApi (parameters: { |
383 | actorId: number | |
384 | start: number | |
385 | count: number | |
386 | sort: string | |
bc99dfe5 | 387 | }) { |
4f5d0459 | 388 | const { actorId } = parameters |
bc99dfe5 | 389 | |
3fd3ab2d | 390 | const query = { |
bc99dfe5 RK |
391 | offset: parameters.start, |
392 | limit: parameters.count, | |
393 | order: getSort(parameters.sort) | |
3fd3ab2d | 394 | } |
72c7248b | 395 | |
50d6de9c | 396 | return VideoChannelModel |
b49f22d8 C |
397 | .scope({ |
398 | method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] | |
399 | }) | |
f37dc0dd C |
400 | .findAndCountAll(query) |
401 | .then(({ rows, count }) => { | |
402 | return { total: count, data: rows } | |
403 | }) | |
404 | } | |
405 | ||
b49f22d8 | 406 | static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> { |
2feebf3e C |
407 | const query = { |
408 | attributes: [ ], | |
409 | offset: 0, | |
410 | order: getSort(sort), | |
411 | include: [ | |
412 | { | |
413 | attributes: [ 'preferredUsername', 'serverId' ], | |
414 | model: ActorModel.unscoped(), | |
415 | where: { | |
416 | serverId: null | |
417 | } | |
418 | } | |
419 | ] | |
420 | } | |
421 | ||
422 | return VideoChannelModel | |
423 | .unscoped() | |
424 | .findAll(query) | |
425 | } | |
426 | ||
f37dc0dd C |
427 | static searchForApi (options: { |
428 | actorId: number | |
429 | search: string | |
430 | start: number | |
431 | count: number | |
432 | sort: string | |
433 | }) { | |
434 | const attributesInclude = [] | |
435 | const escapedSearch = VideoModel.sequelize.escape(options.search) | |
436 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
437 | attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search)) | |
438 | ||
439 | const query = { | |
440 | attributes: { | |
441 | include: attributesInclude | |
442 | }, | |
443 | offset: options.start, | |
444 | limit: options.count, | |
445 | order: getSort(options.sort), | |
446 | where: { | |
1735c825 | 447 | [Op.or]: [ |
c3c2ab1c C |
448 | Sequelize.literal( |
449 | 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' | |
450 | ), | |
451 | Sequelize.literal( | |
452 | 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' | |
f37dc0dd | 453 | ) |
c3c2ab1c | 454 | ] |
f37dc0dd C |
455 | } |
456 | } | |
457 | ||
f37dc0dd | 458 | return VideoChannelModel |
b49f22d8 C |
459 | .scope({ |
460 | method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ] | |
461 | }) | |
50d6de9c | 462 | .findAndCountAll(query) |
3fd3ab2d C |
463 | .then(({ rows, count }) => { |
464 | return { total: count, data: rows } | |
465 | }) | |
72c7248b C |
466 | } |
467 | ||
91b66319 | 468 | static listByAccount (options: { |
a1587156 C |
469 | accountId: number |
470 | start: number | |
471 | count: number | |
91b66319 | 472 | sort: string |
8165d00a | 473 | withStats?: boolean |
4f5d0459 | 474 | search?: string |
91b66319 | 475 | }) { |
4f5d0459 RK |
476 | const escapedSearch = VideoModel.sequelize.escape(options.search) |
477 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
478 | const where = options.search | |
479 | ? { | |
480 | [Op.or]: [ | |
481 | Sequelize.literal( | |
482 | 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' | |
483 | ), | |
484 | Sequelize.literal( | |
485 | 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' | |
486 | ) | |
487 | ] | |
488 | } | |
489 | : null | |
490 | ||
3fd3ab2d | 491 | const query = { |
91b66319 C |
492 | offset: options.start, |
493 | limit: options.count, | |
494 | order: getSort(options.sort), | |
3fd3ab2d C |
495 | include: [ |
496 | { | |
497 | model: AccountModel, | |
498 | where: { | |
91b66319 | 499 | id: options.accountId |
3fd3ab2d | 500 | }, |
50d6de9c | 501 | required: true |
3fd3ab2d | 502 | } |
4f5d0459 RK |
503 | ], |
504 | where | |
3fd3ab2d | 505 | } |
72c7248b | 506 | |
2cb03dc1 | 507 | const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ] |
8165d00a | 508 | |
5a61ffbb | 509 | if (options.withStats === true) { |
8165d00a RK |
510 | scopes.push({ |
511 | method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ] | |
512 | }) | |
513 | } | |
514 | ||
50d6de9c | 515 | return VideoChannelModel |
8165d00a | 516 | .scope(scopes) |
50d6de9c | 517 | .findAndCountAll(query) |
3fd3ab2d C |
518 | .then(({ rows, count }) => { |
519 | return { total: count, data: rows } | |
520 | }) | |
72c7248b C |
521 | } |
522 | ||
2cb03dc1 | 523 | static loadAndPopulateAccount (id: number): Promise<MChannelBannerAccountDefault> { |
5cf84858 | 524 | return VideoChannelModel.unscoped() |
2cb03dc1 | 525 | .scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ]) |
9b39106d | 526 | .findByPk(id) |
3fd3ab2d | 527 | } |
0d0e8dd0 | 528 | |
2cb03dc1 | 529 | static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> { |
f37dc0dd C |
530 | const query = { |
531 | include: [ | |
532 | { | |
533 | model: ActorModel, | |
534 | required: true, | |
535 | where: { | |
536 | url | |
2cb03dc1 C |
537 | }, |
538 | include: [ | |
539 | { | |
540 | model: ActorImageModel, | |
541 | required: false, | |
542 | as: 'Banner' | |
543 | } | |
544 | ] | |
f37dc0dd C |
545 | } |
546 | ] | |
547 | } | |
548 | ||
549 | return VideoChannelModel | |
550 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
8a19bee1 | 551 | .findOne(query) |
72c7248b C |
552 | } |
553 | ||
92bf2f62 C |
554 | static loadByNameWithHostAndPopulateAccount (nameWithHost: string) { |
555 | const [ name, host ] = nameWithHost.split('@') | |
556 | ||
6dd9de95 | 557 | if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name) |
92bf2f62 C |
558 | |
559 | return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) | |
560 | } | |
561 | ||
2cb03dc1 | 562 | static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> { |
8a19bee1 | 563 | const query = { |
3fd3ab2d | 564 | include: [ |
8a19bee1 C |
565 | { |
566 | model: ActorModel, | |
567 | required: true, | |
568 | where: { | |
569 | preferredUsername: name, | |
570 | serverId: null | |
2cb03dc1 C |
571 | }, |
572 | include: [ | |
573 | { | |
574 | model: ActorImageModel, | |
575 | required: false, | |
576 | as: 'Banner' | |
577 | } | |
578 | ] | |
8a19bee1 | 579 | } |
3fd3ab2d C |
580 | ] |
581 | } | |
72c7248b | 582 | |
5cf84858 | 583 | return VideoChannelModel.unscoped() |
2cb03dc1 | 584 | .scope([ ScopeNames.WITH_ACCOUNT ]) |
8a19bee1 | 585 | .findOne(query) |
72c7248b C |
586 | } |
587 | ||
2cb03dc1 | 588 | static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> { |
06a05d5f C |
589 | const query = { |
590 | include: [ | |
591 | { | |
592 | model: ActorModel, | |
593 | required: true, | |
594 | where: { | |
8a19bee1 C |
595 | preferredUsername: name |
596 | }, | |
597 | include: [ | |
598 | { | |
599 | model: ServerModel, | |
600 | required: true, | |
601 | where: { host } | |
2cb03dc1 C |
602 | }, |
603 | { | |
604 | model: ActorImageModel, | |
605 | required: false, | |
606 | as: 'Banner' | |
8a19bee1 C |
607 | } |
608 | ] | |
06a05d5f C |
609 | } |
610 | ] | |
611 | } | |
612 | ||
5cf84858 | 613 | return VideoChannelModel.unscoped() |
2cb03dc1 | 614 | .scope([ ScopeNames.WITH_ACCOUNT ]) |
8a19bee1 C |
615 | .findOne(query) |
616 | } | |
617 | ||
1ca9f7c3 C |
618 | toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary { |
619 | const actor = this.Actor.toFormattedSummaryJSON() | |
620 | ||
621 | return { | |
622 | id: this.id, | |
623 | name: actor.name, | |
624 | displayName: this.getDisplayName(), | |
625 | url: actor.url, | |
626 | host: actor.host, | |
627 | avatar: actor.avatar | |
628 | } | |
629 | } | |
630 | ||
631 | toFormattedJSON (this: MChannelFormattable): VideoChannel { | |
1ba471c5 C |
632 | const viewsPerDayString = this.get('viewsPerDay') as string |
633 | const videosCount = this.get('videosCount') as number | |
634 | ||
635 | let viewsPerDay: { date: Date, views: number }[] | |
636 | ||
637 | if (viewsPerDayString) { | |
638 | viewsPerDay = viewsPerDayString.split(',') | |
639 | .map(v => { | |
640 | const [ dateString, amount ] = v.split('|') | |
641 | ||
642 | return { | |
643 | date: new Date(dateString), | |
644 | views: +amount | |
645 | } | |
646 | }) | |
647 | } | |
8165d00a | 648 | |
50d6de9c | 649 | const actor = this.Actor.toFormattedJSON() |
6b738c7a | 650 | const videoChannel = { |
3fd3ab2d | 651 | id: this.id, |
749c7247 | 652 | displayName: this.getDisplayName(), |
3fd3ab2d | 653 | description: this.description, |
2422c46b | 654 | support: this.support, |
50d6de9c | 655 | isLocal: this.Actor.isOwned(), |
3fd3ab2d | 656 | createdAt: this.createdAt, |
6b738c7a | 657 | updatedAt: this.updatedAt, |
8165d00a | 658 | ownerAccount: undefined, |
1ba471c5 C |
659 | videosCount, |
660 | viewsPerDay | |
6b738c7a C |
661 | } |
662 | ||
a4f99a76 | 663 | if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() |
72c7248b | 664 | |
6b738c7a | 665 | return Object.assign(actor, videoChannel) |
72c7248b C |
666 | } |
667 | ||
b5fecbf4 | 668 | toActivityPubObject (this: MChannelAP): ActivityPubActor { |
8424c402 | 669 | const obj = this.Actor.toActivityPubObject(this.name) |
50d6de9c C |
670 | |
671 | return Object.assign(obj, { | |
672 | summary: this.description, | |
2422c46b | 673 | support: this.support, |
50d6de9c C |
674 | attributedTo: [ |
675 | { | |
676 | type: 'Person' as 'Person', | |
677 | id: this.Account.Actor.url | |
678 | } | |
679 | ] | |
680 | }) | |
72c7248b | 681 | } |
749c7247 | 682 | |
a59f210f C |
683 | getLocalUrl (this: MAccountActor | MChannelActor) { |
684 | return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername | |
685 | } | |
686 | ||
749c7247 C |
687 | getDisplayName () { |
688 | return this.name | |
689 | } | |
744d0eca C |
690 | |
691 | isOutdated () { | |
692 | return this.Actor.isOutdated() | |
693 | } | |
72c7248b | 694 | } |