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