X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fuser%2Fuser.ts;h=672728a2a98ab81bdcf9ebfc7e23753b52bec97a;hb=4638cd713dcdd007cd7f49b9a95fa62ac7823e7c;hp=bcf56dfa12d10dc09f6fa8ac89588d3c7cedda65;hpb=d0800f7661f13fabe7bb6f4aa0ea50764f106405;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user/user.ts b/server/models/user/user.ts index bcf56dfa1..672728a2a 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -1,4 +1,3 @@ -import { values } from 'lodash' import { col, FindOptions, fn, literal, Op, QueryTypes, where, WhereOptions } from 'sequelize' import { AfterDestroy, @@ -22,18 +21,18 @@ import { UpdatedAt } from 'sequelize-typescript' import { TokensCache } from '@server/lib/auth/tokens-cache' +import { LiveQuotaStore } from '@server/lib/live' import { MMyUserFormattable, MUser, MUserDefault, MUserFormattable, MUserNotifSettingChannelDefault, - MUserWithNotificationSetting, - MVideoWithRights + MUserWithNotificationSetting } from '@server/types/models' import { AttributesOnly } from '@shared/typescript-utils' import { hasUserRight, USER_ROLE_LABELS } from '../../../shared/core-utils/users' -import { AbuseState, MyUser, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared/models' +import { AbuseState, MyUser, UserRight, VideoPlaylistType } from '../../../shared/models' import { User, UserRole } from '../../../shared/models/users' import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' @@ -51,7 +50,6 @@ import { isUserP2PEnabledValid, isUserPasswordValid, isUserRoleValid, - isUserUsernameValid, isUserVideoLanguages, isUserVideoQuotaDailyValid, isUserVideoQuotaValid, @@ -65,17 +63,19 @@ import { ActorModel } from '../actor/actor' import { ActorFollowModel } from '../actor/actor-follow' import { ActorImageModel } from '../actor/actor-image' import { OAuthTokenModel } from '../oauth/oauth-token' -import { getSort, throwIfNotValid } from '../utils' +import { getAdminUsersSort, throwIfNotValid } from '../utils' import { VideoModel } from '../video/video' import { VideoChannelModel } from '../video/video-channel' import { VideoImportModel } from '../video/video-import' import { VideoLiveModel } from '../video/video-live' import { VideoPlaylistModel } from '../video/video-playlist' import { UserNotificationSettingModel } from './user-notification-setting' +import { forceNumber } from '@shared/core-utils' enum ScopeNames { FOR_ME_API = 'FOR_ME_API', WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', + WITH_QUOTA = 'WITH_QUOTA', WITH_STATS = 'WITH_STATS' } @@ -153,7 +153,7 @@ enum ScopeNames { } ] }, - [ScopeNames.WITH_STATS]: { + [ScopeNames.WITH_QUOTA]: { attributes: { include: [ [ @@ -161,12 +161,31 @@ enum ScopeNames { '(' + UserModel.generateUserQuotaBaseSQL({ withSelect: false, - whereUserId: '"UserModel"."id"' + whereUserId: '"UserModel"."id"', + daily: false }) + ')' ), 'videoQuotaUsed' ], + [ + literal( + '(' + + UserModel.generateUserQuotaBaseSQL({ + withSelect: false, + whereUserId: '"UserModel"."id"', + daily: true + }) + + ')' + ), + 'videoQuotaUsedDaily' + ] + ] + } + }, + [ScopeNames.WITH_STATS]: { + attributes: { + include: [ [ literal( '(' + @@ -241,7 +260,6 @@ export class UserModel extends Model>> { password: string @AllowNull(false) - @Is('UserUsername', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) @Column username: string @@ -263,7 +281,7 @@ export class UserModel extends Model>> { @AllowNull(false) @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) - @Column(DataType.ENUM(...values(NSFW_POLICY_TYPES))) + @Column(DataType.ENUM(...Object.values(NSFW_POLICY_TYPES))) nsfwPolicy: NSFWPolicyType @AllowNull(false) @@ -386,6 +404,11 @@ export class UserModel extends Model>> { @Column lastLoginDate: Date + @AllowNull(true) + @Default(null) + @Column + otpSecret: string + @CreatedAt createdAt: Date @@ -440,7 +463,7 @@ export class UserModel extends Model>> { return this.count() } - static listForApi (parameters: { + static listForAdminApi (parameters: { start: number count: number sort: string @@ -468,36 +491,19 @@ export class UserModel extends Model>> { } if (blocked !== undefined) { - Object.assign(where, { - blocked: blocked - }) + Object.assign(where, { blocked }) } const query: FindOptions = { - attributes: { - include: [ - [ - literal( - '(' + - UserModel.generateUserQuotaBaseSQL({ - withSelect: false, - whereUserId: '"UserModel"."id"' - }) + - ')' - ), - 'videoQuotaUsed' - ] - ] - }, offset: start, limit: count, - order: getSort(sort), + order: getAdminUsersSort(sort), where } return Promise.all([ UserModel.unscoped().count(query), - UserModel.findAll(query) + UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA ]).findAll(query) ]).then(([ total, data ]) => ({ total, data })) } @@ -579,7 +585,10 @@ export class UserModel extends Model>> { ScopeNames.WITH_VIDEOCHANNELS ] - if (withStats) scopes.push(ScopeNames.WITH_STATS) + if (withStats) { + scopes.push(ScopeNames.WITH_QUOTA) + scopes.push(ScopeNames.WITH_STATS) + } return UserModel.scope(scopes).findByPk(id) } @@ -760,10 +769,10 @@ export class UserModel extends Model>> { static generateUserQuotaBaseSQL (options: { whereUserId: '$userId' | '"UserModel"."id"' withSelect: boolean - where?: string + daily: boolean }) { - const andWhere = options.where - ? 'AND ' + options.where + const andWhere = options.daily === true + ? 'AND "video"."createdAt" > now() - interval \'24 hours\'' : '' const videoChannelJoin = 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + @@ -810,10 +819,10 @@ export class UserModel extends Model>> { } } - return UserModel.count(query) + return UserModel.unscoped().count(query) } - const totalUsers = await UserModel.count() + const totalUsers = await UserModel.unscoped().count() const totalDailyActiveUsers = await getActiveUsers(1) const totalWeeklyActiveUsers = await getActiveUsers(7) const totalMonthlyActiveUsers = await getActiveUsers(30) @@ -842,22 +851,6 @@ export class UserModel extends Model>> { .then(u => u.map(u => u.username)) } - canGetVideo (video: MVideoWithRights) { - const videoUserId = video.VideoChannel.Account.userId - - if (video.isBlacklisted()) { - return videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) - } - - if (video.privacy === VideoPrivacy.PRIVATE) { - return video.VideoChannel && videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) - } - - if (video.privacy === VideoPrivacy.INTERNAL) return true - - return false - } - hasRight (right: UserRight) { return hasUserRight(this.role, right) } @@ -899,31 +892,36 @@ export class UserModel extends Model>> { autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist, videoLanguages: this.videoLanguages, - role: this.role, - roleLabel: USER_ROLE_LABELS[this.role], + role: { + id: this.role, + label: USER_ROLE_LABELS[this.role] + }, videoQuota: this.videoQuota, videoQuotaDaily: this.videoQuotaDaily, + videoQuotaUsed: videoQuotaUsed !== undefined - ? parseInt(videoQuotaUsed + '', 10) + ? forceNumber(videoQuotaUsed) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined - ? parseInt(videoQuotaUsedDaily + '', 10) + ? forceNumber(videoQuotaUsedDaily) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videosCount: videosCount !== undefined - ? parseInt(videosCount + '', 10) + ? forceNumber(videosCount) : undefined, abusesCount: abusesCount - ? parseInt(abusesCount, 10) + ? forceNumber(abusesCount) : undefined, abusesAcceptedCount: abusesAcceptedCount - ? parseInt(abusesAcceptedCount, 10) + ? forceNumber(abusesAcceptedCount) : undefined, abusesCreatedCount: abusesCreatedCount !== undefined - ? parseInt(abusesCreatedCount + '', 10) + ? forceNumber(abusesCreatedCount) : undefined, videoCommentsCount: videoCommentsCount !== undefined - ? parseInt(videoCommentsCount + '', 10) + ? forceNumber(videoCommentsCount) : undefined, noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, @@ -945,7 +943,9 @@ export class UserModel extends Model>> { pluginAuth: this.pluginAuth, - lastLoginDate: this.lastLoginDate + lastLoginDate: this.lastLoginDate, + + twoFactorEnabled: !!this.otpSecret } if (parameters.withAdminFlags) {