aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-09-25 16:19:35 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commitfb7194043d0486ce0a6a40b2ffbdf32878c33a6f (patch)
tree6ed304a5d730a75da0a4460b3009df88684fa598 /server/lib/user.ts
parenta5cf76afa378aae81af2a9b0ce548e5d2582f832 (diff)
downloadPeerTube-fb7194043d0486ce0a6a40b2ffbdf32878c33a6f.tar.gz
PeerTube-fb7194043d0486ce0a6a40b2ffbdf32878c33a6f.tar.zst
PeerTube-fb7194043d0486ce0a6a40b2ffbdf32878c33a6f.zip
Check live duration and size
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r--server/lib/user.ts72
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 @@
1import { Transaction } from 'sequelize/types'
1import { v4 as uuidv4 } from 'uuid' 2import { v4 as uuidv4 } from 'uuid'
3import { UserModel } from '@server/models/account/user'
2import { ActivityPubActorType } from '../../shared/models/activitypub' 4import { ActivityPubActorType } from '../../shared/models/activitypub'
5import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
3import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' 6import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
7import { sequelizeTypescript } from '../initializers/database'
4import { AccountModel } from '../models/account/account' 8import { AccountModel } from '../models/account/account'
5import { buildActorInstance, setAsyncActorKeys } from './activitypub/actor'
6import { createLocalVideoChannel } from './video-channel'
7import { ActorModel } from '../models/activitypub/actor'
8import { UserNotificationSettingModel } from '../models/account/user-notification-setting' 9import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
9import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' 10import { ActorModel } from '../models/activitypub/actor'
10import { createWatchLaterPlaylist } from './video-playlist'
11import { sequelizeTypescript } from '../initializers/database'
12import { Transaction } from 'sequelize/types'
13import { Redis } from './redis'
14import { Emailer } from './emailer'
15import { MAccountDefault, MActorDefault, MChannelActor } from '../types/models' 11import { MAccountDefault, MActorDefault, MChannelActor } from '../types/models'
16import { MUser, MUserDefault, MUserId } from '../types/models/user' 12import { MUser, MUserDefault, MUserId } from '../types/models/user'
13import { buildActorInstance, setAsyncActorKeys } from './activitypub/actor'
17import { getAccountActivityPubUrl } from './activitypub/url' 14import { getAccountActivityPubUrl } from './activitypub/url'
15import { Emailer } from './emailer'
16import { LiveManager } from './live-manager'
17import { Redis } from './redis'
18import { createLocalVideoChannel } from './video-channel'
19import { createWatchLaterPlaylist } from './video-playlist'
20
21import memoizee = require('memoizee')
18 22
19type ChannelNames = { name: string, displayName: string } 23type 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
123async 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.
136async 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
149async 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
121export { 170export {
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// ---------------------------------------------------------------------------