]>
Commit | Line | Data |
---|---|---|
bd45d503 C |
1 | import * as Bluebird from 'bluebird' |
2 | import { values } from 'lodash' | |
5600def4 | 3 | import { col, FindOptions, fn, literal, Op, QueryTypes, where, WhereOptions } from 'sequelize' |
3fd3ab2d | 4 | import { |
d175a6f7 | 5 | AfterDestroy, |
f201a749 | 6 | AfterUpdate, |
a73c582e C |
7 | AllowNull, |
8 | BeforeCreate, | |
9 | BeforeUpdate, | |
10 | Column, | |
11 | CreatedAt, | |
12 | DataType, | |
13 | Default, | |
14 | DefaultScope, | |
15 | HasMany, | |
16 | HasOne, | |
17 | Is, | |
18 | IsEmail, | |
19 | Model, | |
20 | Scopes, | |
21 | Table, | |
22 | UpdatedAt | |
3fd3ab2d | 23 | } from 'sequelize-typescript' |
bd45d503 C |
24 | import { |
25 | MMyUserFormattable, | |
fb719404 | 26 | MUser, |
bd45d503 C |
27 | MUserDefault, |
28 | MUserFormattable, | |
bd45d503 C |
29 | MUserNotifSettingChannelDefault, |
30 | MUserWithNotificationSetting, | |
31 | MVideoFullLight | |
32 | } from '@server/types/models' | |
33 | import { hasUserRight, USER_ROLE_LABELS } from '../../../shared/core-utils/users' | |
34 | import { AbuseState, MyUser, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared/models' | |
ba75d268 | 35 | import { User, UserRole } from '../../../shared/models/users' |
bd45d503 C |
36 | import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' |
37 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' | |
38 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' | |
65fcc311 | 39 | import { |
43d0ea7f | 40 | isNoInstanceConfigWarningModal, |
ac0868bc | 41 | isNoWelcomeModal, |
1eddc9a7 | 42 | isUserAdminFlagsValid, |
bee29df8 | 43 | isUserAutoPlayNextVideoPlaylistValid, |
ac0868bc C |
44 | isUserAutoPlayNextVideoValid, |
45 | isUserAutoPlayVideoValid, | |
eacb25c4 | 46 | isUserBlockedReasonValid, |
e6921918 | 47 | isUserBlockedValid, |
d9eaee39 | 48 | isUserEmailVerifiedValid, |
5cf84858 | 49 | isUserNSFWPolicyValid, |
a73c582e C |
50 | isUserPasswordValid, |
51 | isUserRoleValid, | |
52 | isUserUsernameValid, | |
3caf77d3 | 53 | isUserVideoLanguages, |
5cf84858 | 54 | isUserVideoQuotaDailyValid, |
64cc5e85 | 55 | isUserVideoQuotaValid, |
cef534ed | 56 | isUserVideosHistoryEnabledValid, |
ac0868bc | 57 | isUserWebTorrentEnabledValid |
3fd3ab2d | 58 | } from '../../helpers/custom-validators/users' |
da854ddd | 59 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' |
bd45d503 C |
60 | import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' |
61 | import { clearCacheByUserId } from '../../lib/oauth-model' | |
62 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' | |
63 | import { ActorModel } from '../activitypub/actor' | |
64 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
3fd3ab2d C |
65 | import { OAuthTokenModel } from '../oauth/oauth-token' |
66 | import { getSort, throwIfNotValid } from '../utils' | |
bd45d503 | 67 | import { VideoModel } from '../video/video' |
3fd3ab2d | 68 | import { VideoChannelModel } from '../video/video-channel' |
bd45d503 | 69 | import { VideoImportModel } from '../video/video-import' |
97969c4e | 70 | import { VideoLiveModel } from '../video/video-live' |
29128b2f | 71 | import { VideoPlaylistModel } from '../video/video-playlist' |
3fd3ab2d | 72 | import { AccountModel } from './account' |
cef534ed | 73 | import { UserNotificationSettingModel } from './user-notification-setting' |
3fd3ab2d | 74 | |
9c2e0dbf | 75 | enum ScopeNames { |
76314386 RK |
76 | FOR_ME_API = 'FOR_ME_API', |
77 | WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', | |
78 | WITH_STATS = 'WITH_STATS' | |
9c2e0dbf C |
79 | } |
80 | ||
3acc5084 | 81 | @DefaultScope(() => ({ |
d48ff09d C |
82 | include: [ |
83 | { | |
3acc5084 | 84 | model: AccountModel, |
d48ff09d | 85 | required: true |
cef534ed C |
86 | }, |
87 | { | |
3acc5084 | 88 | model: UserNotificationSettingModel, |
cef534ed | 89 | required: true |
d48ff09d C |
90 | } |
91 | ] | |
3acc5084 C |
92 | })) |
93 | @Scopes(() => ({ | |
ac0868bc | 94 | [ScopeNames.FOR_ME_API]: { |
d48ff09d C |
95 | include: [ |
96 | { | |
3acc5084 | 97 | model: AccountModel, |
ac0868bc C |
98 | include: [ |
99 | { | |
100 | model: VideoChannelModel | |
101 | }, | |
102 | { | |
103 | attributes: [ 'id', 'name', 'type' ], | |
104 | model: VideoPlaylistModel.unscoped(), | |
105 | required: true, | |
106 | where: { | |
107 | type: { | |
a1587156 | 108 | [Op.ne]: VideoPlaylistType.REGULAR |
ac0868bc C |
109 | } |
110 | } | |
111 | } | |
112 | ] | |
cef534ed C |
113 | }, |
114 | { | |
3acc5084 | 115 | model: UserNotificationSettingModel, |
cef534ed | 116 | required: true |
d48ff09d | 117 | } |
3acc5084 | 118 | ] |
76314386 RK |
119 | }, |
120 | [ScopeNames.WITH_VIDEOCHANNELS]: { | |
121 | include: [ | |
122 | { | |
123 | model: AccountModel, | |
124 | include: [ | |
125 | { | |
126 | model: VideoChannelModel | |
127 | }, | |
128 | { | |
129 | attributes: [ 'id', 'name', 'type' ], | |
130 | model: VideoPlaylistModel.unscoped(), | |
131 | required: true, | |
132 | where: { | |
133 | type: { | |
134 | [Op.ne]: VideoPlaylistType.REGULAR | |
135 | } | |
136 | } | |
137 | } | |
138 | ] | |
139 | } | |
140 | ] | |
141 | }, | |
142 | [ScopeNames.WITH_STATS]: { | |
143 | attributes: { | |
144 | include: [ | |
5600def4 C |
145 | [ |
146 | literal( | |
147 | '(' + | |
148 | UserModel.generateUserQuotaBaseSQL({ | |
149 | withSelect: false, | |
150 | whereUserId: '"UserModel"."id"' | |
151 | }) + | |
152 | ')' | |
153 | ), | |
154 | 'videoQuotaUsed' | |
155 | ], | |
76314386 RK |
156 | [ |
157 | literal( | |
158 | '(' + | |
159 | 'SELECT COUNT("video"."id") ' + | |
160 | 'FROM "video" ' + | |
161 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
162 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | |
163 | 'WHERE "account"."userId" = "UserModel"."id"' + | |
164 | ')' | |
165 | ), | |
166 | 'videosCount' | |
167 | ], | |
168 | [ | |
169 | literal( | |
170 | '(' + | |
171 | `SELECT concat_ws(':', "abuses", "acceptedAbuses") ` + | |
172 | 'FROM (' + | |
4f32032f C |
173 | 'SELECT COUNT("abuse"."id") AS "abuses", ' + |
174 | `COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` + | |
175 | 'FROM "abuse" ' + | |
176 | 'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' + | |
76314386 RK |
177 | 'WHERE "account"."userId" = "UserModel"."id"' + |
178 | ') t' + | |
179 | ')' | |
180 | ), | |
4f32032f | 181 | 'abusesCount' |
76314386 RK |
182 | ], |
183 | [ | |
184 | literal( | |
185 | '(' + | |
4f32032f C |
186 | 'SELECT COUNT("abuse"."id") ' + |
187 | 'FROM "abuse" ' + | |
188 | 'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' + | |
76314386 RK |
189 | 'WHERE "account"."userId" = "UserModel"."id"' + |
190 | ')' | |
191 | ), | |
4f32032f | 192 | 'abusesCreatedCount' |
76314386 RK |
193 | ], |
194 | [ | |
195 | literal( | |
196 | '(' + | |
197 | 'SELECT COUNT("videoComment"."id") ' + | |
198 | 'FROM "videoComment" ' + | |
199 | 'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' + | |
200 | 'WHERE "account"."userId" = "UserModel"."id"' + | |
201 | ')' | |
202 | ), | |
203 | 'videoCommentsCount' | |
204 | ] | |
205 | ] | |
206 | } | |
d48ff09d | 207 | } |
3acc5084 | 208 | })) |
3fd3ab2d C |
209 | @Table({ |
210 | tableName: 'user', | |
211 | indexes: [ | |
feb4bdfd | 212 | { |
3fd3ab2d C |
213 | fields: [ 'username' ], |
214 | unique: true | |
feb4bdfd C |
215 | }, |
216 | { | |
3fd3ab2d C |
217 | fields: [ 'email' ], |
218 | unique: true | |
feb4bdfd | 219 | } |
e02643f3 | 220 | ] |
3fd3ab2d C |
221 | }) |
222 | export class UserModel extends Model<UserModel> { | |
223 | ||
7fed6375 | 224 | @AllowNull(true) |
e1c55031 | 225 | @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password', true)) |
3fd3ab2d C |
226 | @Column |
227 | password: string | |
228 | ||
229 | @AllowNull(false) | |
51892fe0 | 230 | @Is('UserUsername', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) |
3fd3ab2d C |
231 | @Column |
232 | username: string | |
233 | ||
234 | @AllowNull(false) | |
235 | @IsEmail | |
236 | @Column(DataType.STRING(400)) | |
237 | email: string | |
238 | ||
d1ab89de C |
239 | @AllowNull(true) |
240 | @IsEmail | |
241 | @Column(DataType.STRING(400)) | |
242 | pendingEmail: string | |
243 | ||
d9eaee39 JM |
244 | @AllowNull(true) |
245 | @Default(null) | |
1735c825 | 246 | @Is('UserEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean', true)) |
d9eaee39 JM |
247 | @Column |
248 | emailVerified: boolean | |
249 | ||
3fd3ab2d | 250 | @AllowNull(false) |
0883b324 | 251 | @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) |
1735c825 | 252 | @Column(DataType.ENUM(...values(NSFW_POLICY_TYPES))) |
0883b324 | 253 | nsfwPolicy: NSFWPolicyType |
3fd3ab2d | 254 | |
64cc5e85 | 255 | @AllowNull(false) |
0229b014 | 256 | @Default(true) |
ed638e53 RK |
257 | @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) |
258 | @Column | |
259 | webTorrentEnabled: boolean | |
64cc5e85 | 260 | |
8b9a525a C |
261 | @AllowNull(false) |
262 | @Default(true) | |
263 | @Is('UserVideosHistoryEnabled', value => throwIfNotValid(value, isUserVideosHistoryEnabledValid, 'Videos history enabled')) | |
264 | @Column | |
265 | videosHistoryEnabled: boolean | |
266 | ||
7efe153b AL |
267 | @AllowNull(false) |
268 | @Default(true) | |
269 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | |
270 | @Column | |
271 | autoPlayVideo: boolean | |
272 | ||
6aa54148 L |
273 | @AllowNull(false) |
274 | @Default(false) | |
275 | @Is('UserAutoPlayNextVideo', value => throwIfNotValid(value, isUserAutoPlayNextVideoValid, 'auto play next video boolean')) | |
276 | @Column | |
277 | autoPlayNextVideo: boolean | |
278 | ||
bee29df8 RK |
279 | @AllowNull(false) |
280 | @Default(true) | |
a1587156 C |
281 | @Is( |
282 | 'UserAutoPlayNextVideoPlaylist', | |
283 | value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean') | |
284 | ) | |
bee29df8 RK |
285 | @Column |
286 | autoPlayNextVideoPlaylist: boolean | |
287 | ||
3caf77d3 C |
288 | @AllowNull(true) |
289 | @Default(null) | |
290 | @Is('UserVideoLanguages', value => throwIfNotValid(value, isUserVideoLanguages, 'video languages')) | |
291 | @Column(DataType.ARRAY(DataType.STRING)) | |
292 | videoLanguages: string[] | |
293 | ||
1eddc9a7 C |
294 | @AllowNull(false) |
295 | @Default(UserAdminFlag.NONE) | |
296 | @Is('UserAdminFlags', value => throwIfNotValid(value, isUserAdminFlagsValid, 'user admin flags')) | |
297 | @Column | |
298 | adminFlags?: UserAdminFlag | |
299 | ||
e6921918 C |
300 | @AllowNull(false) |
301 | @Default(false) | |
302 | @Is('UserBlocked', value => throwIfNotValid(value, isUserBlockedValid, 'blocked boolean')) | |
303 | @Column | |
304 | blocked: boolean | |
305 | ||
eacb25c4 C |
306 | @AllowNull(true) |
307 | @Default(null) | |
1735c825 | 308 | @Is('UserBlockedReason', value => throwIfNotValid(value, isUserBlockedReasonValid, 'blocked reason', true)) |
eacb25c4 C |
309 | @Column |
310 | blockedReason: string | |
311 | ||
3fd3ab2d C |
312 | @AllowNull(false) |
313 | @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role')) | |
314 | @Column | |
315 | role: number | |
316 | ||
317 | @AllowNull(false) | |
318 | @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota')) | |
319 | @Column(DataType.BIGINT) | |
320 | videoQuota: number | |
321 | ||
bee0abff FA |
322 | @AllowNull(false) |
323 | @Is('UserVideoQuotaDaily', value => throwIfNotValid(value, isUserVideoQuotaDailyValid, 'video quota daily')) | |
324 | @Column(DataType.BIGINT) | |
325 | videoQuotaDaily: number | |
326 | ||
7cd4d2ba | 327 | @AllowNull(false) |
3f87a46f | 328 | @Default(DEFAULT_USER_THEME_NAME) |
503c6f44 | 329 | @Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme')) |
7cd4d2ba C |
330 | @Column |
331 | theme: string | |
332 | ||
43d0ea7f C |
333 | @AllowNull(false) |
334 | @Default(false) | |
335 | @Is( | |
336 | 'UserNoInstanceConfigWarningModal', | |
337 | value => throwIfNotValid(value, isNoInstanceConfigWarningModal, 'no instance config warning modal') | |
338 | ) | |
339 | @Column | |
340 | noInstanceConfigWarningModal: boolean | |
341 | ||
342 | @AllowNull(false) | |
343 | @Default(false) | |
344 | @Is( | |
345 | 'UserNoInstanceConfigWarningModal', | |
346 | value => throwIfNotValid(value, isNoWelcomeModal, 'no welcome modal') | |
347 | ) | |
348 | @Column | |
349 | noWelcomeModal: boolean | |
350 | ||
7fed6375 C |
351 | @AllowNull(true) |
352 | @Default(null) | |
353 | @Column | |
354 | pluginAuth: string | |
355 | ||
3cc665f4 C |
356 | @AllowNull(true) |
357 | @Default(null) | |
358 | @Column | |
359 | lastLoginDate: Date | |
360 | ||
3fd3ab2d C |
361 | @CreatedAt |
362 | createdAt: Date | |
363 | ||
364 | @UpdatedAt | |
365 | updatedAt: Date | |
366 | ||
367 | @HasOne(() => AccountModel, { | |
368 | foreignKey: 'userId', | |
f05a1c30 C |
369 | onDelete: 'cascade', |
370 | hooks: true | |
3fd3ab2d C |
371 | }) |
372 | Account: AccountModel | |
69b0a27c | 373 | |
cef534ed C |
374 | @HasOne(() => UserNotificationSettingModel, { |
375 | foreignKey: 'userId', | |
376 | onDelete: 'cascade', | |
377 | hooks: true | |
378 | }) | |
379 | NotificationSetting: UserNotificationSettingModel | |
380 | ||
dc133480 C |
381 | @HasMany(() => VideoImportModel, { |
382 | foreignKey: 'userId', | |
383 | onDelete: 'cascade' | |
384 | }) | |
385 | VideoImports: VideoImportModel[] | |
386 | ||
3fd3ab2d C |
387 | @HasMany(() => OAuthTokenModel, { |
388 | foreignKey: 'userId', | |
389 | onDelete: 'cascade' | |
390 | }) | |
391 | OAuthTokens: OAuthTokenModel[] | |
392 | ||
393 | @BeforeCreate | |
394 | @BeforeUpdate | |
395 | static cryptPasswordIfNeeded (instance: UserModel) { | |
e1c55031 | 396 | if (instance.changed('password') && instance.password) { |
3fd3ab2d C |
397 | return cryptPassword(instance.password) |
398 | .then(hash => { | |
399 | instance.password = hash | |
400 | return undefined | |
401 | }) | |
402 | } | |
59557c46 | 403 | } |
26d7d31b | 404 | |
f201a749 | 405 | @AfterUpdate |
d175a6f7 | 406 | @AfterDestroy |
f201a749 C |
407 | static removeTokenCache (instance: UserModel) { |
408 | return clearCacheByUserId(instance.id) | |
409 | } | |
410 | ||
3fd3ab2d C |
411 | static countTotal () { |
412 | return this.count() | |
413 | } | |
954605a8 | 414 | |
8491293b RK |
415 | static listForApi (parameters: { |
416 | start: number | |
417 | count: number | |
418 | sort: string | |
419 | search?: string | |
420 | blocked?: boolean | |
421 | }) { | |
422 | const { start, count, sort, search, blocked } = parameters | |
423 | const where: WhereOptions = {} | |
a1587156 | 424 | |
24b9417c | 425 | if (search) { |
8491293b | 426 | Object.assign(where, { |
3acc5084 | 427 | [Op.or]: [ |
24b9417c C |
428 | { |
429 | email: { | |
3acc5084 | 430 | [Op.iLike]: '%' + search + '%' |
24b9417c C |
431 | } |
432 | }, | |
433 | { | |
434 | username: { | |
a1587156 | 435 | [Op.iLike]: '%' + search + '%' |
24b9417c C |
436 | } |
437 | } | |
438 | ] | |
8491293b RK |
439 | }) |
440 | } | |
441 | ||
442 | if (blocked !== undefined) { | |
443 | Object.assign(where, { | |
444 | blocked: blocked | |
445 | }) | |
24b9417c C |
446 | } |
447 | ||
3acc5084 | 448 | const query: FindOptions = { |
a76138ff | 449 | attributes: { |
5600def4 C |
450 | include: [ |
451 | [ | |
452 | literal( | |
453 | '(' + | |
454 | UserModel.generateUserQuotaBaseSQL({ | |
455 | withSelect: false, | |
456 | whereUserId: '"UserModel"."id"' | |
457 | }) + | |
458 | ')' | |
459 | ), | |
460 | 'videoQuotaUsed' | |
461 | ] as any // FIXME: typings | |
462 | ] | |
a76138ff | 463 | }, |
3fd3ab2d C |
464 | offset: start, |
465 | limit: count, | |
24b9417c C |
466 | order: getSort(sort), |
467 | where | |
3fd3ab2d | 468 | } |
72c7248b | 469 | |
3fd3ab2d | 470 | return UserModel.findAndCountAll(query) |
a1587156 C |
471 | .then(({ rows, count }) => { |
472 | return { | |
473 | data: rows, | |
474 | total: count | |
475 | } | |
476 | }) | |
72c7248b C |
477 | } |
478 | ||
453e83ea | 479 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { |
ba75d268 | 480 | const roles = Object.keys(USER_ROLE_LABELS) |
a1587156 C |
481 | .map(k => parseInt(k, 10) as UserRole) |
482 | .filter(role => hasUserRight(role, right)) | |
ba75d268 | 483 | |
ba75d268 | 484 | const query = { |
ba75d268 C |
485 | where: { |
486 | role: { | |
3acc5084 | 487 | [Op.in]: roles |
ba75d268 C |
488 | } |
489 | } | |
490 | } | |
491 | ||
cef534ed C |
492 | return UserModel.findAll(query) |
493 | } | |
494 | ||
453e83ea | 495 | static listUserSubscribersOf (actorId: number): Bluebird<MUserWithNotificationSetting[]> { |
cef534ed C |
496 | const query = { |
497 | include: [ | |
498 | { | |
499 | model: UserNotificationSettingModel.unscoped(), | |
500 | required: true | |
501 | }, | |
502 | { | |
503 | attributes: [ 'userId' ], | |
504 | model: AccountModel.unscoped(), | |
505 | required: true, | |
506 | include: [ | |
507 | { | |
a1587156 | 508 | attributes: [], |
cef534ed C |
509 | model: ActorModel.unscoped(), |
510 | required: true, | |
511 | where: { | |
512 | serverId: null | |
513 | }, | |
514 | include: [ | |
515 | { | |
a1587156 | 516 | attributes: [], |
cef534ed C |
517 | as: 'ActorFollowings', |
518 | model: ActorFollowModel.unscoped(), | |
519 | required: true, | |
520 | where: { | |
521 | targetActorId: actorId | |
522 | } | |
523 | } | |
524 | ] | |
525 | } | |
526 | ] | |
527 | } | |
528 | ] | |
529 | } | |
530 | ||
531 | return UserModel.unscoped().findAll(query) | |
ba75d268 C |
532 | } |
533 | ||
453e83ea | 534 | static listByUsernames (usernames: string[]): Bluebird<MUserDefault[]> { |
f7cc67b4 C |
535 | const query = { |
536 | where: { | |
537 | username: usernames | |
538 | } | |
539 | } | |
540 | ||
541 | return UserModel.findAll(query) | |
542 | } | |
543 | ||
fb719404 C |
544 | static loadById (id: number): Bluebird<MUser> { |
545 | return UserModel.unscoped().findByPk(id) | |
546 | } | |
547 | ||
548 | static loadByIdWithChannels (id: number, withStats = false): Bluebird<MUserDefault> { | |
76314386 RK |
549 | const scopes = [ |
550 | ScopeNames.WITH_VIDEOCHANNELS | |
551 | ] | |
552 | ||
553 | if (withStats) scopes.push(ScopeNames.WITH_STATS) | |
554 | ||
555 | return UserModel.scope(scopes).findByPk(id) | |
3fd3ab2d | 556 | } |
feb4bdfd | 557 | |
453e83ea | 558 | static loadByUsername (username: string): Bluebird<MUserDefault> { |
3fd3ab2d C |
559 | const query = { |
560 | where: { | |
a1587156 | 561 | username: { [Op.iLike]: username } |
d48ff09d | 562 | } |
3fd3ab2d | 563 | } |
089ff2f2 | 564 | |
3fd3ab2d | 565 | return UserModel.findOne(query) |
feb4bdfd C |
566 | } |
567 | ||
ac0868bc | 568 | static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> { |
3fd3ab2d C |
569 | const query = { |
570 | where: { | |
a1587156 | 571 | username: { [Op.iLike]: username } |
d48ff09d | 572 | } |
3fd3ab2d | 573 | } |
9bd26629 | 574 | |
ac0868bc | 575 | return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query) |
feb4bdfd C |
576 | } |
577 | ||
453e83ea | 578 | static loadByEmail (email: string): Bluebird<MUserDefault> { |
ecb4e35f C |
579 | const query = { |
580 | where: { | |
581 | ||
582 | } | |
583 | } | |
584 | ||
585 | return UserModel.findOne(query) | |
586 | } | |
587 | ||
453e83ea | 588 | static loadByUsernameOrEmail (username: string, email?: string): Bluebird<MUserDefault> { |
ba12e8b3 C |
589 | if (!email) email = username |
590 | ||
3fd3ab2d | 591 | const query = { |
3fd3ab2d | 592 | where: { |
a1587156 | 593 | [Op.or]: [ |
c4a1811e C |
594 | where(fn('lower', col('username')), fn('lower', username)), |
595 | ||
596 | { email } | |
597 | ] | |
3fd3ab2d | 598 | } |
6fcd19ba | 599 | } |
69b0a27c | 600 | |
d48ff09d | 601 | return UserModel.findOne(query) |
72c7248b C |
602 | } |
603 | ||
453e83ea | 604 | static loadByVideoId (videoId: number): Bluebird<MUserDefault> { |
cef534ed C |
605 | const query = { |
606 | include: [ | |
607 | { | |
608 | required: true, | |
609 | attributes: [ 'id' ], | |
610 | model: AccountModel.unscoped(), | |
611 | include: [ | |
612 | { | |
613 | required: true, | |
614 | attributes: [ 'id' ], | |
615 | model: VideoChannelModel.unscoped(), | |
616 | include: [ | |
617 | { | |
618 | required: true, | |
619 | attributes: [ 'id' ], | |
620 | model: VideoModel.unscoped(), | |
621 | where: { | |
622 | id: videoId | |
623 | } | |
624 | } | |
625 | ] | |
626 | } | |
627 | ] | |
628 | } | |
629 | ] | |
630 | } | |
631 | ||
632 | return UserModel.findOne(query) | |
633 | } | |
634 | ||
453e83ea | 635 | static loadByVideoImportId (videoImportId: number): Bluebird<MUserDefault> { |
dc133480 C |
636 | const query = { |
637 | include: [ | |
638 | { | |
639 | required: true, | |
640 | attributes: [ 'id' ], | |
641 | model: VideoImportModel.unscoped(), | |
642 | where: { | |
643 | id: videoImportId | |
644 | } | |
645 | } | |
646 | ] | |
647 | } | |
648 | ||
649 | return UserModel.findOne(query) | |
650 | } | |
651 | ||
453e83ea | 652 | static loadByChannelActorId (videoChannelActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
653 | const query = { |
654 | include: [ | |
655 | { | |
656 | required: true, | |
657 | attributes: [ 'id' ], | |
658 | model: AccountModel.unscoped(), | |
659 | include: [ | |
660 | { | |
661 | required: true, | |
662 | attributes: [ 'id' ], | |
663 | model: VideoChannelModel.unscoped(), | |
664 | where: { | |
665 | actorId: videoChannelActorId | |
666 | } | |
667 | } | |
668 | ] | |
669 | } | |
670 | ] | |
671 | } | |
672 | ||
673 | return UserModel.findOne(query) | |
674 | } | |
675 | ||
453e83ea | 676 | static loadByAccountActorId (accountActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
677 | const query = { |
678 | include: [ | |
679 | { | |
680 | required: true, | |
681 | attributes: [ 'id' ], | |
682 | model: AccountModel.unscoped(), | |
683 | where: { | |
684 | actorId: accountActorId | |
685 | } | |
686 | } | |
687 | ] | |
688 | } | |
689 | ||
690 | return UserModel.findOne(query) | |
691 | } | |
692 | ||
fb719404 C |
693 | static loadByLiveId (liveId: number): Bluebird<MUser> { |
694 | const query = { | |
695 | include: [ | |
696 | { | |
697 | attributes: [ 'id' ], | |
698 | model: AccountModel.unscoped(), | |
699 | required: true, | |
700 | include: [ | |
701 | { | |
702 | attributes: [ 'id' ], | |
703 | model: VideoChannelModel.unscoped(), | |
704 | required: true, | |
705 | include: [ | |
706 | { | |
707 | attributes: [ 'id' ], | |
708 | model: VideoModel.unscoped(), | |
709 | required: true, | |
710 | include: [ | |
711 | { | |
31c82cd9 | 712 | attributes: [], |
fb719404 C |
713 | model: VideoLiveModel.unscoped(), |
714 | required: true, | |
715 | where: { | |
716 | id: liveId | |
717 | } | |
718 | } | |
719 | ] | |
720 | } | |
721 | ] | |
722 | } | |
723 | ] | |
724 | } | |
725 | ] | |
726 | } | |
727 | ||
31c82cd9 | 728 | return UserModel.unscoped().findOne(query) |
fb719404 C |
729 | } |
730 | ||
731 | static generateUserQuotaBaseSQL (options: { | |
732 | whereUserId: '$userId' | '"UserModel"."id"' | |
733 | withSelect: boolean | |
734 | where?: string | |
735 | }) { | |
736 | const andWhere = options.where | |
737 | ? 'AND ' + options.where | |
738 | : '' | |
739 | ||
740 | const videoChannelJoin = 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
741 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
742 | `WHERE "account"."userId" = ${options.whereUserId} ${andWhere}` | |
743 | ||
744 | const webtorrentFiles = 'SELECT "videoFile"."size" AS "size", "video"."id" AS "videoId" FROM "videoFile" ' + | |
745 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
746 | videoChannelJoin | |
747 | ||
748 | const hlsFiles = 'SELECT "videoFile"."size" AS "size", "video"."id" AS "videoId" FROM "videoFile" ' + | |
749 | 'INNER JOIN "videoStreamingPlaylist" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id ' + | |
750 | 'INNER JOIN "video" ON "videoStreamingPlaylist"."videoId" = "video"."id" ' + | |
751 | videoChannelJoin | |
bee0abff | 752 | |
fb719404 C |
753 | return 'SELECT COALESCE(SUM("size"), 0) AS "total" ' + |
754 | 'FROM (' + | |
755 | `SELECT MAX("t1"."size") AS "size" FROM (${webtorrentFiles} UNION ${hlsFiles}) t1 ` + | |
756 | 'GROUP BY "t1"."videoId"' + | |
757 | ') t2' | |
bee0abff FA |
758 | } |
759 | ||
fb719404 C |
760 | static getTotalRawQuery (query: string, userId: number) { |
761 | const options = { | |
762 | bind: { userId }, | |
763 | type: QueryTypes.SELECT as QueryTypes.SELECT | |
764 | } | |
765 | ||
766 | return UserModel.sequelize.query<{ total: string }>(query, options) | |
767 | .then(([ { total } ]) => { | |
768 | if (total === null) return 0 | |
68a3b9f2 | 769 | |
fb719404 C |
770 | return parseInt(total, 10) |
771 | }) | |
72c7248b C |
772 | } |
773 | ||
09cababd | 774 | static async getStats () { |
3cc665f4 C |
775 | function getActiveUsers (days: number) { |
776 | const query = { | |
777 | where: { | |
778 | [Op.and]: [ | |
779 | literal(`"lastLoginDate" > NOW() - INTERVAL '${days}d'`) | |
780 | ] | |
781 | } | |
782 | } | |
783 | ||
784 | return UserModel.count(query) | |
785 | } | |
786 | ||
09cababd | 787 | const totalUsers = await UserModel.count() |
3cc665f4 C |
788 | const totalDailyActiveUsers = await getActiveUsers(1) |
789 | const totalWeeklyActiveUsers = await getActiveUsers(7) | |
790 | const totalMonthlyActiveUsers = await getActiveUsers(30) | |
09cababd C |
791 | |
792 | return { | |
3cc665f4 C |
793 | totalUsers, |
794 | totalDailyActiveUsers, | |
795 | totalWeeklyActiveUsers, | |
796 | totalMonthlyActiveUsers | |
09cababd C |
797 | } |
798 | } | |
799 | ||
5cf84858 C |
800 | static autoComplete (search: string) { |
801 | const query = { | |
802 | where: { | |
803 | username: { | |
a1587156 | 804 | [Op.like]: `%${search}%` |
5cf84858 C |
805 | } |
806 | }, | |
807 | limit: 10 | |
808 | } | |
809 | ||
810 | return UserModel.findAll(query) | |
811 | .then(u => u.map(u => u.username)) | |
812 | } | |
813 | ||
22a73cb8 | 814 | canGetVideo (video: MVideoFullLight) { |
2a5518a6 | 815 | const videoUserId = video.VideoChannel.Account.userId |
22a73cb8 | 816 | |
2a5518a6 C |
817 | if (video.isBlacklisted()) { |
818 | return videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) | |
22a73cb8 C |
819 | } |
820 | ||
2a5518a6 C |
821 | if (video.privacy === VideoPrivacy.PRIVATE) { |
822 | return video.VideoChannel && videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) | |
22a73cb8 C |
823 | } |
824 | ||
2a5518a6 C |
825 | if (video.privacy === VideoPrivacy.INTERNAL) return true |
826 | ||
22a73cb8 C |
827 | return false |
828 | } | |
829 | ||
3fd3ab2d C |
830 | hasRight (right: UserRight) { |
831 | return hasUserRight(this.role, right) | |
832 | } | |
72c7248b | 833 | |
1eddc9a7 C |
834 | hasAdminFlag (flag: UserAdminFlag) { |
835 | return this.adminFlags & flag | |
836 | } | |
837 | ||
3fd3ab2d C |
838 | isPasswordMatch (password: string) { |
839 | return comparePassword(password, this.password) | |
feb4bdfd C |
840 | } |
841 | ||
ac0868bc | 842 | toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { |
a76138ff | 843 | const videoQuotaUsed = this.get('videoQuotaUsed') |
bee0abff | 844 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') |
76314386 | 845 | const videosCount = this.get('videosCount') |
4f32032f C |
846 | const [ abusesCount, abusesAcceptedCount ] = (this.get('abusesCount') as string || ':').split(':') |
847 | const abusesCreatedCount = this.get('abusesCreatedCount') | |
76314386 | 848 | const videoCommentsCount = this.get('videoCommentsCount') |
a76138ff | 849 | |
ac0868bc | 850 | const json: User = { |
3fd3ab2d C |
851 | id: this.id, |
852 | username: this.username, | |
853 | email: this.email, | |
43d0ea7f C |
854 | theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), |
855 | ||
d1ab89de | 856 | pendingEmail: this.pendingEmail, |
d9eaee39 | 857 | emailVerified: this.emailVerified, |
43d0ea7f | 858 | |
0883b324 | 859 | nsfwPolicy: this.nsfwPolicy, |
ed638e53 | 860 | webTorrentEnabled: this.webTorrentEnabled, |
276d9652 | 861 | videosHistoryEnabled: this.videosHistoryEnabled, |
7efe153b | 862 | autoPlayVideo: this.autoPlayVideo, |
6aa54148 | 863 | autoPlayNextVideo: this.autoPlayNextVideo, |
bee29df8 | 864 | autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist, |
3caf77d3 | 865 | videoLanguages: this.videoLanguages, |
43d0ea7f | 866 | |
3fd3ab2d | 867 | role: this.role, |
a1587156 | 868 | roleLabel: USER_ROLE_LABELS[this.role], |
43d0ea7f | 869 | |
3fd3ab2d | 870 | videoQuota: this.videoQuota, |
bee0abff | 871 | videoQuotaDaily: this.videoQuotaDaily, |
43d0ea7f C |
872 | videoQuotaUsed: videoQuotaUsed !== undefined |
873 | ? parseInt(videoQuotaUsed + '', 10) | |
874 | : undefined, | |
875 | videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined | |
876 | ? parseInt(videoQuotaUsedDaily + '', 10) | |
877 | : undefined, | |
76314386 RK |
878 | videosCount: videosCount !== undefined |
879 | ? parseInt(videosCount + '', 10) | |
880 | : undefined, | |
4f32032f C |
881 | abusesCount: abusesCount |
882 | ? parseInt(abusesCount, 10) | |
76314386 | 883 | : undefined, |
4f32032f C |
884 | abusesAcceptedCount: abusesAcceptedCount |
885 | ? parseInt(abusesAcceptedCount, 10) | |
76314386 | 886 | : undefined, |
4f32032f C |
887 | abusesCreatedCount: abusesCreatedCount !== undefined |
888 | ? parseInt(abusesCreatedCount + '', 10) | |
76314386 RK |
889 | : undefined, |
890 | videoCommentsCount: videoCommentsCount !== undefined | |
891 | ? parseInt(videoCommentsCount + '', 10) | |
892 | : undefined, | |
43d0ea7f C |
893 | |
894 | noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, | |
895 | noWelcomeModal: this.noWelcomeModal, | |
896 | ||
eacb25c4 C |
897 | blocked: this.blocked, |
898 | blockedReason: this.blockedReason, | |
43d0ea7f | 899 | |
c5911fd3 | 900 | account: this.Account.toFormattedJSON(), |
43d0ea7f C |
901 | |
902 | notificationSettings: this.NotificationSetting | |
903 | ? this.NotificationSetting.toFormattedJSON() | |
904 | : undefined, | |
905 | ||
a76138ff | 906 | videoChannels: [], |
43d0ea7f | 907 | |
8bb71f2e C |
908 | createdAt: this.createdAt, |
909 | ||
3cc665f4 C |
910 | pluginAuth: this.pluginAuth, |
911 | ||
912 | lastLoginDate: this.lastLoginDate | |
3fd3ab2d C |
913 | } |
914 | ||
1eddc9a7 C |
915 | if (parameters.withAdminFlags) { |
916 | Object.assign(json, { adminFlags: this.adminFlags }) | |
917 | } | |
918 | ||
3fd3ab2d | 919 | if (Array.isArray(this.Account.VideoChannels) === true) { |
c5911fd3 | 920 | json.videoChannels = this.Account.VideoChannels |
a1587156 C |
921 | .map(c => c.toFormattedJSON()) |
922 | .sort((v1, v2) => { | |
923 | if (v1.createdAt < v2.createdAt) return -1 | |
924 | if (v1.createdAt === v2.createdAt) return 0 | |
ad4a8a1c | 925 | |
a1587156 C |
926 | return 1 |
927 | }) | |
ad4a8a1c | 928 | } |
3fd3ab2d C |
929 | |
930 | return json | |
ad4a8a1c C |
931 | } |
932 | ||
ac0868bc C |
933 | toMeFormattedJSON (this: MMyUserFormattable): MyUser { |
934 | const formatted = this.toFormattedJSON() | |
935 | ||
936 | const specialPlaylists = this.Account.VideoPlaylists | |
a1587156 | 937 | .map(p => ({ id: p.id, name: p.name, type: p.type })) |
ac0868bc C |
938 | |
939 | return Object.assign(formatted, { specialPlaylists }) | |
940 | } | |
b0f9f39e | 941 | } |