aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r--server/lib/user.ts61
1 files changed, 58 insertions, 3 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts
index 0d292ac90..ea755f4be 100644
--- a/server/lib/user.ts
+++ b/server/lib/user.ts
@@ -1,9 +1,11 @@
1import { Transaction } from 'sequelize/types' 1import { Transaction } from 'sequelize/types'
2import { logger } from '@server/helpers/logger'
3import { CONFIG } from '@server/initializers/config'
2import { UserModel } from '@server/models/user/user' 4import { UserModel } from '@server/models/user/user'
3import { MActorDefault } from '@server/types/models/actor' 5import { MActorDefault } from '@server/types/models/actor'
4import { buildUUID } from '@shared/extra-utils' 6import { buildUUID } from '@shared/extra-utils'
5import { ActivityPubActorType } from '../../shared/models/activitypub' 7import { ActivityPubActorType } from '../../shared/models/activitypub'
6import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' 8import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users'
7import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' 9import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
8import { sequelizeTypescript } from '../initializers/database' 10import { sequelizeTypescript } from '../initializers/database'
9import { AccountModel } from '../models/account/account' 11import { AccountModel } from '../models/account/account'
@@ -22,6 +24,53 @@ import { createWatchLaterPlaylist } from './video-playlist'
22 24
23type ChannelNames = { name: string, displayName: string } 25type ChannelNames = { name: string, displayName: string }
24 26
27function buildUser (options: {
28 username: string
29 password: string
30 email: string
31
32 role?: UserRole // Default to UserRole.User
33 adminFlags?: UserAdminFlag // Default to UserAdminFlag.NONE
34
35 emailVerified: boolean | null
36
37 videoQuota?: number // Default to CONFIG.USER.VIDEO_QUOTA
38 videoQuotaDaily?: number // Default to CONFIG.USER.VIDEO_QUOTA_DAILY
39
40 pluginAuth?: string
41}): MUser {
42 const {
43 username,
44 password,
45 email,
46 role = UserRole.USER,
47 emailVerified,
48 videoQuota = CONFIG.USER.VIDEO_QUOTA,
49 videoQuotaDaily = CONFIG.USER.VIDEO_QUOTA_DAILY,
50 adminFlags = UserAdminFlag.NONE,
51 pluginAuth
52 } = options
53
54 return new UserModel({
55 username,
56 password,
57 email,
58
59 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
60 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
61 autoPlayVideo: true,
62
63 role,
64 emailVerified,
65 adminFlags,
66
67 videoQuota: videoQuota,
68 videoQuotaDaily: videoQuotaDaily,
69
70 pluginAuth
71 })
72}
73
25async function createUserAccountAndChannelAndPlaylist (parameters: { 74async function createUserAccountAndChannelAndPlaylist (parameters: {
26 userToCreate: MUser 75 userToCreate: MUser
27 userDisplayName?: string 76 userDisplayName?: string
@@ -117,7 +166,7 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
117 const email = isPendingEmail ? user.pendingEmail : user.email 166 const email = isPendingEmail ? user.pendingEmail : user.email
118 const username = user.username 167 const username = user.username
119 168
120 await Emailer.Instance.addVerifyEmailJob(username, email, url) 169 Emailer.Instance.addVerifyEmailJob(username, email, url)
121} 170}
122 171
123async function getOriginalVideoFileTotalFromUser (user: MUserId) { 172async function getOriginalVideoFileTotalFromUser (user: MUserId) {
@@ -159,6 +208,11 @@ async function isAbleToUploadVideo (userId: number, newVideoSize: number) {
159 const uploadedTotal = newVideoSize + totalBytes 208 const uploadedTotal = newVideoSize + totalBytes
160 const uploadedDaily = newVideoSize + totalBytesDaily 209 const uploadedDaily = newVideoSize + totalBytesDaily
161 210
211 logger.debug(
212 'Check user %d quota to upload another video.', userId,
213 { totalBytes, totalBytesDaily, videoQuota: user.videoQuota, videoQuotaDaily: user.videoQuotaDaily, newVideoSize }
214 )
215
162 if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota 216 if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota
163 if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily 217 if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily
164 218
@@ -174,7 +228,8 @@ export {
174 createUserAccountAndChannelAndPlaylist, 228 createUserAccountAndChannelAndPlaylist,
175 createLocalAccountWithoutKeys, 229 createLocalAccountWithoutKeys,
176 sendVerifyUserEmail, 230 sendVerifyUserEmail,
177 isAbleToUploadVideo 231 isAbleToUploadVideo,
232 buildUser
178} 233}
179 234
180// --------------------------------------------------------------------------- 235// ---------------------------------------------------------------------------