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