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