user.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as Sequelize from 'sequelize'
  2. import * as uuidv4 from 'uuid/v4'
  3. import { ActivityPubActorType } from '../../shared/models/activitypub'
  4. import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
  5. import { AccountModel } from '../models/account/account'
  6. import { UserModel } from '../models/account/user'
  7. import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
  8. import { createVideoChannel } from './video-channel'
  9. import { VideoChannelModel } from '../models/video/video-channel'
  10. import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
  11. import { ActorModel } from '../models/activitypub/actor'
  12. async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
  13. const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
  14. const userOptions = {
  15. transaction: t,
  16. validate: validateUser
  17. }
  18. const userCreated = await userToCreate.save(userOptions)
  19. const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
  20. userCreated.Account = accountCreated
  21. let channelName = userCreated.username + '_channel'
  22. // Conflict, generate uuid instead
  23. const actor = await ActorModel.loadLocalByName(channelName)
  24. if (actor) channelName = uuidv4()
  25. const videoChannelDisplayName = `Main ${userCreated.username} channel`
  26. const videoChannelInfo = {
  27. name: channelName,
  28. displayName: videoChannelDisplayName
  29. }
  30. const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
  31. return { user: userCreated, account: accountCreated, videoChannel }
  32. })
  33. const [ accountKeys, channelKeys ] = await Promise.all([
  34. setAsyncActorKeys(account.Actor),
  35. setAsyncActorKeys(videoChannel.Actor)
  36. ])
  37. account.Actor = accountKeys
  38. videoChannel.Actor = channelKeys
  39. return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
  40. }
  41. async function createLocalAccountWithoutKeys (
  42. name: string,
  43. userId: number | null,
  44. applicationId: number | null,
  45. t: Sequelize.Transaction | undefined,
  46. type: ActivityPubActorType= 'Person'
  47. ) {
  48. const url = getAccountActivityPubUrl(name)
  49. const actorInstance = buildActorInstance(type, url, name)
  50. const actorInstanceCreated = await actorInstance.save({ transaction: t })
  51. const accountInstance = new AccountModel({
  52. name,
  53. userId,
  54. applicationId,
  55. actorId: actorInstanceCreated.id
  56. } as FilteredModelAttributes<AccountModel>)
  57. const accountInstanceCreated = await accountInstance.save({ transaction: t })
  58. accountInstanceCreated.Actor = actorInstanceCreated
  59. return accountInstanceCreated
  60. }
  61. async function createApplicationActor (applicationId: number) {
  62. const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
  63. accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
  64. return accountCreated
  65. }
  66. // ---------------------------------------------------------------------------
  67. export {
  68. createApplicationActor,
  69. createUserAccountAndChannel,
  70. createLocalAccountWithoutKeys
  71. }