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.ts33
1 files changed, 17 insertions, 16 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts
index 0e4007770..d84aff464 100644
--- a/server/lib/user.ts
+++ b/server/lib/user.ts
@@ -2,10 +2,8 @@ import * as uuidv4 from 'uuid/v4'
2import { ActivityPubActorType } from '../../shared/models/activitypub' 2import { ActivityPubActorType } from '../../shared/models/activitypub'
3import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' 3import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
4import { AccountModel } from '../models/account/account' 4import { AccountModel } from '../models/account/account'
5import { UserModel } from '../models/account/user'
6import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' 5import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
7import { createVideoChannel } from './video-channel' 6import { createLocalVideoChannel } from './video-channel'
8import { VideoChannelModel } from '../models/video/video-channel'
9import { ActorModel } from '../models/activitypub/actor' 7import { ActorModel } from '../models/activitypub/actor'
10import { UserNotificationSettingModel } from '../models/account/user-notification-setting' 8import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
11import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' 9import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
@@ -14,14 +12,17 @@ import { sequelizeTypescript } from '../initializers/database'
14import { Transaction } from 'sequelize/types' 12import { Transaction } from 'sequelize/types'
15import { Redis } from './redis' 13import { Redis } from './redis'
16import { Emailer } from './emailer' 14import { Emailer } from './emailer'
15import { MAccountDefault, MActorDefault, MChannelActor } from '../typings/models'
16import { MUser, MUserDefault, MUserId } from '../typings/models/user'
17 17
18type ChannelNames = { name: string, displayName: string } 18type ChannelNames = { name: string, displayName: string }
19
19async function createUserAccountAndChannelAndPlaylist (parameters: { 20async function createUserAccountAndChannelAndPlaylist (parameters: {
20 userToCreate: UserModel, 21 userToCreate: MUser,
21 userDisplayName?: string, 22 userDisplayName?: string,
22 channelNames?: ChannelNames, 23 channelNames?: ChannelNames,
23 validateUser?: boolean 24 validateUser?: boolean
24}) { 25}): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> {
25 const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters 26 const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
26 27
27 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { 28 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
@@ -30,7 +31,7 @@ async function createUserAccountAndChannelAndPlaylist (parameters: {
30 validate: validateUser 31 validate: validateUser
31 } 32 }
32 33
33 const userCreated = await userToCreate.save(userOptions) 34 const userCreated: MUserDefault = await userToCreate.save(userOptions)
34 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) 35 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
35 36
36 const accountCreated = await createLocalAccountWithoutKeys({ 37 const accountCreated = await createLocalAccountWithoutKeys({
@@ -43,22 +44,22 @@ async function createUserAccountAndChannelAndPlaylist (parameters: {
43 userCreated.Account = accountCreated 44 userCreated.Account = accountCreated
44 45
45 const channelAttributes = await buildChannelAttributes(userCreated, channelNames) 46 const channelAttributes = await buildChannelAttributes(userCreated, channelNames)
46 const videoChannel = await createVideoChannel(channelAttributes, accountCreated, t) 47 const videoChannel = await createLocalVideoChannel(channelAttributes, accountCreated, t)
47 48
48 const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) 49 const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
49 50
50 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } 51 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
51 }) 52 })
52 53
53 const [ accountKeys, channelKeys ] = await Promise.all([ 54 const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([
54 setAsyncActorKeys(account.Actor), 55 setAsyncActorKeys(account.Actor),
55 setAsyncActorKeys(videoChannel.Actor) 56 setAsyncActorKeys(videoChannel.Actor)
56 ]) 57 ])
57 58
58 account.Actor = accountKeys 59 account.Actor = accountActorWithKeys
59 videoChannel.Actor = channelKeys 60 videoChannel.Actor = channelActorWithKeys
60 61
61 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } 62 return { user, account, videoChannel }
62} 63}
63 64
64async function createLocalAccountWithoutKeys (parameters: { 65async function createLocalAccountWithoutKeys (parameters: {
@@ -73,7 +74,7 @@ async function createLocalAccountWithoutKeys (parameters: {
73 const url = getAccountActivityPubUrl(name) 74 const url = getAccountActivityPubUrl(name)
74 75
75 const actorInstance = buildActorInstance(type, url, name) 76 const actorInstance = buildActorInstance(type, url, name)
76 const actorInstanceCreated = await actorInstance.save({ transaction: t }) 77 const actorInstanceCreated: MActorDefault = await actorInstance.save({ transaction: t })
77 78
78 const accountInstance = new AccountModel({ 79 const accountInstance = new AccountModel({
79 name: displayName || name, 80 name: displayName || name,
@@ -82,7 +83,7 @@ async function createLocalAccountWithoutKeys (parameters: {
82 actorId: actorInstanceCreated.id 83 actorId: actorInstanceCreated.id
83 }) 84 })
84 85
85 const accountInstanceCreated = await accountInstance.save({ transaction: t }) 86 const accountInstanceCreated: MAccountDefault = await accountInstance.save({ transaction: t })
86 accountInstanceCreated.Actor = actorInstanceCreated 87 accountInstanceCreated.Actor = actorInstanceCreated
87 88
88 return accountInstanceCreated 89 return accountInstanceCreated
@@ -102,7 +103,7 @@ async function createApplicationActor (applicationId: number) {
102 return accountCreated 103 return accountCreated
103} 104}
104 105
105async function sendVerifyUserEmail (user: UserModel, isPendingEmail = false) { 106async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
106 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) 107 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id)
107 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString 108 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString
108 109
@@ -124,7 +125,7 @@ export {
124 125
125// --------------------------------------------------------------------------- 126// ---------------------------------------------------------------------------
126 127
127function createDefaultUserNotificationSettings (user: UserModel, t: Transaction | undefined) { 128function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | undefined) {
128 const values: UserNotificationSetting & { userId: number } = { 129 const values: UserNotificationSetting & { userId: number } = {
129 userId: user.id, 130 userId: user.id,
130 newVideoFromSubscription: UserNotificationSettingValue.WEB, 131 newVideoFromSubscription: UserNotificationSettingValue.WEB,
@@ -143,7 +144,7 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Transaction
143 return UserNotificationSettingModel.create(values, { transaction: t }) 144 return UserNotificationSettingModel.create(values, { transaction: t })
144} 145}
145 146
146async function buildChannelAttributes (user: UserModel, channelNames?: ChannelNames) { 147async function buildChannelAttributes (user: MUser, channelNames?: ChannelNames) {
147 if (channelNames) return channelNames 148 if (channelNames) return channelNames
148 149
149 let channelName = user.username + '_channel' 150 let channelName = user.username + '_channel'