]>
Commit | Line | Data |
---|---|---|
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 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' | |
13 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | |
14 | ||
15 | async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { | |
16 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
17 | const userOptions = { | |
18 | transaction: t, | |
19 | validate: validateUser | |
20 | } | |
21 | ||
22 | const userCreated = await userToCreate.save(userOptions) | |
23 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) | |
24 | ||
25 | const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t) | |
26 | userCreated.Account = accountCreated | |
27 | ||
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` | |
35 | const videoChannelInfo = { | |
36 | name: channelName, | |
37 | displayName: videoChannelDisplayName | |
38 | } | |
39 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) | |
40 | ||
41 | return { user: userCreated, account: accountCreated, videoChannel } | |
42 | }) | |
43 | ||
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 | |
51 | ||
52 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } | |
53 | } | |
54 | ||
55 | async function createLocalAccountWithoutKeys ( | |
56 | name: string, | |
57 | userId: number | null, | |
58 | applicationId: number | null, | |
59 | t: Sequelize.Transaction | undefined, | |
60 | type: ActivityPubActorType= 'Person' | |
61 | ) { | |
62 | const url = getAccountActivityPubUrl(name) | |
63 | ||
64 | const actorInstance = buildActorInstance(type, url, name) | |
65 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) | |
66 | ||
67 | const accountInstance = new AccountModel({ | |
68 | name, | |
69 | userId, | |
70 | applicationId, | |
71 | actorId: actorInstanceCreated.id | |
72 | } as FilteredModelAttributes<AccountModel>) | |
73 | ||
74 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) | |
75 | accountInstanceCreated.Actor = actorInstanceCreated | |
76 | ||
77 | return accountInstanceCreated | |
78 | } | |
79 | ||
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 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | export { | |
91 | createApplicationActor, | |
92 | createUserAccountAndChannel, | |
93 | createLocalAccountWithoutKeys | |
94 | } | |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | ||
98 | function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) { | |
99 | const values: UserNotificationSetting & { userId: number } = { | |
100 | userId: user.id, | |
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 | |
110 | } | |
111 | ||
112 | return UserNotificationSettingModel.create(values, { transaction: t }) | |
113 | } |