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