Don't expose constants directly in initializers/
[oweals/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 { 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 import { createWatchLaterPlaylist } from './video-playlist'
15 import { sequelizeTypescript } from '../initializers/database'
16
17 async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, 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     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`
37     const videoChannelInfo = {
38       name: channelName,
39       displayName: videoChannelDisplayName
40     }
41     const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
42
43     const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
44
45     return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
46   })
47
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
55
56   return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
57 }
58
59 async function createLocalAccountWithoutKeys (
60   name: string,
61   userId: number | null,
62   applicationId: number | null,
63   t: Sequelize.Transaction | undefined,
64   type: ActivityPubActorType= 'Person'
65 ) {
66   const url = getAccountActivityPubUrl(name)
67
68   const actorInstance = buildActorInstance(type, url, name)
69   const actorInstanceCreated = await actorInstance.save({ transaction: t })
70
71   const accountInstance = new AccountModel({
72     name,
73     userId,
74     applicationId,
75     actorId: actorInstanceCreated.id
76   } as FilteredModelAttributes<AccountModel>)
77
78   const accountInstanceCreated = await accountInstance.save({ transaction: t })
79   accountInstanceCreated.Actor = actorInstanceCreated
80
81   return accountInstanceCreated
82 }
83
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
92 // ---------------------------------------------------------------------------
93
94 export {
95   createApplicationActor,
96   createUserAccountAndChannelAndPlaylist,
97   createLocalAccountWithoutKeys
98 }
99
100 // ---------------------------------------------------------------------------
101
102 function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) {
103   const values: UserNotificationSetting & { userId: number } = {
104     userId: user.id,
105     newVideoFromSubscription: UserNotificationSettingValue.WEB,
106     newCommentOnMyVideo: UserNotificationSettingValue.WEB,
107     myVideoImportFinished: UserNotificationSettingValue.WEB,
108     myVideoPublished: UserNotificationSettingValue.WEB,
109     videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
110     videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
111     blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
112     newUserRegistration: UserNotificationSettingValue.WEB,
113     commentMention: UserNotificationSettingValue.WEB,
114     newFollow: UserNotificationSettingValue.WEB,
115     newInstanceFollower: UserNotificationSettingValue.WEB
116   }
117
118   return UserNotificationSettingModel.create(values, { transaction: t })
119 }