]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/user.ts
Add new follow, mention and user registered notifs
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
... / ...
CommitLineData
1import * as Sequelize from 'sequelize'
2import * as uuidv4 from 'uuid/v4'
3import { ActivityPubActorType } from '../../shared/models/activitypub'
4import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
5import { AccountModel } from '../models/account/account'
6import { UserModel } from '../models/account/user'
7import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
8import { createVideoChannel } from './video-channel'
9import { VideoChannelModel } from '../models/video/video-channel'
10import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
11import { ActorModel } from '../models/activitypub/actor'
12import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
13import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
14
15async 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
55async 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
80async 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
90export {
91 createApplicationActor,
92 createUserAccountAndChannel,
93 createLocalAccountWithoutKeys
94}
95
96// ---------------------------------------------------------------------------
97
98function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) {
99 const values: UserNotificationSetting & { userId: number } = {
100 userId: user.id,
101 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
102 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
103 myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION,
104 myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION,
105 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
106 blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
107 newUserRegistration: UserNotificationSettingValue.WEB_NOTIFICATION,
108 commentMention: UserNotificationSettingValue.WEB_NOTIFICATION,
109 newFollow: UserNotificationSettingValue.WEB_NOTIFICATION
110 }
111
112 return UserNotificationSettingModel.create(values, { transaction: t })
113}