aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-08 12:53:09 +0100
committerChocobozzz <me@florianbigard.com>2018-01-08 12:53:09 +0100
commitce5496d6a31b9617aba67970f5dc135e73335234 (patch)
treeb5d2578f4618b71632e92838c3ceae36aa102798 /server
parent108af66140713c4beec681a71d360ab788226528 (diff)
downloadPeerTube-ce5496d6a31b9617aba67970f5dc135e73335234.tar.gz
PeerTube-ce5496d6a31b9617aba67970f5dc135e73335234.tar.zst
PeerTube-ce5496d6a31b9617aba67970f5dc135e73335234.zip
Support video quota on client
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/users.ts17
-rw-r--r--server/models/account/user.ts2
-rw-r--r--server/tests/api/users/users.ts13
-rw-r--r--server/tests/utils/users/users.ts12
4 files changed, 40 insertions, 4 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 2d77a5249..5374c4b6a 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -30,6 +30,11 @@ usersRouter.get('/me',
30 asyncMiddleware(getUserInformation) 30 asyncMiddleware(getUserInformation)
31) 31)
32 32
33usersRouter.get('/me/video-quota-used',
34 authenticate,
35 asyncMiddleware(getUserVideoQuotaUsed)
36)
37
33usersRouter.get('/me/videos', 38usersRouter.get('/me/videos',
34 authenticate, 39 authenticate,
35 paginationValidator, 40 paginationValidator,
@@ -183,8 +188,18 @@ async function getUserInformation (req: express.Request, res: express.Response,
183 return res.json(user.toFormattedJSON()) 188 return res.json(user.toFormattedJSON())
184} 189}
185 190
191async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) {
192 // We did not load channels in res.locals.user
193 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
194 const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
195
196 return res.json({
197 videoQuotaUsed
198 })
199}
200
186function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { 201function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
187 return res.json(res.locals.user.toFormattedJSON()) 202 return res.json((res.locals.user as UserModel).toFormattedJSON())
188} 203}
189 204
190async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { 205async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index 4226bcb35..e37fd4d3b 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -181,7 +181,7 @@ export class UserModel extends Model<UserModel> {
181 return UserModel.findOne(query) 181 return UserModel.findOne(query)
182 } 182 }
183 183
184 private static getOriginalVideoFileTotalFromUser (user: UserModel) { 184 static getOriginalVideoFileTotalFromUser (user: UserModel) {
185 // Don't use sequelize because we need to use a sub query 185 // Don't use sequelize because we need to use a sub query
186 const query = 'SELECT SUM("size") AS "total" FROM ' + 186 const query = 'SELECT SUM("size") AS "total" FROM ' +
187 '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + 187 '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index f7e5972d3..b788637e7 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -4,7 +4,8 @@ import * as chai from 'chai'
4import 'mocha' 4import 'mocha'
5import { UserRole } from '../../../../shared/index' 5import { UserRole } from '../../../../shared/index'
6import { 6import {
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList, 7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating, getUserInformation,
8 getUsersList,
8 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo, 9 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
9 runServer, ServerInfo, serverLogin, testVideoImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo 10 runServer, ServerInfo, serverLogin, testVideoImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo
10} from '../../utils/index' 11} from '../../utils/index'
@@ -179,11 +180,19 @@ describe('Test users', function () {
179 this.timeout(5000) 180 this.timeout(5000)
180 181
181 const videoAttributes = { 182 const videoAttributes = {
182 name: 'super user video' 183 name: 'super user video',
184 fixture: 'video_short.webm'
183 } 185 }
184 await uploadVideo(server.url, accessTokenUser, videoAttributes) 186 await uploadVideo(server.url, accessTokenUser, videoAttributes)
185 }) 187 })
186 188
189 it('Should have video quota updated', async function () {
190 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
191 const data = res.body
192
193 expect(data.videoQuotaUsed).to.equal(218910)
194 })
195
187 it('Should be able to list my videos', async function () { 196 it('Should be able to list my videos', async function () {
188 const res = await getMyVideos(server.url, accessTokenUser, 0, 5) 197 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
189 expect(res.body.total).to.equal(1) 198 expect(res.body.total).to.equal(1)
diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts
index 90b1ca0a6..12945a805 100644
--- a/server/tests/utils/users/users.ts
+++ b/server/tests/utils/users/users.ts
@@ -56,6 +56,17 @@ function getMyUserInformation (url: string, accessToken: string, specialStatus =
56 .expect('Content-Type', /json/) 56 .expect('Content-Type', /json/)
57} 57}
58 58
59function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
60 const path = '/api/v1/users/me/video-quota-used'
61
62 return request(url)
63 .get(path)
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
66 .expect(specialStatus)
67 .expect('Content-Type', /json/)
68}
69
59function getUserInformation (url: string, accessToken: string, userId: number) { 70function getUserInformation (url: string, accessToken: string, userId: number) {
60 const path = '/api/v1/users/' + userId 71 const path = '/api/v1/users/' + userId
61 72
@@ -192,6 +203,7 @@ export {
192 registerUser, 203 registerUser,
193 getMyUserInformation, 204 getMyUserInformation,
194 getMyUserVideoRating, 205 getMyUserVideoRating,
206 getMyUserVideoQuotaUsed,
195 getUsersList, 207 getUsersList,
196 getUsersListPaginationAndSort, 208 getUsersListPaginationAndSort,
197 removeUser, 209 removeUser,