]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Add username near the display name for account pages
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4 1import * as Sequelize from 'sequelize'
50d6de9c
C
2import { ActivityPubActorType } from '../../shared/models/activitypub'
3import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
3fd3ab2d
C
4import { AccountModel } from '../models/account/account'
5import { UserModel } from '../models/account/user'
50d6de9c 6import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
72c7248b 7import { createVideoChannel } from './video-channel'
6b738c7a 8import { VideoChannelModel } from '../models/video/video-channel'
72c7248b 9
f05a1c30
C
10async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
11 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
72c7248b
C
12 const userOptions = {
13 transaction: t,
14 validate: validateUser
15 }
16
f05a1c30
C
17 const userCreated = await userToCreate.save(userOptions)
18 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.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
f05a1c30 26 return { user: userCreated, account: accountCreated, videoChannel }
72c7248b 27 })
f5028693 28
50d6de9c
C
29 account.Actor = await setAsyncActorKeys(account.Actor)
30 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
47e0652b 31
6b738c7a 32 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
72c7248b
C
33}
34
50d6de9c
C
35async function createLocalAccountWithoutKeys (
36 name: string,
37 userId: number,
38 applicationId: number,
39 t: Sequelize.Transaction,
40 type: ActivityPubActorType= 'Person'
41) {
54141398 42 const url = getAccountActivityPubUrl(name)
571389d4 43
50d6de9c 44 const actorInstance = buildActorInstance(type, url, name)
fadf619a
C
45 const actorInstanceCreated = await actorInstance.save({ transaction: t })
46
47 const accountInstance = new AccountModel({
48 name,
571389d4
C
49 userId,
50 applicationId,
c39ea24b 51 actorId: actorInstanceCreated.id
571389d4
C
52 })
53
fadf619a
C
54 const accountInstanceCreated = await accountInstance.save({ transaction: t })
55 accountInstanceCreated.Actor = actorInstanceCreated
56
57 return accountInstanceCreated
571389d4
C
58}
59
50d6de9c
C
60async function createApplicationActor (applicationId: number) {
61 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
62
63 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
64
65 return accountCreated
66}
67
72c7248b
C
68// ---------------------------------------------------------------------------
69
70export {
50d6de9c 71 createApplicationActor,
571389d4 72 createUserAccountAndChannel,
47e0652b 73 createLocalAccountWithoutKeys
72c7248b 74}