X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fuser%2Fuser.ts;h=4f6a8fce442ad64340ca101e296a1f80cd1543fd;hb=64aa66c4a61e1c6aa83a775e7af498e288ea82e4;hp=f70feed73aedd13cb0f42ae2ff7cae786649fb15;hpb=2f061e065ab43cc0b73595b619639a92952aeeba;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user/user.ts b/server/models/user/user.ts index f70feed73..4f6a8fce4 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -30,6 +30,7 @@ import { MUserNotifSettingChannelDefault, MUserWithNotificationSetting } from '@server/types/models' +import { forceNumber } 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 } from '../../../shared/models' @@ -63,7 +64,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 { getAdminUsersSort, throwIfNotValid } from '../utils' +import { getAdminUsersSort, throwIfNotValid } from '../shared' import { VideoModel } from '../video/video' import { VideoChannelModel } from '../video/video-channel' import { VideoImportModel } from '../video/video-import' @@ -403,6 +404,11 @@ export class UserModel extends Model>> { @Column lastLoginDate: Date + @AllowNull(false) + @Default(false) + @Column + emailPublic: boolean + @AllowNull(true) @Default(null) @Column @@ -440,16 +446,17 @@ export class UserModel extends Model>> { }) OAuthTokens: OAuthTokenModel[] + // Used if we already set an encrypted password in user model + skipPasswordEncryption = false + @BeforeCreate @BeforeUpdate - static cryptPasswordIfNeeded (instance: UserModel) { - if (instance.changed('password') && instance.password) { - return cryptPassword(instance.password) - .then(hash => { - instance.password = hash - return undefined - }) - } + static async cryptPasswordIfNeeded (instance: UserModel) { + if (instance.skipPasswordEncryption) return + if (!instance.changed('password')) return + if (!instance.password) return + + instance.password = await cryptPassword(instance.password) } @AfterUpdate @@ -459,7 +466,7 @@ export class UserModel extends Model>> { } static countTotal () { - return this.count() + return UserModel.unscoped().count() } static listForAdminApi (parameters: { @@ -548,6 +555,7 @@ export class UserModel extends Model>> { model: ActorFollowModel.unscoped(), required: true, where: { + state: 'accepted', targetActorId: actorId } } @@ -779,12 +787,12 @@ export class UserModel extends Model>> { `WHERE "account"."userId" = ${options.whereUserId} ${andWhere}` const webtorrentFiles = 'SELECT "videoFile"."size" AS "size", "video"."id" AS "videoId" FROM "videoFile" ' + - 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" AND "video"."isLive" IS FALSE ' + videoChannelJoin const hlsFiles = 'SELECT "videoFile"."size" AS "size", "video"."id" AS "videoId" FROM "videoFile" ' + 'INNER JOIN "videoStreamingPlaylist" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id ' + - 'INNER JOIN "video" ON "videoStreamingPlaylist"."videoId" = "video"."id" ' + + 'INNER JOIN "video" ON "videoStreamingPlaylist"."videoId" = "video"."id" AND "video"."isLive" IS FALSE ' + videoChannelJoin return 'SELECT COALESCE(SUM("size"), 0) AS "total" ' + @@ -877,6 +885,7 @@ export class UserModel extends Model>> { theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), pendingEmail: this.pendingEmail, + emailPublic: this.emailPublic, emailVerified: this.emailVerified, nsfwPolicy: this.nsfwPolicy, @@ -900,27 +909,27 @@ export class UserModel extends Model>> { videoQuotaDaily: this.videoQuotaDaily, videoQuotaUsed: videoQuotaUsed !== undefined - ? parseInt(videoQuotaUsed + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) + ? forceNumber(videoQuotaUsed) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined - ? parseInt(videoQuotaUsedDaily + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) + ? 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,