From 76314386aeafdd6849b7b70c517779d6b2013473 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Fri, 27 Mar 2020 15:19:03 +0100 Subject: Add overview of a user's actions in user-edit (#2558) --- server/middlewares/validators/users.ts | 9 +- server/models/account/user.ts | 149 ++++++++++++++++++++++++++++----- server/tests/api/users/users.ts | 122 +++++++++++++++++++++++---- 3 files changed, 241 insertions(+), 39 deletions(-) (limited to 'server') diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index adc67a046..840b9fc74 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,6 +1,6 @@ import * as Bluebird from 'bluebird' import * as express from 'express' -import { body, param } from 'express-validator' +import { body, param, query } from 'express-validator' import { omit } from 'lodash' import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' import { @@ -256,12 +256,13 @@ const usersUpdateMeValidator = [ const usersGetValidator = [ param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), + query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersGet parameters', { parameters: req.params }) if (areValidationErrors(req, res)) return - if (!await checkUserIdExist(req.params.id, res)) return + if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return return next() } @@ -460,9 +461,9 @@ export { // --------------------------------------------------------------------------- -function checkUserIdExist (idArg: number | string, res: express.Response) { +function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) { const id = parseInt(idArg + '', 10) - return checkUserExist(() => UserModel.loadById(id), res) + return checkUserExist(() => UserModel.loadById(id, withStats), res) } function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 777f09666..026bf1318 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -19,7 +19,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared' +import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy, VideoAbuseState } from '../../../shared' import { User, UserRole } from '../../../shared/models/users' import { isNoInstanceConfigWarningModal, @@ -70,8 +70,26 @@ import { MVideoFullLight } from '@server/typings/models' +const literalVideoQuotaUsed: any = [ + 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' +] + enum ScopeNames { - FOR_ME_API = 'FOR_ME_API' + FOR_ME_API = 'FOR_ME_API', + WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', + WITH_STATS = 'WITH_STATS' } @DefaultScope(() => ({ @@ -112,6 +130,86 @@ enum ScopeNames { required: true } ] + }, + [ScopeNames.WITH_VIDEOCHANNELS]: { + include: [ + { + model: AccountModel, + include: [ + { + model: VideoChannelModel + }, + { + attributes: [ 'id', 'name', 'type' ], + model: VideoPlaylistModel.unscoped(), + required: true, + where: { + type: { + [Op.ne]: VideoPlaylistType.REGULAR + } + } + } + ] + } + ] + }, + [ScopeNames.WITH_STATS]: { + attributes: { + include: [ + literalVideoQuotaUsed, + [ + literal( + '(' + + 'SELECT COUNT("video"."id") ' + + 'FROM "video" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + + 'WHERE "account"."userId" = "UserModel"."id"' + + ')' + ), + 'videosCount' + ], + [ + literal( + '(' + + `SELECT concat_ws(':', "abuses", "acceptedAbuses") ` + + 'FROM (' + + 'SELECT COUNT("videoAbuse"."id") AS "abuses", ' + + `COUNT("videoAbuse"."id") FILTER (WHERE "videoAbuse"."state" = ${VideoAbuseState.ACCEPTED}) AS "acceptedAbuses" ` + + 'FROM "videoAbuse" ' + + 'INNER JOIN "video" ON "videoAbuse"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + + 'WHERE "account"."userId" = "UserModel"."id"' + + ') t' + + ')' + ), + 'videoAbusesCount' + ], + [ + literal( + '(' + + 'SELECT COUNT("videoAbuse"."id") ' + + 'FROM "videoAbuse" ' + + 'INNER JOIN "account" ON "account"."id" = "videoAbuse"."reporterAccountId" ' + + 'WHERE "account"."userId" = "UserModel"."id"' + + ')' + ), + 'videoAbusesCreatedCount' + ], + [ + literal( + '(' + + 'SELECT COUNT("videoComment"."id") ' + + 'FROM "videoComment" ' + + 'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' + + 'WHERE "account"."userId" = "UserModel"."id"' + + ')' + ), + 'videoCommentsCount' + ] + ] + } } })) @Table({ @@ -332,23 +430,7 @@ export class UserModel extends Model { const query: FindOptions = { attributes: { - include: [ - [ - 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' - ] - ] + include: [ literalVideoQuotaUsed ] }, offset: start, limit: count, @@ -430,8 +512,14 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static loadById (id: number): Bluebird { - return UserModel.findByPk(id) + static loadById (id: number, withStats = false): Bluebird { + const scopes = [ + ScopeNames.WITH_VIDEOCHANNELS + ] + + if (withStats) scopes.push(ScopeNames.WITH_STATS) + + return UserModel.scope(scopes).findByPk(id) } static loadByUsername (username: string): Bluebird { @@ -637,6 +725,10 @@ export class UserModel extends Model { toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { const videoQuotaUsed = this.get('videoQuotaUsed') const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') + const videosCount = this.get('videosCount') + const [ videoAbusesCount, videoAbusesAcceptedCount ] = (this.get('videoAbusesCount') as string || ':').split(':') + const videoAbusesCreatedCount = this.get('videoAbusesCreatedCount') + const videoCommentsCount = this.get('videoCommentsCount') const json: User = { id: this.id, @@ -666,6 +758,21 @@ export class UserModel extends Model { videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined ? parseInt(videoQuotaUsedDaily + '', 10) : undefined, + videosCount: videosCount !== undefined + ? parseInt(videosCount + '', 10) + : undefined, + videoAbusesCount: videoAbusesCount + ? parseInt(videoAbusesCount, 10) + : undefined, + videoAbusesAcceptedCount: videoAbusesAcceptedCount + ? parseInt(videoAbusesAcceptedCount, 10) + : undefined, + videoAbusesCreatedCount: videoAbusesCreatedCount !== undefined + ? parseInt(videoAbusesCreatedCount + '', 10) + : undefined, + videoCommentsCount: videoCommentsCount !== undefined + ? parseInt(videoCommentsCount + '', 10) + : undefined, noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, noWelcomeModal: this.noWelcomeModal, diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 502eac0bb..3e1a0c19b 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 { MyUser, User, UserRole, Video, VideoPlaylistType } from '../../../../shared/index' +import { MyUser, User, UserRole, Video, VideoPlaylistType, VideoAbuseState, VideoAbuseUpdate } from '../../../../shared/index' import { blockUser, cleanupTests, @@ -33,7 +33,11 @@ import { updateMyUser, updateUser, uploadVideo, - userLogin + userLogin, + reportVideoAbuse, + addVideoCommentThread, + updateVideoAbuse, + getVideoAbusesList } from '../../../../shared/extra-utils' import { follow } from '../../../../shared/extra-utils/server/follows' import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login' @@ -254,7 +258,7 @@ describe('Test users', function () { const res1 = await getMyUserInformation(server.url, accessTokenUser) const userMe: MyUser = res1.body - const res2 = await getUserInformation(server.url, server.accessToken, userMe.id) + const res2 = await getUserInformation(server.url, server.accessToken, userMe.id, true) const userGet: User = res2.body for (const user of [ userMe, userGet ]) { @@ -273,6 +277,16 @@ describe('Test users', function () { expect(userMe.specialPlaylists).to.have.lengthOf(1) expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER) + + // Check stats are included with withStats + expect(userGet.videosCount).to.be.a('number') + expect(userGet.videosCount).to.equal(0) + expect(userGet.videoCommentsCount).to.be.a('number') + expect(userGet.videoCommentsCount).to.equal(0) + expect(userGet.videoAbusesCount).to.be.a('number') + expect(userGet.videoAbusesCount).to.equal(0) + expect(userGet.videoAbusesAcceptedCount).to.be.a('number') + expect(userGet.videoAbusesAcceptedCount).to.equal(0) }) }) @@ -623,7 +637,6 @@ describe('Test users', function () { }) describe('Updating another user', function () { - it('Should be able to update another user', async function () { await updateUser({ url: server.url, @@ -698,6 +711,8 @@ describe('Test users', function () { }) describe('Registering a new user', function () { + let user15AccessToken + it('Should register a new user', async function () { const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' } const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' } @@ -711,18 +726,18 @@ describe('Test users', function () { password: 'my super password' } - accessToken = await userLogin(server, user15) + user15AccessToken = await userLogin(server, user15) }) it('Should have the correct display name', async function () { - const res = await getMyUserInformation(server.url, accessToken) + const res = await getMyUserInformation(server.url, user15AccessToken) const user: User = res.body expect(user.account.displayName).to.equal('super user 15') }) it('Should have the correct video quota', async function () { - const res = await getMyUserInformation(server.url, accessToken) + const res = await getMyUserInformation(server.url, user15AccessToken) const user = res.body expect(user.videoQuota).to.equal(5 * 1024 * 1024) @@ -740,7 +755,7 @@ describe('Test users', function () { expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined } - await deleteMe(server.url, accessToken) + await deleteMe(server.url, user15AccessToken) { const res = await getUsersList(server.url, server.accessToken) @@ -750,6 +765,9 @@ describe('Test users', function () { }) describe('User blocking', function () { + let user16Id + let user16AccessToken + it('Should block and unblock a user', async function () { const user16 = { username: 'user_16', @@ -761,19 +779,95 @@ describe('Test users', function () { username: user16.username, password: user16.password }) - const user16Id = resUser.body.user.id + user16Id = resUser.body.user.id - accessToken = await userLogin(server, user16) + user16AccessToken = await userLogin(server, user16) - await getMyUserInformation(server.url, accessToken, 200) + await getMyUserInformation(server.url, user16AccessToken, 200) await blockUser(server.url, user16Id, server.accessToken) - await getMyUserInformation(server.url, accessToken, 401) + await getMyUserInformation(server.url, user16AccessToken, 401) await userLogin(server, user16, 400) await unblockUser(server.url, user16Id, server.accessToken) - accessToken = await userLogin(server, user16) - await getMyUserInformation(server.url, accessToken, 200) + user16AccessToken = await userLogin(server, user16) + await getMyUserInformation(server.url, user16AccessToken, 200) + }) + }) + + describe('User stats', function () { + let user17Id + let user17AccessToken + + it('Should report correct initial statistics about a user', async function () { + const user17 = { + username: 'user_17', + password: 'my super password' + } + const resUser = await createUser({ + url: server.url, + accessToken: server.accessToken, + username: user17.username, + password: user17.password + }) + + user17Id = resUser.body.user.id + user17AccessToken = await userLogin(server, user17) + + const res = await getUserInformation(server.url, server.accessToken, user17Id, true) + const user: User = res.body + + expect(user.videosCount).to.equal(0) + expect(user.videoCommentsCount).to.equal(0) + expect(user.videoAbusesCount).to.equal(0) + expect(user.videoAbusesCreatedCount).to.equal(0) + expect(user.videoAbusesAcceptedCount).to.equal(0) + }) + + it('Should report correct videos count', async function () { + const videoAttributes = { + name: 'video to test user stats' + } + await uploadVideo(server.url, user17AccessToken, videoAttributes) + const res1 = await getVideosList(server.url) + videoId = res1.body.data.find(video => video.name === videoAttributes.name).id + + const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true) + const user: User = res2.body + + expect(user.videosCount).to.equal(1) + }) + + it('Should report correct video comments for user', async function () { + const text = 'super comment' + await addVideoCommentThread(server.url, user17AccessToken, videoId, text) + + const res = await getUserInformation(server.url, server.accessToken, user17Id, true) + const user: User = res.body + + expect(user.videoCommentsCount).to.equal(1) + }) + + it('Should report correct video abuses counts', async function () { + const reason = 'my super bad reason' + await reportVideoAbuse(server.url, user17AccessToken, videoId, reason) + + const res1 = await getVideoAbusesList(server.url, server.accessToken) + const abuseId = res1.body.data[0].id + + const res2 = await getUserInformation(server.url, server.accessToken, user17Id, true) + const user2: User = res2.body + + expect(user2.videoAbusesCount).to.equal(1) // number of incriminations + expect(user2.videoAbusesCreatedCount).to.equal(1) // number of reports created + + const body: VideoAbuseUpdate = { state: VideoAbuseState.ACCEPTED } + await updateVideoAbuse(server.url, server.accessToken, videoId, abuseId, body) + + const res3 = await getUserInformation(server.url, server.accessToken, user17Id, true) + const user3: User = res3.body + + expect(user3.videoAbusesAcceptedCount).to.equal(1) // number of reports created accepted }) }) -- cgit v1.2.3