]>
Commit | Line | Data |
---|---|---|
571389d4 | 1 | import * as Sequelize from 'sequelize' |
3fd3ab2d C |
2 | import { createPrivateAndPublicKeys, logger } from '../helpers' |
3 | import { CONFIG, sequelizeTypescript } from '../initializers' | |
4 | import { AccountModel } from '../models/account/account' | |
5 | import { UserModel } from '../models/account/user' | |
fadf619a | 6 | import { ActorModel } from '../models/activitypub/actor' |
3fd3ab2d | 7 | import { getAccountActivityPubUrl } from './activitypub' |
72c7248b C |
8 | import { createVideoChannel } from './video-channel' |
9 | ||
3fd3ab2d C |
10 | async function createUserAccountAndChannel (user: UserModel, validateUser = true) { |
11 | const { account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
72c7248b C |
12 | const userOptions = { |
13 | transaction: t, | |
14 | validate: validateUser | |
15 | } | |
16 | ||
f5028693 | 17 | const userCreated = await user.save(userOptions) |
47e0652b | 18 | const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t) |
f5028693 | 19 | |
e34c85e5 | 20 | const videoChannelName = `Default ${userCreated.username} channel` |
f5028693 | 21 | const videoChannelInfo = { |
e34c85e5 | 22 | name: videoChannelName |
f5028693 | 23 | } |
e4f97bab | 24 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
f5028693 | 25 | |
571389d4 | 26 | return { account: accountCreated, videoChannel } |
72c7248b | 27 | }) |
f5028693 | 28 | |
47e0652b C |
29 | // Set account keys, this could be long so process after the account creation and do not block the client |
30 | const { publicKey, privateKey } = await createPrivateAndPublicKeys() | |
fadf619a C |
31 | const actor = account.Actor |
32 | actor.set('publicKey', publicKey) | |
33 | actor.set('privateKey', privateKey) | |
34 | actor.save().catch(err => logger.error('Cannot set public/private keys of actor %d.', actor.uuid, err)) | |
47e0652b C |
35 | |
36 | return { account, videoChannel } | |
72c7248b C |
37 | } |
38 | ||
47e0652b | 39 | async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { |
54141398 | 40 | const url = getAccountActivityPubUrl(name) |
571389d4 | 41 | |
fadf619a | 42 | const actorInstance = new ActorModel({ |
571389d4 | 43 | url, |
47e0652b C |
44 | publicKey: null, |
45 | privateKey: null, | |
571389d4 C |
46 | followersCount: 0, |
47 | followingCount: 0, | |
48 | inboxUrl: url + '/inbox', | |
49 | outboxUrl: url + '/outbox', | |
50 | sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', | |
51 | followersUrl: url + '/followers', | |
fadf619a C |
52 | followingUrl: url + '/following' |
53 | }) | |
54 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) | |
55 | ||
56 | const accountInstance = new AccountModel({ | |
57 | name, | |
571389d4 C |
58 | userId, |
59 | applicationId, | |
fadf619a | 60 | actorId: actorInstanceCreated.id, |
60862425 | 61 | serverId: null // It is our server |
571389d4 C |
62 | }) |
63 | ||
fadf619a C |
64 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) |
65 | accountInstanceCreated.Actor = actorInstanceCreated | |
66 | ||
67 | return accountInstanceCreated | |
571389d4 C |
68 | } |
69 | ||
72c7248b C |
70 | // --------------------------------------------------------------------------- |
71 | ||
72 | export { | |
571389d4 | 73 | createUserAccountAndChannel, |
47e0652b | 74 | createLocalAccountWithoutKeys |
72c7248b | 75 | } |