diff options
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r-- | server/lib/user.ts | 91 |
1 files changed, 67 insertions, 24 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts index 7badb3e72..0e4007770 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as uuidv4 from 'uuid/v4' | 1 | import * as uuidv4 from 'uuid/v4' |
3 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 2 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
4 | import { SERVER_ACTOR_NAME } from '../initializers/constants' | 3 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
5 | import { AccountModel } from '../models/account/account' | 4 | import { AccountModel } from '../models/account/account' |
6 | import { UserModel } from '../models/account/user' | 5 | import { UserModel } from '../models/account/user' |
7 | import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' | 6 | import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' |
@@ -12,8 +11,19 @@ import { UserNotificationSettingModel } from '../models/account/user-notificatio | |||
12 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | 11 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' |
13 | import { createWatchLaterPlaylist } from './video-playlist' | 12 | import { createWatchLaterPlaylist } from './video-playlist' |
14 | import { sequelizeTypescript } from '../initializers/database' | 13 | import { sequelizeTypescript } from '../initializers/database' |
14 | import { Transaction } from 'sequelize/types' | ||
15 | import { Redis } from './redis' | ||
16 | import { Emailer } from './emailer' | ||
17 | |||
18 | type ChannelNames = { name: string, displayName: string } | ||
19 | async function createUserAccountAndChannelAndPlaylist (parameters: { | ||
20 | userToCreate: UserModel, | ||
21 | userDisplayName?: string, | ||
22 | channelNames?: ChannelNames, | ||
23 | validateUser?: boolean | ||
24 | }) { | ||
25 | const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters | ||
15 | 26 | ||
16 | async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, validateUser = true) { | ||
17 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | 27 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { |
18 | const userOptions = { | 28 | const userOptions = { |
19 | transaction: t, | 29 | transaction: t, |
@@ -23,21 +33,17 @@ async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, | |||
23 | const userCreated = await userToCreate.save(userOptions) | 33 | const userCreated = await userToCreate.save(userOptions) |
24 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) | 34 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) |
25 | 35 | ||
26 | const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t) | 36 | const accountCreated = await createLocalAccountWithoutKeys({ |
37 | name: userCreated.username, | ||
38 | displayName: userDisplayName, | ||
39 | userId: userCreated.id, | ||
40 | applicationId: null, | ||
41 | t: t | ||
42 | }) | ||
27 | userCreated.Account = accountCreated | 43 | userCreated.Account = accountCreated |
28 | 44 | ||
29 | let channelName = userCreated.username + '_channel' | 45 | const channelAttributes = await buildChannelAttributes(userCreated, channelNames) |
30 | 46 | const videoChannel = await createVideoChannel(channelAttributes, accountCreated, t) | |
31 | // Conflict, generate uuid instead | ||
32 | const actor = await ActorModel.loadLocalByName(channelName) | ||
33 | if (actor) channelName = uuidv4() | ||
34 | |||
35 | const videoChannelDisplayName = `Main ${userCreated.username} channel` | ||
36 | const videoChannelInfo = { | ||
37 | name: channelName, | ||
38 | displayName: videoChannelDisplayName | ||
39 | } | ||
40 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) | ||
41 | 47 | ||
42 | const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) | 48 | const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) |
43 | 49 | ||
@@ -55,20 +61,22 @@ async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, | |||
55 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } | 61 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } |
56 | } | 62 | } |
57 | 63 | ||
58 | async function createLocalAccountWithoutKeys ( | 64 | async function createLocalAccountWithoutKeys (parameters: { |
59 | name: string, | 65 | name: string, |
66 | displayName?: string, | ||
60 | userId: number | null, | 67 | userId: number | null, |
61 | applicationId: number | null, | 68 | applicationId: number | null, |
62 | t: Sequelize.Transaction | undefined, | 69 | t: Transaction | undefined, |
63 | type: ActivityPubActorType= 'Person' | 70 | type?: ActivityPubActorType |
64 | ) { | 71 | }) { |
72 | const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters | ||
65 | const url = getAccountActivityPubUrl(name) | 73 | const url = getAccountActivityPubUrl(name) |
66 | 74 | ||
67 | const actorInstance = buildActorInstance(type, url, name) | 75 | const actorInstance = buildActorInstance(type, url, name) |
68 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) | 76 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) |
69 | 77 | ||
70 | const accountInstance = new AccountModel({ | 78 | const accountInstance = new AccountModel({ |
71 | name, | 79 | name: displayName || name, |
72 | userId, | 80 | userId, |
73 | applicationId, | 81 | applicationId, |
74 | actorId: actorInstanceCreated.id | 82 | actorId: actorInstanceCreated.id |
@@ -81,24 +89,42 @@ async function createLocalAccountWithoutKeys ( | |||
81 | } | 89 | } |
82 | 90 | ||
83 | async function createApplicationActor (applicationId: number) { | 91 | async function createApplicationActor (applicationId: number) { |
84 | const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application') | 92 | const accountCreated = await createLocalAccountWithoutKeys({ |
93 | name: SERVER_ACTOR_NAME, | ||
94 | userId: null, | ||
95 | applicationId: applicationId, | ||
96 | t: undefined, | ||
97 | type: 'Application' | ||
98 | }) | ||
85 | 99 | ||
86 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) | 100 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) |
87 | 101 | ||
88 | return accountCreated | 102 | return accountCreated |
89 | } | 103 | } |
90 | 104 | ||
105 | async function sendVerifyUserEmail (user: UserModel, isPendingEmail = false) { | ||
106 | const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) | ||
107 | let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString | ||
108 | |||
109 | if (isPendingEmail) url += '&isPendingEmail=true' | ||
110 | |||
111 | const email = isPendingEmail ? user.pendingEmail : user.email | ||
112 | |||
113 | await Emailer.Instance.addVerifyEmailJob(email, url) | ||
114 | } | ||
115 | |||
91 | // --------------------------------------------------------------------------- | 116 | // --------------------------------------------------------------------------- |
92 | 117 | ||
93 | export { | 118 | export { |
94 | createApplicationActor, | 119 | createApplicationActor, |
95 | createUserAccountAndChannelAndPlaylist, | 120 | createUserAccountAndChannelAndPlaylist, |
96 | createLocalAccountWithoutKeys | 121 | createLocalAccountWithoutKeys, |
122 | sendVerifyUserEmail | ||
97 | } | 123 | } |
98 | 124 | ||
99 | // --------------------------------------------------------------------------- | 125 | // --------------------------------------------------------------------------- |
100 | 126 | ||
101 | function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) { | 127 | function createDefaultUserNotificationSettings (user: UserModel, t: Transaction | undefined) { |
102 | const values: UserNotificationSetting & { userId: number } = { | 128 | const values: UserNotificationSetting & { userId: number } = { |
103 | userId: user.id, | 129 | userId: user.id, |
104 | newVideoFromSubscription: UserNotificationSettingValue.WEB, | 130 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
@@ -116,3 +142,20 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Tr | |||
116 | 142 | ||
117 | return UserNotificationSettingModel.create(values, { transaction: t }) | 143 | return UserNotificationSettingModel.create(values, { transaction: t }) |
118 | } | 144 | } |
145 | |||
146 | async function buildChannelAttributes (user: UserModel, channelNames?: ChannelNames) { | ||
147 | if (channelNames) return channelNames | ||
148 | |||
149 | let channelName = user.username + '_channel' | ||
150 | |||
151 | // Conflict, generate uuid instead | ||
152 | const actor = await ActorModel.loadLocalByName(channelName) | ||
153 | if (actor) channelName = uuidv4() | ||
154 | |||
155 | const videoChannelDisplayName = `Main ${user.username} channel` | ||
156 | |||
157 | return { | ||
158 | name: channelName, | ||
159 | displayName: videoChannelDisplayName | ||
160 | } | ||
161 | } | ||