diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
commit | d3d3deaa7aae49d6d949aebcba015af75d292ccc (patch) | |
tree | 42e487810f5b7362c28f744371d160a4effc7566 /server/lib/user.ts | |
parent | 90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff) | |
download | PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip |
Refactor user build and express file middlewares
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r-- | server/lib/user.ts | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts index 3f7499296..ea755f4be 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,9 +1,11 @@ | |||
1 | import { Transaction } from 'sequelize/types' | 1 | import { Transaction } from 'sequelize/types' |
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
2 | import { UserModel } from '@server/models/user/user' | 4 | import { UserModel } from '@server/models/user/user' |
3 | import { MActorDefault } from '@server/types/models/actor' | 5 | import { MActorDefault } from '@server/types/models/actor' |
4 | import { buildUUID } from '@shared/extra-utils' | 6 | import { buildUUID } from '@shared/extra-utils' |
5 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 7 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
6 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | 8 | import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users' |
7 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' | 9 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
8 | import { sequelizeTypescript } from '../initializers/database' | 10 | import { sequelizeTypescript } from '../initializers/database' |
9 | import { AccountModel } from '../models/account/account' | 11 | import { AccountModel } from '../models/account/account' |
@@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor' | |||
19 | import { Redis } from './redis' | 21 | import { Redis } from './redis' |
20 | import { createLocalVideoChannel } from './video-channel' | 22 | import { createLocalVideoChannel } from './video-channel' |
21 | import { createWatchLaterPlaylist } from './video-playlist' | 23 | import { createWatchLaterPlaylist } from './video-playlist' |
22 | import { logger } from '@server/helpers/logger' | ||
23 | 24 | ||
24 | type ChannelNames = { name: string, displayName: string } | 25 | type ChannelNames = { name: string, displayName: string } |
25 | 26 | ||
27 | function 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 | |||
26 | async function createUserAccountAndChannelAndPlaylist (parameters: { | 74 | async function createUserAccountAndChannelAndPlaylist (parameters: { |
27 | userToCreate: MUser | 75 | userToCreate: MUser |
28 | userDisplayName?: string | 76 | userDisplayName?: string |
@@ -118,7 +166,7 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { | |||
118 | const email = isPendingEmail ? user.pendingEmail : user.email | 166 | const email = isPendingEmail ? user.pendingEmail : user.email |
119 | const username = user.username | 167 | const username = user.username |
120 | 168 | ||
121 | await Emailer.Instance.addVerifyEmailJob(username, email, url) | 169 | Emailer.Instance.addVerifyEmailJob(username, email, url) |
122 | } | 170 | } |
123 | 171 | ||
124 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { | 172 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { |
@@ -180,7 +228,8 @@ export { | |||
180 | createUserAccountAndChannelAndPlaylist, | 228 | createUserAccountAndChannelAndPlaylist, |
181 | createLocalAccountWithoutKeys, | 229 | createLocalAccountWithoutKeys, |
182 | sendVerifyUserEmail, | 230 | sendVerifyUserEmail, |
183 | isAbleToUploadVideo | 231 | isAbleToUploadVideo, |
232 | buildUser | ||
184 | } | 233 | } |
185 | 234 | ||
186 | // --------------------------------------------------------------------------- | 235 | // --------------------------------------------------------------------------- |