From ce5496d6a31b9617aba67970f5dc135e73335234 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Jan 2018 12:53:09 +0100 Subject: Support video quota on client --- server/controllers/api/users.ts | 17 ++++++++++++++++- server/models/account/user.ts | 2 +- server/tests/api/users/users.ts | 13 +++++++++++-- server/tests/utils/users/users.ts | 12 ++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) (limited to 'server') 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', asyncMiddleware(getUserInformation) ) +usersRouter.get('/me/video-quota-used', + authenticate, + asyncMiddleware(getUserVideoQuotaUsed) +) + usersRouter.get('/me/videos', authenticate, paginationValidator, @@ -183,8 +188,18 @@ async function getUserInformation (req: express.Request, res: express.Response, return res.json(user.toFormattedJSON()) } +async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) { + // We did not load channels in res.locals.user + const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) + const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) + + return res.json({ + videoQuotaUsed + }) +} + function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { - return res.json(res.locals.user.toFormattedJSON()) + return res.json((res.locals.user as UserModel).toFormattedJSON()) } async 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 { return UserModel.findOne(query) } - private static getOriginalVideoFileTotalFromUser (user: UserModel) { + static getOriginalVideoFileTotalFromUser (user: UserModel) { // Don't use sequelize because we need to use a sub query const query = 'SELECT SUM("size") AS "total" FROM ' + '(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' import 'mocha' import { UserRole } from '../../../../shared/index' import { - createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList, + createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating, getUserInformation, + getUsersList, getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo, runServer, ServerInfo, serverLogin, testVideoImage, updateMyAvatar, updateMyUser, updateUser, uploadVideo } from '../../utils/index' @@ -179,11 +180,19 @@ describe('Test users', function () { this.timeout(5000) const videoAttributes = { - name: 'super user video' + name: 'super user video', + fixture: 'video_short.webm' } await uploadVideo(server.url, accessTokenUser, videoAttributes) }) + it('Should have video quota updated', async function () { + const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser) + const data = res.body + + expect(data.videoQuotaUsed).to.equal(218910) + }) + it('Should be able to list my videos', async function () { const res = await getMyVideos(server.url, accessTokenUser, 0, 5) 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 = .expect('Content-Type', /json/) } +function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { + const path = '/api/v1/users/me/video-quota-used' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + function getUserInformation (url: string, accessToken: string, userId: number) { const path = '/api/v1/users/' + userId @@ -192,6 +203,7 @@ export { registerUser, getMyUserInformation, getMyUserVideoRating, + getMyUserVideoQuotaUsed, getUsersList, getUsersListPaginationAndSort, removeUser, -- cgit v1.2.3