]>
Commit | Line | Data |
---|---|---|
fb719404 | 1 | import { Transaction } from 'sequelize/types' |
7d9ba5c0 C |
2 | import { UserModel } from '@server/models/user/user' |
3 | import { MActorDefault } from '@server/types/models/actor' | |
0628157f | 4 | import { buildUUID } from '@shared/extra-utils' |
50d6de9c | 5 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
fb719404 | 6 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' |
d1ab89de | 7 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
fb719404 | 8 | import { sequelizeTypescript } from '../initializers/database' |
3fd3ab2d | 9 | import { AccountModel } from '../models/account/account' |
7d9ba5c0 C |
10 | import { ActorModel } from '../models/actor/actor' |
11 | import { UserNotificationSettingModel } from '../models/user/user-notification-setting' | |
12 | import { MAccountDefault, MChannelActor } from '../types/models' | |
26d6bf65 | 13 | import { MUser, MUserDefault, MUserId } from '../types/models/user' |
136d7efd | 14 | import { generateAndSaveActorKeys } from './activitypub/actors' |
de94ac86 | 15 | import { getLocalAccountActivityPubUrl } from './activitypub/url' |
fb719404 | 16 | import { Emailer } from './emailer' |
8ebf2a5d | 17 | import { LiveQuotaStore } from './live/live-quota-store' |
136d7efd | 18 | import { buildActorInstance } from './local-actor' |
fb719404 C |
19 | import { Redis } from './redis' |
20 | import { createLocalVideoChannel } from './video-channel' | |
21 | import { createWatchLaterPlaylist } from './video-playlist' | |
c729caf6 | 22 | import { logger } from '@server/helpers/logger' |
fb719404 | 23 | |
e590b4a5 | 24 | type ChannelNames = { name: string, displayName: string } |
453e83ea | 25 | |
1f20622f | 26 | async function createUserAccountAndChannelAndPlaylist (parameters: { |
a1587156 C |
27 | userToCreate: MUser |
28 | userDisplayName?: string | |
29 | channelNames?: ChannelNames | |
1f20622f | 30 | validateUser?: boolean |
1ca9f7c3 | 31 | }): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> { |
1f20622f C |
32 | const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters |
33 | ||
f05a1c30 | 34 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { |
72c7248b C |
35 | const userOptions = { |
36 | transaction: t, | |
37 | validate: validateUser | |
38 | } | |
39 | ||
1ca9f7c3 | 40 | const userCreated: MUserDefault = await userToCreate.save(userOptions) |
cef534ed C |
41 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) |
42 | ||
1f20622f C |
43 | const accountCreated = await createLocalAccountWithoutKeys({ |
44 | name: userCreated.username, | |
45 | displayName: userDisplayName, | |
46 | userId: userCreated.id, | |
47 | applicationId: null, | |
eae0365b | 48 | t |
1f20622f | 49 | }) |
80e36cd9 | 50 | userCreated.Account = accountCreated |
f5028693 | 51 | |
eae0365b | 52 | const channelAttributes = await buildChannelAttributes(userCreated, t, channelNames) |
1ca9f7c3 | 53 | const videoChannel = await createLocalVideoChannel(channelAttributes, accountCreated, t) |
f5028693 | 54 | |
df0b219d C |
55 | const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) |
56 | ||
57 | return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } | |
72c7248b | 58 | }) |
f5028693 | 59 | |
453e83ea | 60 | const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([ |
8795d6f2 C |
61 | generateAndSaveActorKeys(account.Actor), |
62 | generateAndSaveActorKeys(videoChannel.Actor) | |
0b2f03d3 C |
63 | ]) |
64 | ||
453e83ea C |
65 | account.Actor = accountActorWithKeys |
66 | videoChannel.Actor = channelActorWithKeys | |
47e0652b | 67 | |
453e83ea | 68 | return { user, account, videoChannel } |
72c7248b C |
69 | } |
70 | ||
1f20622f | 71 | async function createLocalAccountWithoutKeys (parameters: { |
a1587156 C |
72 | name: string |
73 | displayName?: string | |
74 | userId: number | null | |
75 | applicationId: number | null | |
76 | t: Transaction | undefined | |
1f20622f C |
77 | type?: ActivityPubActorType |
78 | }) { | |
79 | const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters | |
de94ac86 | 80 | const url = getLocalAccountActivityPubUrl(name) |
571389d4 | 81 | |
50d6de9c | 82 | const actorInstance = buildActorInstance(type, url, name) |
1ca9f7c3 | 83 | const actorInstanceCreated: MActorDefault = await actorInstance.save({ transaction: t }) |
fadf619a C |
84 | |
85 | const accountInstance = new AccountModel({ | |
1f20622f | 86 | name: displayName || name, |
571389d4 C |
87 | userId, |
88 | applicationId, | |
c39ea24b | 89 | actorId: actorInstanceCreated.id |
1735c825 | 90 | }) |
571389d4 | 91 | |
1ca9f7c3 | 92 | const accountInstanceCreated: MAccountDefault = await accountInstance.save({ transaction: t }) |
fadf619a C |
93 | accountInstanceCreated.Actor = actorInstanceCreated |
94 | ||
95 | return accountInstanceCreated | |
571389d4 C |
96 | } |
97 | ||
50d6de9c | 98 | async function createApplicationActor (applicationId: number) { |
1f20622f C |
99 | const accountCreated = await createLocalAccountWithoutKeys({ |
100 | name: SERVER_ACTOR_NAME, | |
101 | userId: null, | |
102 | applicationId: applicationId, | |
103 | t: undefined, | |
104 | type: 'Application' | |
105 | }) | |
50d6de9c | 106 | |
8795d6f2 | 107 | accountCreated.Actor = await generateAndSaveActorKeys(accountCreated.Actor) |
50d6de9c C |
108 | |
109 | return accountCreated | |
110 | } | |
111 | ||
453e83ea | 112 | async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { |
d1ab89de C |
113 | const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) |
114 | let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString | |
115 | ||
116 | if (isPendingEmail) url += '&isPendingEmail=true' | |
117 | ||
118 | const email = isPendingEmail ? user.pendingEmail : user.email | |
963023ab | 119 | const username = user.username |
d1ab89de | 120 | |
963023ab | 121 | await Emailer.Instance.addVerifyEmailJob(username, email, url) |
d1ab89de C |
122 | } |
123 | ||
fb719404 C |
124 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { |
125 | // Don't use sequelize because we need to use a sub query | |
126 | const query = UserModel.generateUserQuotaBaseSQL({ | |
127 | withSelect: true, | |
128 | whereUserId: '$userId' | |
129 | }) | |
130 | ||
131 | const base = await UserModel.getTotalRawQuery(query, user.id) | |
132 | ||
8ebf2a5d | 133 | return base + LiveQuotaStore.Instance.getLiveQuotaOf(user.id) |
fb719404 C |
134 | } |
135 | ||
136 | // Returns cumulative size of all video files uploaded in the last 24 hours. | |
137 | async function getOriginalVideoFileTotalDailyFromUser (user: MUserId) { | |
138 | // Don't use sequelize because we need to use a sub query | |
139 | const query = UserModel.generateUserQuotaBaseSQL({ | |
140 | withSelect: true, | |
141 | whereUserId: '$userId', | |
142 | where: '"video"."createdAt" > now() - interval \'24 hours\'' | |
143 | }) | |
144 | ||
145 | const base = await UserModel.getTotalRawQuery(query, user.id) | |
146 | ||
8ebf2a5d | 147 | return base + LiveQuotaStore.Instance.getLiveQuotaOf(user.id) |
fb719404 C |
148 | } |
149 | ||
8ebf2a5d | 150 | async function isAbleToUploadVideo (userId: number, newVideoSize: number) { |
fb719404 C |
151 | const user = await UserModel.loadById(userId) |
152 | ||
153 | if (user.videoQuota === -1 && user.videoQuotaDaily === -1) return Promise.resolve(true) | |
154 | ||
155 | const [ totalBytes, totalBytesDaily ] = await Promise.all([ | |
97969c4e C |
156 | getOriginalVideoFileTotalFromUser(user), |
157 | getOriginalVideoFileTotalDailyFromUser(user) | |
fb719404 C |
158 | ]) |
159 | ||
8ebf2a5d C |
160 | const uploadedTotal = newVideoSize + totalBytes |
161 | const uploadedDaily = newVideoSize + totalBytesDaily | |
fb719404 | 162 | |
c729caf6 C |
163 | logger.debug( |
164 | 'Check user %d quota to upload another video.', userId, | |
165 | { totalBytes, totalBytesDaily, videoQuota: user.videoQuota, videoQuotaDaily: user.videoQuotaDaily, newVideoSize } | |
166 | ) | |
167 | ||
fb719404 C |
168 | if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota |
169 | if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily | |
170 | ||
171 | return uploadedTotal < user.videoQuota && uploadedDaily < user.videoQuotaDaily | |
172 | } | |
173 | ||
72c7248b C |
174 | // --------------------------------------------------------------------------- |
175 | ||
176 | export { | |
fb719404 C |
177 | getOriginalVideoFileTotalFromUser, |
178 | getOriginalVideoFileTotalDailyFromUser, | |
50d6de9c | 179 | createApplicationActor, |
df0b219d | 180 | createUserAccountAndChannelAndPlaylist, |
d1ab89de | 181 | createLocalAccountWithoutKeys, |
fb719404 C |
182 | sendVerifyUserEmail, |
183 | isAbleToUploadVideo | |
72c7248b | 184 | } |
cef534ed C |
185 | |
186 | // --------------------------------------------------------------------------- | |
187 | ||
453e83ea | 188 | function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | undefined) { |
f7cc67b4 | 189 | const values: UserNotificationSetting & { userId: number } = { |
cef534ed | 190 | userId: user.id, |
2f1548fd C |
191 | newVideoFromSubscription: UserNotificationSettingValue.WEB, |
192 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
193 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
194 | myVideoPublished: UserNotificationSettingValue.WEB, | |
4f32032f | 195 | abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, |
7ccddd7b | 196 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, |
2f1548fd C |
197 | blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, |
198 | newUserRegistration: UserNotificationSettingValue.WEB, | |
199 | commentMention: UserNotificationSettingValue.WEB, | |
883993c8 | 200 | newFollow: UserNotificationSettingValue.WEB, |
8424c402 | 201 | newInstanceFollower: UserNotificationSettingValue.WEB, |
594d3e48 C |
202 | abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, |
203 | abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
32a18cbf C |
204 | autoInstanceFollowing: UserNotificationSettingValue.WEB, |
205 | newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
206 | newPluginVersion: UserNotificationSettingValue.WEB | |
f7cc67b4 C |
207 | } |
208 | ||
209 | return UserNotificationSettingModel.create(values, { transaction: t }) | |
cef534ed | 210 | } |
e590b4a5 | 211 | |
eae0365b | 212 | async function buildChannelAttributes (user: MUser, transaction?: Transaction, channelNames?: ChannelNames) { |
e590b4a5 C |
213 | if (channelNames) return channelNames |
214 | ||
215 | let channelName = user.username + '_channel' | |
216 | ||
217 | // Conflict, generate uuid instead | |
eae0365b | 218 | const actor = await ActorModel.loadLocalByName(channelName, transaction) |
d4a8e7a6 | 219 | if (actor) channelName = buildUUID() |
e590b4a5 C |
220 | |
221 | const videoChannelDisplayName = `Main ${user.username} channel` | |
222 | ||
223 | return { | |
224 | name: channelName, | |
225 | displayName: videoChannelDisplayName | |
226 | } | |
227 | } |