]>
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 { SERVER_ACTOR_NAME } from '../initializers/constants' | |
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 { ActorModel } from '../models/activitypub/actor' | |
11 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' | |
12 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | |
13 | import { createWatchLaterPlaylist } from './video-playlist' | |
14 | import { sequelizeTypescript } from '../initializers/database' | |
15 | ||
16 | async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, validateUser = true) { | |
17 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
18 | const userOptions = { | |
19 | transaction: t, | |
20 | validate: validateUser | |
21 | } | |
22 | ||
23 | const userCreated = await userToCreate.save(userOptions) | |
24 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) | |
25 | ||
26 | const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t) | |
27 | userCreated.Account = accountCreated | |
28 | ||
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` | |
36 | const videoChannelInfo = { | |
37 | name: channelName, | |
38 | displayName: videoChannelDisplayName | |
39 | } | |
40 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) | |
41 | ||
42 | const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) | |
43 | ||
44 | return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } | |
45 | }) | |
46 | ||
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 | |
54 | ||
55 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } | |
56 | } | |
57 | ||
58 | async function createLocalAccountWithoutKeys ( | |
59 | name: string, | |
60 | userId: number | null, | |
61 | applicationId: number | null, | |
62 | t: Sequelize.Transaction | undefined, | |
63 | type: ActivityPubActorType= 'Person' | |
64 | ) { | |
65 | const url = getAccountActivityPubUrl(name) | |
66 | ||
67 | const actorInstance = buildActorInstance(type, url, name) | |
68 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) | |
69 | ||
70 | const accountInstance = new AccountModel({ | |
71 | name, | |
72 | userId, | |
73 | applicationId, | |
74 | actorId: actorInstanceCreated.id | |
75 | }) | |
76 | ||
77 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) | |
78 | accountInstanceCreated.Actor = actorInstanceCreated | |
79 | ||
80 | return accountInstanceCreated | |
81 | } | |
82 | ||
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 | ||
91 | // --------------------------------------------------------------------------- | |
92 | ||
93 | export { | |
94 | createApplicationActor, | |
95 | createUserAccountAndChannelAndPlaylist, | |
96 | createLocalAccountWithoutKeys | |
97 | } | |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
101 | function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.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 | ||
117 | return UserNotificationSettingModel.create(values, { transaction: t }) | |
118 | } |