]>
Commit | Line | Data |
---|---|---|
571389d4 | 1 | import * as Sequelize from 'sequelize' |
8a19bee1 | 2 | import * as uuidv4 from 'uuid/v4' |
50d6de9c C |
3 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
4 | import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers' | |
3fd3ab2d C |
5 | import { AccountModel } from '../models/account/account' |
6 | import { UserModel } from '../models/account/user' | |
50d6de9c | 7 | import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' |
72c7248b | 8 | import { createVideoChannel } from './video-channel' |
6b738c7a | 9 | import { VideoChannelModel } from '../models/video/video-channel' |
c1e791ba | 10 | import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model' |
8a19bee1 | 11 | import { ActorModel } from '../models/activitypub/actor' |
cef534ed | 12 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' |
f7cc67b4 | 13 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' |
72c7248b | 14 | |
f05a1c30 C |
15 | async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { |
16 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
72c7248b C |
17 | const userOptions = { |
18 | transaction: t, | |
19 | validate: validateUser | |
20 | } | |
21 | ||
d175a6f7 | 22 | const userCreated = await userToCreate.save(userOptions) |
cef534ed C |
23 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) |
24 | ||
d175a6f7 | 25 | const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t) |
80e36cd9 | 26 | userCreated.Account = accountCreated |
f5028693 | 27 | |
8a19bee1 C |
28 | let channelName = userCreated.username + '_channel' |
29 | ||
30 | // Conflict, generate uuid instead | |
31 | const actor = await ActorModel.loadLocalByName(channelName) | |
32 | if (actor) channelName = uuidv4() | |
33 | ||
34 | const videoChannelDisplayName = `Main ${userCreated.username} channel` | |
f5028693 | 35 | const videoChannelInfo = { |
8a19bee1 | 36 | name: channelName, |
08c1efbe | 37 | displayName: videoChannelDisplayName |
f5028693 | 38 | } |
e4f97bab | 39 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
f5028693 | 40 | |
f05a1c30 | 41 | return { user: userCreated, account: accountCreated, videoChannel } |
72c7248b | 42 | }) |
f5028693 | 43 | |
0b2f03d3 C |
44 | const [ accountKeys, channelKeys ] = await Promise.all([ |
45 | setAsyncActorKeys(account.Actor), | |
46 | setAsyncActorKeys(videoChannel.Actor) | |
47 | ]) | |
48 | ||
49 | account.Actor = accountKeys | |
50 | videoChannel.Actor = channelKeys | |
47e0652b | 51 | |
6b738c7a | 52 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } |
72c7248b C |
53 | } |
54 | ||
50d6de9c C |
55 | async function createLocalAccountWithoutKeys ( |
56 | name: string, | |
c1e791ba RK |
57 | userId: number | null, |
58 | applicationId: number | null, | |
59 | t: Sequelize.Transaction | undefined, | |
50d6de9c C |
60 | type: ActivityPubActorType= 'Person' |
61 | ) { | |
54141398 | 62 | const url = getAccountActivityPubUrl(name) |
571389d4 | 63 | |
50d6de9c | 64 | const actorInstance = buildActorInstance(type, url, name) |
fadf619a C |
65 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) |
66 | ||
67 | const accountInstance = new AccountModel({ | |
68 | name, | |
571389d4 C |
69 | userId, |
70 | applicationId, | |
c39ea24b | 71 | actorId: actorInstanceCreated.id |
c1e791ba | 72 | } as FilteredModelAttributes<AccountModel>) |
571389d4 | 73 | |
fadf619a C |
74 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) |
75 | accountInstanceCreated.Actor = actorInstanceCreated | |
76 | ||
77 | return accountInstanceCreated | |
571389d4 C |
78 | } |
79 | ||
50d6de9c C |
80 | async function createApplicationActor (applicationId: number) { |
81 | const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application') | |
82 | ||
83 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) | |
84 | ||
85 | return accountCreated | |
86 | } | |
87 | ||
72c7248b C |
88 | // --------------------------------------------------------------------------- |
89 | ||
90 | export { | |
50d6de9c | 91 | createApplicationActor, |
571389d4 | 92 | createUserAccountAndChannel, |
47e0652b | 93 | createLocalAccountWithoutKeys |
72c7248b | 94 | } |
cef534ed C |
95 | |
96 | // --------------------------------------------------------------------------- | |
97 | ||
98 | function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) { | |
f7cc67b4 | 99 | const values: UserNotificationSetting & { userId: number } = { |
cef534ed | 100 | userId: user.id, |
2f1548fd C |
101 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
102 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
103 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
104 | myVideoPublished: UserNotificationSettingValue.WEB, | |
105 | videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
106 | blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
107 | newUserRegistration: UserNotificationSettingValue.WEB, | |
108 | commentMention: UserNotificationSettingValue.WEB, | |
109 | newFollow: UserNotificationSettingValue.WEB | |
f7cc67b4 C |
110 | } |
111 | ||
112 | return UserNotificationSettingModel.create(values, { transaction: t }) | |
cef534ed | 113 | } |