]>
Commit | Line | Data |
---|---|---|
571389d4 C |
1 | import * as Sequelize from 'sequelize' |
2 | import { getActivityPubUrl } from '../helpers/activitypub' | |
3 | import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto' | |
72c7248b | 4 | import { database as db } from '../initializers' |
571389d4 | 5 | import { CONFIG } from '../initializers/constants' |
72c7248b | 6 | import { UserInstance } from '../models' |
72c7248b C |
7 | import { createVideoChannel } from './video-channel' |
8 | ||
e4f97bab | 9 | async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { |
f5028693 | 10 | const res = await db.sequelize.transaction(async t => { |
72c7248b C |
11 | const userOptions = { |
12 | transaction: t, | |
13 | validate: validateUser | |
14 | } | |
15 | ||
f5028693 | 16 | const userCreated = await user.save(userOptions) |
571389d4 | 17 | const accountCreated = await createLocalAccount(user.username, user.id, null, t) |
f5028693 | 18 | |
e34c85e5 | 19 | const videoChannelName = `Default ${userCreated.username} channel` |
f5028693 | 20 | const videoChannelInfo = { |
e34c85e5 | 21 | name: videoChannelName |
f5028693 | 22 | } |
e4f97bab | 23 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
f5028693 | 24 | |
571389d4 | 25 | return { account: accountCreated, videoChannel } |
72c7248b | 26 | }) |
f5028693 C |
27 | |
28 | return res | |
72c7248b C |
29 | } |
30 | ||
571389d4 C |
31 | async function createLocalAccount (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { |
32 | const { publicKey, privateKey } = await createPrivateAndPublicKeys() | |
33 | const url = getActivityPubUrl('account', name) | |
34 | ||
35 | const accountInstance = db.Account.build({ | |
36 | name, | |
37 | url, | |
38 | publicKey, | |
39 | privateKey, | |
40 | followersCount: 0, | |
41 | followingCount: 0, | |
42 | inboxUrl: url + '/inbox', | |
43 | outboxUrl: url + '/outbox', | |
44 | sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', | |
45 | followersUrl: url + '/followers', | |
46 | followingUrl: url + '/following', | |
47 | userId, | |
48 | applicationId, | |
49 | podId: null // It is our pod | |
50 | }) | |
51 | ||
52 | return accountInstance.save({ transaction: t }) | |
53 | } | |
54 | ||
72c7248b C |
55 | // --------------------------------------------------------------------------- |
56 | ||
57 | export { | |
571389d4 C |
58 | createUserAccountAndChannel, |
59 | createLocalAccount | |
72c7248b | 60 | } |