diff options
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r-- | server/lib/user.ts | 72 |
1 files changed, 62 insertions, 10 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts index aa14f0b54..d3338f329 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,20 +1,24 @@ | |||
1 | import { Transaction } from 'sequelize/types' | ||
1 | import { v4 as uuidv4 } from 'uuid' | 2 | import { v4 as uuidv4 } from 'uuid' |
3 | import { UserModel } from '@server/models/account/user' | ||
2 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 4 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
5 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | ||
3 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' | 6 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
7 | import { sequelizeTypescript } from '../initializers/database' | ||
4 | import { AccountModel } from '../models/account/account' | 8 | import { AccountModel } from '../models/account/account' |
5 | import { buildActorInstance, setAsyncActorKeys } from './activitypub/actor' | ||
6 | import { createLocalVideoChannel } from './video-channel' | ||
7 | import { ActorModel } from '../models/activitypub/actor' | ||
8 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' | 9 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' |
9 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | 10 | import { ActorModel } from '../models/activitypub/actor' |
10 | import { createWatchLaterPlaylist } from './video-playlist' | ||
11 | import { sequelizeTypescript } from '../initializers/database' | ||
12 | import { Transaction } from 'sequelize/types' | ||
13 | import { Redis } from './redis' | ||
14 | import { Emailer } from './emailer' | ||
15 | import { MAccountDefault, MActorDefault, MChannelActor } from '../types/models' | 11 | import { MAccountDefault, MActorDefault, MChannelActor } from '../types/models' |
16 | import { MUser, MUserDefault, MUserId } from '../types/models/user' | 12 | import { MUser, MUserDefault, MUserId } from '../types/models/user' |
13 | import { buildActorInstance, setAsyncActorKeys } from './activitypub/actor' | ||
17 | import { getAccountActivityPubUrl } from './activitypub/url' | 14 | import { getAccountActivityPubUrl } from './activitypub/url' |
15 | import { Emailer } from './emailer' | ||
16 | import { LiveManager } from './live-manager' | ||
17 | import { Redis } from './redis' | ||
18 | import { createLocalVideoChannel } from './video-channel' | ||
19 | import { createWatchLaterPlaylist } from './video-playlist' | ||
20 | |||
21 | import memoizee = require('memoizee') | ||
18 | 22 | ||
19 | type ChannelNames = { name: string, displayName: string } | 23 | type ChannelNames = { name: string, displayName: string } |
20 | 24 | ||
@@ -116,13 +120,61 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { | |||
116 | await Emailer.Instance.addVerifyEmailJob(username, email, url) | 120 | await Emailer.Instance.addVerifyEmailJob(username, email, url) |
117 | } | 121 | } |
118 | 122 | ||
123 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { | ||
124 | // Don't use sequelize because we need to use a sub query | ||
125 | const query = UserModel.generateUserQuotaBaseSQL({ | ||
126 | withSelect: true, | ||
127 | whereUserId: '$userId' | ||
128 | }) | ||
129 | |||
130 | const base = await UserModel.getTotalRawQuery(query, user.id) | ||
131 | |||
132 | return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) | ||
133 | } | ||
134 | |||
135 | // Returns cumulative size of all video files uploaded in the last 24 hours. | ||
136 | async function getOriginalVideoFileTotalDailyFromUser (user: MUserId) { | ||
137 | // Don't use sequelize because we need to use a sub query | ||
138 | const query = UserModel.generateUserQuotaBaseSQL({ | ||
139 | withSelect: true, | ||
140 | whereUserId: '$userId', | ||
141 | where: '"video"."createdAt" > now() - interval \'24 hours\'' | ||
142 | }) | ||
143 | |||
144 | const base = await UserModel.getTotalRawQuery(query, user.id) | ||
145 | |||
146 | return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) | ||
147 | } | ||
148 | |||
149 | async function isAbleToUploadVideo (userId: number, size: number) { | ||
150 | const user = await UserModel.loadById(userId) | ||
151 | |||
152 | if (user.videoQuota === -1 && user.videoQuotaDaily === -1) return Promise.resolve(true) | ||
153 | |||
154 | const [ totalBytes, totalBytesDaily ] = await Promise.all([ | ||
155 | getOriginalVideoFileTotalFromUser(user.id), | ||
156 | getOriginalVideoFileTotalDailyFromUser(user.id) | ||
157 | ]) | ||
158 | |||
159 | const uploadedTotal = size + totalBytes | ||
160 | const uploadedDaily = size + totalBytesDaily | ||
161 | |||
162 | if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota | ||
163 | if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily | ||
164 | |||
165 | return uploadedTotal < user.videoQuota && uploadedDaily < user.videoQuotaDaily | ||
166 | } | ||
167 | |||
119 | // --------------------------------------------------------------------------- | 168 | // --------------------------------------------------------------------------- |
120 | 169 | ||
121 | export { | 170 | export { |
171 | getOriginalVideoFileTotalFromUser, | ||
172 | getOriginalVideoFileTotalDailyFromUser, | ||
122 | createApplicationActor, | 173 | createApplicationActor, |
123 | createUserAccountAndChannelAndPlaylist, | 174 | createUserAccountAndChannelAndPlaylist, |
124 | createLocalAccountWithoutKeys, | 175 | createLocalAccountWithoutKeys, |
125 | sendVerifyUserEmail | 176 | sendVerifyUserEmail, |
177 | isAbleToUploadVideo | ||
126 | } | 178 | } |
127 | 179 | ||
128 | // --------------------------------------------------------------------------- | 180 | // --------------------------------------------------------------------------- |