X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fuser%2Fuser.ts;h=3fd359359c0364e327ab2db691511753206da49a;hb=ba2684ceddf9b76312635b9cddc6bf6975ce436a;hp=20696b1f49fb04d0ed8e9f1ae1186c91d6ff6272;hpb=1e4d2cb5aef11898585fae4053da4ebd0a69b480;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 20696b1f4..3fd359359 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -22,25 +22,23 @@ 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/core-utils' +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' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { - isNoInstanceConfigWarningModal, - isNoWelcomeModal, isUserAdminFlagsValid, isUserAutoPlayNextVideoPlaylistValid, isUserAutoPlayNextVideoValid, @@ -48,15 +46,16 @@ import { isUserBlockedReasonValid, isUserBlockedValid, isUserEmailVerifiedValid, + isUserNoModal, isUserNSFWPolicyValid, + isUserP2PEnabledValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, isUserVideoLanguages, isUserVideoQuotaDailyValid, isUserVideoQuotaValid, - isUserVideosHistoryEnabledValid, - isUserWebTorrentEnabledValid + isUserVideosHistoryEnabledValid } from '../../helpers/custom-validators/users' import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' @@ -66,7 +65,7 @@ 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' @@ -77,6 +76,7 @@ import { UserNotificationSettingModel } from './user-notification-setting' enum ScopeNames { FOR_ME_API = 'FOR_ME_API', WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', + WITH_QUOTA = 'WITH_QUOTA', WITH_STATS = 'WITH_STATS' } @@ -107,7 +107,7 @@ enum ScopeNames { include: [ { model: ActorImageModel, - as: 'Banner', + as: 'Banners', required: false } ] @@ -154,7 +154,7 @@ enum ScopeNames { } ] }, - [ScopeNames.WITH_STATS]: { + [ScopeNames.WITH_QUOTA]: { attributes: { include: [ [ @@ -162,12 +162,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( '(' + @@ -268,10 +287,9 @@ export class UserModel extends Model>> { nsfwPolicy: NSFWPolicyType @AllowNull(false) - @Default(true) - @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) + @Is('p2pEnabled', value => throwIfNotValid(value, isUserP2PEnabledValid, 'P2P enabled')) @Column - webTorrentEnabled: boolean + p2pEnabled: boolean @AllowNull(false) @Default(true) @@ -349,7 +367,7 @@ export class UserModel extends Model>> { @Default(false) @Is( 'UserNoInstanceConfigWarningModal', - value => throwIfNotValid(value, isNoInstanceConfigWarningModal, 'no instance config warning modal') + value => throwIfNotValid(value, isUserNoModal, 'no instance config warning modal') ) @Column noInstanceConfigWarningModal: boolean @@ -357,12 +375,21 @@ export class UserModel extends Model>> { @AllowNull(false) @Default(false) @Is( - 'UserNoInstanceConfigWarningModal', - value => throwIfNotValid(value, isNoWelcomeModal, 'no welcome modal') + 'UserNoWelcomeModal', + value => throwIfNotValid(value, isUserNoModal, 'no welcome modal') ) @Column noWelcomeModal: boolean + @AllowNull(false) + @Default(false) + @Is( + 'UserNoAccountSetupWarningModal', + value => throwIfNotValid(value, isUserNoModal, 'no account setup warning modal') + ) + @Column + noAccountSetupWarningModal: boolean + @AllowNull(true) @Default(null) @Column @@ -433,7 +460,7 @@ export class UserModel extends Model>> { return this.count() } - static listForApi (parameters: { + static listForAdminApi (parameters: { start: number count: number sort: string @@ -461,40 +488,20 @@ 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' - ] as any // FIXME: typings - ] - }, offset: start, limit: count, - order: getSort(sort), + order: getAdminUsersSort(sort), where } - return UserModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + UserModel.unscoped().count(query), + UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA ]).findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } static listWithRight (right: UserRight): Promise { @@ -575,7 +582,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) } @@ -616,7 +626,7 @@ export class UserModel extends Model>> { const query = { where: { [Op.or]: [ - where(fn('lower', col('username')), fn('lower', username)), + where(fn('lower', col('username')), fn('lower', username) as any), { email } ] @@ -756,10 +766,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" ' + @@ -806,10 +816,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) @@ -838,22 +848,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) } @@ -884,7 +878,11 @@ export class UserModel extends Model>> { emailVerified: this.emailVerified, nsfwPolicy: this.nsfwPolicy, - webTorrentEnabled: this.webTorrentEnabled, + + // FIXME: deprecated in 4.1 + webTorrentEnabled: this.p2pEnabled, + p2pEnabled: this.p2pEnabled, + videosHistoryEnabled: this.videosHistoryEnabled, autoPlayVideo: this.autoPlayVideo, autoPlayNextVideo: this.autoPlayNextVideo, @@ -896,12 +894,15 @@ export class UserModel extends Model>> { videoQuota: this.videoQuota, videoQuotaDaily: this.videoQuotaDaily, + videoQuotaUsed: videoQuotaUsed !== undefined - ? parseInt(videoQuotaUsed + '', 10) + ? parseInt(videoQuotaUsed + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined - ? parseInt(videoQuotaUsedDaily + '', 10) + ? parseInt(videoQuotaUsedDaily + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videosCount: videosCount !== undefined ? parseInt(videosCount + '', 10) : undefined, @@ -920,6 +921,7 @@ export class UserModel extends Model>> { noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, noWelcomeModal: this.noWelcomeModal, + noAccountSetupWarningModal: this.noAccountSetupWarningModal, blocked: this.blocked, blockedReason: this.blockedReason, @@ -958,7 +960,7 @@ export class UserModel extends Model>> { } toMeFormattedJSON (this: MMyUserFormattable): MyUser { - const formatted = this.toFormattedJSON() + const formatted = this.toFormattedJSON({ withAdminFlags: true }) const specialPlaylists = this.Account.VideoPlaylists .map(p => ({ id: p.id, name: p.name, type: p.type }))