]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Add playlist rest tests
[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'
df0b219d 14import { createWatchLaterPlaylist } from './video-playlist'
72c7248b 15
df0b219d 16async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, validateUser = true) {
f05a1c30 17 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
72c7248b
C
18 const userOptions = {
19 transaction: t,
20 validate: validateUser
21 }
22
d175a6f7 23 const userCreated = await userToCreate.save(userOptions)
cef534ed
C
24 userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t)
25
d175a6f7 26 const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
80e36cd9 27 userCreated.Account = accountCreated
f5028693 28
8a19bee1
C
29 let channelName = userCreated.username + '_channel'
30
31 // Conflict, generate uuid instead
32 const actor = await ActorModel.loadLocalByName(channelName)
33 if (actor) channelName = uuidv4()
34
35 const videoChannelDisplayName = `Main ${userCreated.username} channel`
f5028693 36 const videoChannelInfo = {
8a19bee1 37 name: channelName,
08c1efbe 38 displayName: videoChannelDisplayName
f5028693 39 }
e4f97bab 40 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 41
df0b219d
C
42 const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
43
44 return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
72c7248b 45 })
f5028693 46
0b2f03d3
C
47 const [ accountKeys, channelKeys ] = await Promise.all([
48 setAsyncActorKeys(account.Actor),
49 setAsyncActorKeys(videoChannel.Actor)
50 ])
51
52 account.Actor = accountKeys
53 videoChannel.Actor = channelKeys
47e0652b 54
6b738c7a 55 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
72c7248b
C
56}
57
50d6de9c
C
58async function createLocalAccountWithoutKeys (
59 name: string,
c1e791ba
RK
60 userId: number | null,
61 applicationId: number | null,
62 t: Sequelize.Transaction | undefined,
50d6de9c
C
63 type: ActivityPubActorType= 'Person'
64) {
54141398 65 const url = getAccountActivityPubUrl(name)
571389d4 66
50d6de9c 67 const actorInstance = buildActorInstance(type, url, name)
fadf619a
C
68 const actorInstanceCreated = await actorInstance.save({ transaction: t })
69
70 const accountInstance = new AccountModel({
71 name,
571389d4
C
72 userId,
73 applicationId,
c39ea24b 74 actorId: actorInstanceCreated.id
c1e791ba 75 } as FilteredModelAttributes<AccountModel>)
571389d4 76
fadf619a
C
77 const accountInstanceCreated = await accountInstance.save({ transaction: t })
78 accountInstanceCreated.Actor = actorInstanceCreated
79
80 return accountInstanceCreated
571389d4
C
81}
82
50d6de9c
C
83async function createApplicationActor (applicationId: number) {
84 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
85
86 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
87
88 return accountCreated
89}
90
72c7248b
C
91// ---------------------------------------------------------------------------
92
93export {
50d6de9c 94 createApplicationActor,
df0b219d 95 createUserAccountAndChannelAndPlaylist,
47e0652b 96 createLocalAccountWithoutKeys
72c7248b 97}
cef534ed
C
98
99// ---------------------------------------------------------------------------
100
101function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Transaction | undefined) {
f7cc67b4 102 const values: UserNotificationSetting & { userId: number } = {
cef534ed 103 userId: user.id,
2f1548fd
C
104 newVideoFromSubscription: UserNotificationSettingValue.WEB,
105 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
106 myVideoImportFinished: UserNotificationSettingValue.WEB,
107 myVideoPublished: UserNotificationSettingValue.WEB,
108 videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
109 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
110 newUserRegistration: UserNotificationSettingValue.WEB,
111 commentMention: UserNotificationSettingValue.WEB,
112 newFollow: UserNotificationSettingValue.WEB
f7cc67b4
C
113 }
114
115 return UserNotificationSettingModel.create(values, { transaction: t })
cef534ed 116}