aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-08-15 11:53:26 +0200
committerChocobozzz <me@florianbigard.com>2019-08-19 17:26:35 +0200
commit453e83ea5d81d203ba34bc43cd5c2c750ba40568 (patch)
tree604e02f4343d13a4ba42e1fb7527ba6ab9111712 /server/lib/user.ts
parent13176a07a95984a53cc59aec5217f2ce9806d1bc (diff)
downloadPeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.tar.gz
PeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.tar.zst
PeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.zip
Stronger model typings
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r--server/lib/user.ts26
1 files changed, 14 insertions, 12 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts
index 0e4007770..266974cac 100644
--- a/server/lib/user.ts
+++ b/server/lib/user.ts
@@ -5,7 +5,6 @@ import { AccountModel } from '../models/account/account'
5import { UserModel } from '../models/account/user' 5import { UserModel } from '../models/account/user'
6import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' 6import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
7import { createVideoChannel } from './video-channel' 7import { createVideoChannel } from './video-channel'
8import { VideoChannelModel } from '../models/video/video-channel'
9import { ActorModel } from '../models/activitypub/actor' 8import { ActorModel } from '../models/activitypub/actor'
10import { UserNotificationSettingModel } from '../models/account/user-notification-setting' 9import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
11import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' 10import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
@@ -14,14 +13,17 @@ import { sequelizeTypescript } from '../initializers/database'
14import { Transaction } from 'sequelize/types' 13import { Transaction } from 'sequelize/types'
15import { Redis } from './redis' 14import { Redis } from './redis'
16import { Emailer } from './emailer' 15import { Emailer } from './emailer'
16import { MAccountActor, MActor, MChannelActor } from '../typings/models'
17import { MUser, MUserId, MUserNotifSettingAccount } from '../typings/models/user'
17 18
18type ChannelNames = { name: string, displayName: string } 19type ChannelNames = { name: string, displayName: string }
20
19async function createUserAccountAndChannelAndPlaylist (parameters: { 21async function createUserAccountAndChannelAndPlaylist (parameters: {
20 userToCreate: UserModel, 22 userToCreate: UserModel,
21 userDisplayName?: string, 23 userDisplayName?: string,
22 channelNames?: ChannelNames, 24 channelNames?: ChannelNames,
23 validateUser?: boolean 25 validateUser?: boolean
24}) { 26}): Promise<{ user: MUserNotifSettingAccount, account: MAccountActor, videoChannel: MChannelActor }> {
25 const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters 27 const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
26 28
27 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { 29 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
@@ -30,7 +32,7 @@ async function createUserAccountAndChannelAndPlaylist (parameters: {
30 validate: validateUser 32 validate: validateUser
31 } 33 }
32 34
33 const userCreated = await userToCreate.save(userOptions) 35 const userCreated: MUserNotifSettingAccount = await userToCreate.save(userOptions)
34 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) 36 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
35 37
36 const accountCreated = await createLocalAccountWithoutKeys({ 38 const accountCreated = await createLocalAccountWithoutKeys({
@@ -50,15 +52,15 @@ async function createUserAccountAndChannelAndPlaylist (parameters: {
50 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } 52 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
51 }) 53 })
52 54
53 const [ accountKeys, channelKeys ] = await Promise.all([ 55 const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([
54 setAsyncActorKeys(account.Actor), 56 setAsyncActorKeys(account.Actor),
55 setAsyncActorKeys(videoChannel.Actor) 57 setAsyncActorKeys(videoChannel.Actor)
56 ]) 58 ])
57 59
58 account.Actor = accountKeys 60 account.Actor = accountActorWithKeys
59 videoChannel.Actor = channelKeys 61 videoChannel.Actor = channelActorWithKeys
60 62
61 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } 63 return { user, account, videoChannel }
62} 64}
63 65
64async function createLocalAccountWithoutKeys (parameters: { 66async function createLocalAccountWithoutKeys (parameters: {
@@ -73,7 +75,7 @@ async function createLocalAccountWithoutKeys (parameters: {
73 const url = getAccountActivityPubUrl(name) 75 const url = getAccountActivityPubUrl(name)
74 76
75 const actorInstance = buildActorInstance(type, url, name) 77 const actorInstance = buildActorInstance(type, url, name)
76 const actorInstanceCreated = await actorInstance.save({ transaction: t }) 78 const actorInstanceCreated: MActor = await actorInstance.save({ transaction: t })
77 79
78 const accountInstance = new AccountModel({ 80 const accountInstance = new AccountModel({
79 name: displayName || name, 81 name: displayName || name,
@@ -82,7 +84,7 @@ async function createLocalAccountWithoutKeys (parameters: {
82 actorId: actorInstanceCreated.id 84 actorId: actorInstanceCreated.id
83 }) 85 })
84 86
85 const accountInstanceCreated = await accountInstance.save({ transaction: t }) 87 const accountInstanceCreated: MAccountActor = await accountInstance.save({ transaction: t })
86 accountInstanceCreated.Actor = actorInstanceCreated 88 accountInstanceCreated.Actor = actorInstanceCreated
87 89
88 return accountInstanceCreated 90 return accountInstanceCreated
@@ -102,7 +104,7 @@ async function createApplicationActor (applicationId: number) {
102 return accountCreated 104 return accountCreated
103} 105}
104 106
105async function sendVerifyUserEmail (user: UserModel, isPendingEmail = false) { 107async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
106 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) 108 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id)
107 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString 109 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString
108 110
@@ -124,7 +126,7 @@ export {
124 126
125// --------------------------------------------------------------------------- 127// ---------------------------------------------------------------------------
126 128
127function createDefaultUserNotificationSettings (user: UserModel, t: Transaction | undefined) { 129function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | undefined) {
128 const values: UserNotificationSetting & { userId: number } = { 130 const values: UserNotificationSetting & { userId: number } = {
129 userId: user.id, 131 userId: user.id,
130 newVideoFromSubscription: UserNotificationSettingValue.WEB, 132 newVideoFromSubscription: UserNotificationSettingValue.WEB,
@@ -143,7 +145,7 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Transaction
143 return UserNotificationSettingModel.create(values, { transaction: t }) 145 return UserNotificationSettingModel.create(values, { transaction: t })
144} 146}
145 147
146async function buildChannelAttributes (user: UserModel, channelNames?: ChannelNames) { 148async function buildChannelAttributes (user: MUser, channelNames?: ChannelNames) {
147 if (channelNames) return channelNames 149 if (channelNames) return channelNames
148 150
149 let channelName = user.username + '_channel' 151 let channelName = user.username + '_channel'