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