From a76138ff56ad3ad2df926f59578a06b22fb1f93c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Aug 2018 17:56:51 +0200 Subject: [PATCH] Add quota used in users list --- .../app/+admin/users/shared/user.service.ts | 9 ++++-- .../users/user-list/user-list.component.html | 2 +- .../video-import/video-import.service.ts | 3 +- server/models/account/user.ts | 30 +++++++++++++++++-- server/tests/api/users/users.ts | 8 ++++- shared/models/users/user.model.ts | 2 ++ 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/client/src/app/+admin/users/shared/user.service.ts b/client/src/app/+admin/users/shared/user.service.ts index ad7fb1eee..a0e2f666d 100644 --- a/client/src/app/+admin/users/shared/user.service.ts +++ b/client/src/app/+admin/users/shared/user.service.ts @@ -4,9 +4,9 @@ import { Injectable } from '@angular/core' import { BytesPipe } from 'ngx-pipes' import { SortMeta } from 'primeng/components/common/sortmeta' import { Observable } from 'rxjs' -import { ResultList, UserCreate, UserUpdate } from '../../../../../../shared' +import { ResultList, UserCreate, UserUpdate, User } from '../../../../../../shared' import { environment } from '../../../../environments/environment' -import { RestExtractor, RestPagination, RestService, User } from '../../../shared' +import { RestExtractor, RestPagination, RestService } from '../../../shared' import { I18n } from '@ngx-translate/i18n-polyfill' @Injectable() @@ -79,8 +79,11 @@ export class UserService { videoQuota = this.bytesPipe.transform(user.videoQuota, 0) } + const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) + return Object.assign(user, { - videoQuota + videoQuota, + videoQuotaUsed }) } } diff --git a/client/src/app/+admin/users/user-list/user-list.component.html b/client/src/app/+admin/users/user-list/user-list.component.html index a92fe95ef..bb1b26442 100644 --- a/client/src/app/+admin/users/user-list/user-list.component.html +++ b/client/src/app/+admin/users/user-list/user-list.component.html @@ -36,7 +36,7 @@ (banned) {{ user.email }} - {{ user.videoQuota }} + {{ user.videoQuotaUsed }} / {{ user.videoQuota }} {{ user.roleLabel }} {{ user.createdAt }} diff --git a/client/src/app/shared/video-import/video-import.service.ts b/client/src/app/shared/video-import/video-import.service.ts index f63bfb2b1..fc34dbf2d 100644 --- a/client/src/app/shared/video-import/video-import.service.ts +++ b/client/src/app/shared/video-import/video-import.service.ts @@ -5,9 +5,8 @@ import { Observable } from 'rxjs' import { VideoImport } from '../../../../../shared' import { environment } from '../../../environments/environment' import { RestExtractor, RestService } from '../rest' -import { VideoImportCreate } from '../../../../../shared/models/videos' +import { VideoImportCreate, VideoUpdate } from '../../../../../shared/models/videos' import { objectToFormData } from '@app/shared/misc/utils' -import { VideoUpdate } from '../../../../../shared/models/videos' import { ResultList } from '../../../../../shared/models/result-list.model' import { UserService } from '@app/shared/users/user.service' import { SortMeta } from 'primeng/components/common/sortmeta' diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 81b0651fd..0150df4ce 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -161,6 +161,25 @@ export class UserModel extends Model { static listForApi (start: number, count: number, sort: string) { const query = { + attributes: { + include: [ + [ + Sequelize.literal( + '(' + + 'SELECT COALESCE(SUM("size"), 0) FROM ' + + '(' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + + ') t' + + ')' + ), + 'videoQuotaUsed' + ] as any // FIXME: typings + ] + }, offset: start, limit: count, order: getSort(sort) @@ -168,6 +187,9 @@ export class UserModel extends Model { return UserModel.findAndCountAll(query) .then(({ rows, count }) => { + console.log(rows[0]) + console.log(rows[0]['videoQuotaUsed']) + console.log(rows[0].get('videoQuotaUsed')) return { data: rows, total: count @@ -249,8 +271,7 @@ export class UserModel extends Model { 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' + - 'WHERE "user"."id" = $userId GROUP BY "video"."id") t' + 'WHERE "account"."userId" = $userId GROUP BY "video"."id") t' const options = { bind: { userId: user.id }, @@ -281,6 +302,8 @@ export class UserModel extends Model { } toFormattedJSON (): User { + const videoQuotaUsed = this.get('videoQuotaUsed') + const json = { id: this.id, username: this.username, @@ -294,7 +317,8 @@ export class UserModel extends Model { blocked: this.blocked, blockedReason: this.blockedReason, account: this.Account.toFormattedJSON(), - videoChannels: [] + videoChannels: [], + videoQuotaUsed: videoQuotaUsed !== undefined ? parseInt(videoQuotaUsed, 10) : undefined } if (Array.isArray(this.Account.VideoChannels) === true) { diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 77aa00f60..04dcc8fd1 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -2,7 +2,7 @@ import * as chai from 'chai' import 'mocha' -import { UserRole } from '../../../../shared/index' +import { User, UserRole } from '../../../../shared/index' import { createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating, getUserInformation, getUsersList, getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, @@ -192,6 +192,12 @@ describe('Test users', function () { const data = res.body expect(data.videoQuotaUsed).to.equal(218910) + + const resUsers = await getUsersList(server.url, server.accessToken) + + const users: User[] = resUsers.body.data + const tmpUser = users.find(u => u.username === user.username) + expect(tmpUser.videoQuotaUsed).to.equal(218910) }) it('Should be able to list my videos', async function () { diff --git a/shared/models/users/user.model.ts b/shared/models/users/user.model.ts index 8eddaa496..455211aa3 100644 --- a/shared/models/users/user.model.ts +++ b/shared/models/users/user.model.ts @@ -17,4 +17,6 @@ export interface User { blocked: boolean blockedReason?: string + + videoQuotaUsed?: number }