]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Add new follow, mention and user registered notifs
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4 1import * as Sequelize from 'sequelize'
8a19bee1 2import * as uuidv4 from 'uuid/v4'
50d6de9c
C
3import { ActivityPubActorType } from '../../shared/models/activitypub'
4import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
3fd3ab2d
C
5import { AccountModel } from '../models/account/account'
6import { UserModel } from '../models/account/user'
50d6de9c 7import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
72c7248b 8import { createVideoChannel } from './video-channel'
6b738c7a 9import { VideoChannelModel } from '../models/video/video-channel'
c1e791ba 10import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
8a19bee1 11import { ActorModel } from '../models/activitypub/actor'
cef534ed 12import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
f7cc67b4 13import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
72c7248b 14
f05a1c30
C
15async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
16 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
72c7248b
C
17 const userOptions = {
18 transaction: t,
19 validate: validateUser
20 }
21
d175a6f7 22 const userCreated = await userToCreate.save(userOptions)
cef534ed
C
23 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
24
d175a6f7 25 const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
80e36cd9 26 userCreated.Account = accountCreated
f5028693 27
8a19bee1
C
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`
f5028693 35 const videoChannelInfo = {
8a19bee1 36 name: channelName,
08c1efbe 37 displayName: videoChannelDisplayName
f5028693 38 }
e4f97bab 39 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 40
f05a1c30 41 return { user: userCreated, account: accountCreated, videoChannel }
72c7248b 42 })
f5028693 43
0b2f03d3
C
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
47e0652b 51
6b738c7a 52 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
72c7248b
C
53}
54
50d6de9c
C
55async function createLocalAccountWithoutKeys (
56 name: string,
c1e791ba
RK
57 userId: number | null,
58 applicationId: number | null,
59 t: Sequelize.Transaction | undefined,
50d6de9c
C
60 type: ActivityPubActorType= 'Person'
61) {
54141398 62 const url = getAccountActivityPubUrl(name)
571389d4 63
50d6de9c 64 const actorInstance = buildActorInstance(type, url, name)
fadf619a
C
65 const actorInstanceCreated = await actorInstance.save({ transaction: t })
66
67 const accountInstance = new AccountModel({
68 name,
571389d4
C
69 userId,
70 applicationId,
c39ea24b 71 actorId: actorInstanceCreated.id
c1e791ba 72 } as FilteredModelAttributes<AccountModel>)
571389d4 73
fadf619a
C
74 const accountInstanceCreated = await accountInstance.save({ transaction: t })
75 accountInstanceCreated.Actor = actorInstanceCreated
76
77 return accountInstanceCreated
571389d4
C
78}
79
50d6de9c
C
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
72c7248b
C
88// ---------------------------------------------------------------------------
89
90export {
50d6de9c 91 createApplicationActor,
571389d4 92 createUserAccountAndChannel,
47e0652b 93 createLocalAccountWithoutKeys
72c7248b 94}
cef534ed
C
95
96// ---------------------------------------------------------------------------
97
98function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) {
f7cc67b4 99 const values: UserNotificationSetting & { userId: number } = {
cef534ed
C
100 userId: user.id,
101 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
102 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
dc133480
C
103 myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION,
104 myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION,
cef534ed 105 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
f7cc67b4
C
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 })
cef534ed 113}