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