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