]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/user.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
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 type ChannelNames = { name: string, displayName: string }
17 async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, channelNames?: ChannelNames, validateUser = true) {
18 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
19 const userOptions = {
20 transaction: t,
21 validate: validateUser
22 }
23
24 const userCreated = await userToCreate.save(userOptions)
25 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
26
27 const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
28 userCreated.Account = accountCreated
29
30 const channelAttributes = await buildChannelAttributes(userCreated, channelNames)
31 const videoChannel = await createVideoChannel(channelAttributes, accountCreated, t)
32
33 const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
34
35 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
36 })
37
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
45
46 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
47 }
48
49 async function createLocalAccountWithoutKeys (
50 name: string,
51 userId: number | null,
52 applicationId: number | null,
53 t: Sequelize.Transaction | undefined,
54 type: ActivityPubActorType= 'Person'
55 ) {
56 const url = getAccountActivityPubUrl(name)
57
58 const actorInstance = buildActorInstance(type, url, name)
59 const actorInstanceCreated = await actorInstance.save({ transaction: t })
60
61 const accountInstance = new AccountModel({
62 name,
63 userId,
64 applicationId,
65 actorId: actorInstanceCreated.id
66 })
67
68 const accountInstanceCreated = await accountInstance.save({ transaction: t })
69 accountInstanceCreated.Actor = actorInstanceCreated
70
71 return accountInstanceCreated
72 }
73
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
82 // ---------------------------------------------------------------------------
83
84 export {
85 createApplicationActor,
86 createUserAccountAndChannelAndPlaylist,
87 createLocalAccountWithoutKeys
88 }
89
90 // ---------------------------------------------------------------------------
91
92 function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) {
93 const values: UserNotificationSetting & { userId: number } = {
94 userId: user.id,
95 newVideoFromSubscription: UserNotificationSettingValue.WEB,
96 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
97 myVideoImportFinished: UserNotificationSettingValue.WEB,
98 myVideoPublished: UserNotificationSettingValue.WEB,
99 videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
100 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
101 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
102 newUserRegistration: UserNotificationSettingValue.WEB,
103 commentMention: UserNotificationSettingValue.WEB,
104 newFollow: UserNotificationSettingValue.WEB,
105 newInstanceFollower: UserNotificationSettingValue.WEB
106 }
107
108 return UserNotificationSettingModel.create(values, { transaction: t })
109 }
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 }