2
1

user.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as uuidv4 from 'uuid/v4'
  2. import { ActivityPubActorType } from '../../shared/models/activitypub'
  3. import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
  4. import { AccountModel } from '../models/account/account'
  5. import { UserModel } from '../models/account/user'
  6. import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
  7. import { createVideoChannel } from './video-channel'
  8. import { VideoChannelModel } from '../models/video/video-channel'
  9. import { ActorModel } from '../models/activitypub/actor'
  10. import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
  11. import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
  12. import { createWatchLaterPlaylist } from './video-playlist'
  13. import { sequelizeTypescript } from '../initializers/database'
  14. import { Transaction } from 'sequelize/types'
  15. import { Redis } from './redis'
  16. import { Emailer } from './emailer'
  17. type ChannelNames = { name: string, displayName: string }
  18. async function createUserAccountAndChannelAndPlaylist (parameters: {
  19. userToCreate: UserModel,
  20. userDisplayName?: string,
  21. channelNames?: ChannelNames,
  22. validateUser?: boolean
  23. }) {
  24. const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
  25. const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
  26. const userOptions = {
  27. transaction: t,
  28. validate: validateUser
  29. }
  30. const userCreated = await userToCreate.save(userOptions)
  31. userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
  32. const accountCreated = await createLocalAccountWithoutKeys({
  33. name: userCreated.username,
  34. displayName: userDisplayName,
  35. userId: userCreated.id,
  36. applicationId: null,
  37. t: t
  38. })
  39. userCreated.Account = accountCreated
  40. const channelAttributes = await buildChannelAttributes(userCreated, channelNames)
  41. const videoChannel = await createVideoChannel(channelAttributes, accountCreated, t)
  42. const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
  43. return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
  44. })
  45. const [ accountKeys, channelKeys ] = await Promise.all([
  46. setAsyncActorKeys(account.Actor),
  47. setAsyncActorKeys(videoChannel.Actor)
  48. ])
  49. account.Actor = accountKeys
  50. videoChannel.Actor = channelKeys
  51. return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
  52. }
  53. async function createLocalAccountWithoutKeys (parameters: {
  54. name: string,
  55. displayName?: string,
  56. userId: number | null,
  57. applicationId: number | null,
  58. t: Transaction | undefined,
  59. type?: ActivityPubActorType
  60. }) {
  61. const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters
  62. const url = getAccountActivityPubUrl(name)
  63. const actorInstance = buildActorInstance(type, url, name)
  64. const actorInstanceCreated = await actorInstance.save({ transaction: t })
  65. const accountInstance = new AccountModel({
  66. name: displayName || name,
  67. userId,
  68. applicationId,
  69. actorId: actorInstanceCreated.id
  70. })
  71. const accountInstanceCreated = await accountInstance.save({ transaction: t })
  72. accountInstanceCreated.Actor = actorInstanceCreated
  73. return accountInstanceCreated
  74. }
  75. async function createApplicationActor (applicationId: number) {
  76. const accountCreated = await createLocalAccountWithoutKeys({
  77. name: SERVER_ACTOR_NAME,
  78. userId: null,
  79. applicationId: applicationId,
  80. t: undefined,
  81. type: 'Application'
  82. })
  83. accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
  84. return accountCreated
  85. }
  86. async function sendVerifyUserEmail (user: UserModel, isPendingEmail = false) {
  87. const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id)
  88. let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString
  89. if (isPendingEmail) url += '&isPendingEmail=true'
  90. const email = isPendingEmail ? user.pendingEmail : user.email
  91. await Emailer.Instance.addVerifyEmailJob(email, url)
  92. }
  93. // ---------------------------------------------------------------------------
  94. export {
  95. createApplicationActor,
  96. createUserAccountAndChannelAndPlaylist,
  97. createLocalAccountWithoutKeys,
  98. sendVerifyUserEmail
  99. }
  100. // ---------------------------------------------------------------------------
  101. function createDefaultUserNotificationSettings (user: UserModel, t: Transaction | undefined) {
  102. const values: UserNotificationSetting & { userId: number } = {
  103. userId: user.id,
  104. newVideoFromSubscription: UserNotificationSettingValue.WEB,
  105. newCommentOnMyVideo: UserNotificationSettingValue.WEB,
  106. myVideoImportFinished: UserNotificationSettingValue.WEB,
  107. myVideoPublished: UserNotificationSettingValue.WEB,
  108. videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
  109. videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
  110. blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
  111. newUserRegistration: UserNotificationSettingValue.WEB,
  112. commentMention: UserNotificationSettingValue.WEB,
  113. newFollow: UserNotificationSettingValue.WEB,
  114. newInstanceFollower: UserNotificationSettingValue.WEB
  115. }
  116. return UserNotificationSettingModel.create(values, { transaction: t })
  117. }
  118. async function buildChannelAttributes (user: UserModel, channelNames?: ChannelNames) {
  119. if (channelNames) return channelNames
  120. let channelName = user.username + '_channel'
  121. // Conflict, generate uuid instead
  122. const actor = await ActorModel.loadLocalByName(channelName)
  123. if (actor) channelName = uuidv4()
  124. const videoChannelDisplayName = `Main ${user.username} channel`
  125. return {
  126. name: channelName,
  127. displayName: videoChannelDisplayName
  128. }
  129. }