aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
committerChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
commitd3d3deaa7aae49d6d949aebcba015af75d292ccc (patch)
tree42e487810f5b7362c28f744371d160a4effc7566 /server/lib
parent90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff)
downloadPeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip
Refactor user build and express file middlewares
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/auth/oauth-model.ts20
-rw-r--r--server/lib/user.ts57
2 files changed, 60 insertions, 17 deletions
diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.ts
index 5d68f44e9..910fdeec1 100644
--- a/server/lib/auth/oauth-model.ts
+++ b/server/lib/auth/oauth-model.ts
@@ -5,14 +5,14 @@ import { ActorModel } from '@server/models/actor/actor'
5import { MOAuthClient } from '@server/types/models' 5import { MOAuthClient } from '@server/types/models'
6import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' 6import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
7import { MUser } from '@server/types/models/user/user' 7import { MUser } from '@server/types/models/user/user'
8import { UserAdminFlag } from '@shared/models/users/user-flag.model' 8import { pick } from '@shared/core-utils'
9import { UserRole } from '@shared/models/users/user-role' 9import { UserRole } from '@shared/models/users/user-role'
10import { logger } from '../../helpers/logger' 10import { logger } from '../../helpers/logger'
11import { CONFIG } from '../../initializers/config' 11import { CONFIG } from '../../initializers/config'
12import { OAuthClientModel } from '../../models/oauth/oauth-client' 12import { OAuthClientModel } from '../../models/oauth/oauth-client'
13import { OAuthTokenModel } from '../../models/oauth/oauth-token' 13import { OAuthTokenModel } from '../../models/oauth/oauth-token'
14import { UserModel } from '../../models/user/user' 14import { UserModel } from '../../models/user/user'
15import { createUserAccountAndChannelAndPlaylist } from '../user' 15import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user'
16import { TokensCache } from './tokens-cache' 16import { TokensCache } from './tokens-cache'
17 17
18type TokenInfo = { 18type TokenInfo = {
@@ -229,19 +229,13 @@ async function createUserFromExternal (pluginAuth: string, options: {
229 const actor = await ActorModel.loadLocalByName(options.username) 229 const actor = await ActorModel.loadLocalByName(options.username)
230 if (actor) return null 230 if (actor) return null
231 231
232 const userToCreate = new UserModel({ 232 const userToCreate = buildUser({
233 username: options.username, 233 ...pick(options, [ 'username', 'email', 'role' ]),
234
235 emailVerified: null,
234 password: null, 236 password: null,
235 email: options.email,
236 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
237 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
238 autoPlayVideo: true,
239 role: options.role,
240 videoQuota: CONFIG.USER.VIDEO_QUOTA,
241 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY,
242 adminFlags: UserAdminFlag.NONE,
243 pluginAuth 237 pluginAuth
244 }) as MUser 238 })
245 239
246 const { user } = await createUserAccountAndChannelAndPlaylist({ 240 const { user } = await createUserAccountAndChannelAndPlaylist({
247 userToCreate, 241 userToCreate,
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 @@
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'
@@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor'
19import { Redis } from './redis' 21import { Redis } from './redis'
20import { createLocalVideoChannel } from './video-channel' 22import { createLocalVideoChannel } from './video-channel'
21import { createWatchLaterPlaylist } from './video-playlist' 23import { createWatchLaterPlaylist } from './video-playlist'
22import { logger } from '@server/helpers/logger'
23 24
24type ChannelNames = { name: string, displayName: string } 25type ChannelNames = { name: string, displayName: string }
25 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
26async function createUserAccountAndChannelAndPlaylist (parameters: { 74async 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
124async function getOriginalVideoFileTotalFromUser (user: MUserId) { 172async 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// ---------------------------------------------------------------------------