X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fuser.ts;h=f4ffae0e4178fa088d2e98fb3428d64189101977;hb=ce6b3765a2d31b9b90b7a1435e7180b91cba57b3;hp=3f749929675db1bad4e6951f289471cef6460bf6;hpb=c729caf6cc34630877a0e5a1bda1719384cd0c8a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/user.ts b/server/lib/user.ts index 3f7499296..f4ffae0e4 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -1,9 +1,11 @@ import { Transaction } from 'sequelize/types' +import { logger } from '@server/helpers/logger' +import { CONFIG } from '@server/initializers/config' import { UserModel } from '@server/models/user/user' import { MActorDefault } from '@server/types/models/actor' import { buildUUID } from '@shared/extra-utils' import { ActivityPubActorType } from '../../shared/models/activitypub' -import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' +import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users' import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' import { sequelizeTypescript } from '../initializers/database' import { AccountModel } from '../models/account/account' @@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor' import { Redis } from './redis' import { createLocalVideoChannel } from './video-channel' import { createWatchLaterPlaylist } from './video-playlist' -import { logger } from '@server/helpers/logger' type ChannelNames = { name: string, displayName: string } +function buildUser (options: { + username: string + password: string + email: string + + role?: UserRole // Default to UserRole.User + adminFlags?: UserAdminFlag // Default to UserAdminFlag.NONE + + emailVerified: boolean | null + + videoQuota?: number // Default to CONFIG.USER.VIDEO_QUOTA + videoQuotaDaily?: number // Default to CONFIG.USER.VIDEO_QUOTA_DAILY + + pluginAuth?: string +}): MUser { + const { + username, + password, + email, + role = UserRole.USER, + emailVerified, + videoQuota = CONFIG.USER.VIDEO_QUOTA, + videoQuotaDaily = CONFIG.USER.VIDEO_QUOTA_DAILY, + adminFlags = UserAdminFlag.NONE, + pluginAuth + } = options + + return new UserModel({ + username, + password, + email, + + nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, + autoPlayVideo: true, + + role, + emailVerified, + adminFlags, + + videoQuota, + videoQuotaDaily, + + pluginAuth + }) +} + async function createUserAccountAndChannelAndPlaylist (parameters: { userToCreate: MUser userDisplayName?: string @@ -99,7 +147,7 @@ async function createApplicationActor (applicationId: number) { const accountCreated = await createLocalAccountWithoutKeys({ name: SERVER_ACTOR_NAME, userId: null, - applicationId: applicationId, + applicationId, t: undefined, type: 'Application' }) @@ -118,14 +166,15 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { const email = isPendingEmail ? user.pendingEmail : user.email const username = user.username - await Emailer.Instance.addVerifyEmailJob(username, email, url) + Emailer.Instance.addVerifyEmailJob(username, email, url) } async function getOriginalVideoFileTotalFromUser (user: MUserId) { // Don't use sequelize because we need to use a sub query const query = UserModel.generateUserQuotaBaseSQL({ withSelect: true, - whereUserId: '$userId' + whereUserId: '$userId', + daily: false }) const base = await UserModel.getTotalRawQuery(query, user.id) @@ -139,7 +188,7 @@ async function getOriginalVideoFileTotalDailyFromUser (user: MUserId) { const query = UserModel.generateUserQuotaBaseSQL({ withSelect: true, whereUserId: '$userId', - where: '"video"."createdAt" > now() - interval \'24 hours\'' + daily: true }) const base = await UserModel.getTotalRawQuery(query, user.id) @@ -180,7 +229,8 @@ export { createUserAccountAndChannelAndPlaylist, createLocalAccountWithoutKeys, sendVerifyUserEmail, - isAbleToUploadVideo + isAbleToUploadVideo, + buildUser } // --------------------------------------------------------------------------- @@ -203,7 +253,8 @@ function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, autoInstanceFollowing: UserNotificationSettingValue.WEB, newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newPluginVersion: UserNotificationSettingValue.WEB + newPluginVersion: UserNotificationSettingValue.WEB, + myVideoStudioEditionFinished: UserNotificationSettingValue.WEB } return UserNotificationSettingModel.create(values, { transaction: t })