From d3a31c89081f7d2d268c53fef64c502407ef42bb Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 4 Apr 2020 21:44:26 +0200 Subject: user list --- bucky/app/api.js | 7 ++++++- bucky/app/bucky.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ bucky/app/pages.js | 8 ++++++-- bucky/db/index.js | 11 ++++++++++- 4 files changed, 67 insertions(+), 4 deletions(-) (limited to 'bucky') diff --git a/bucky/app/api.js b/bucky/app/api.js index 45dde3f..11ced51 100644 --- a/bucky/app/api.js +++ b/bucky/app/api.js @@ -37,9 +37,14 @@ function route (app){ app.get("/api/users", middleware.ensureAuthenticated, bucky.ensureUserlist, + bucky.ensureUserThreadCounts, + bucky.ensureUserFileCounts, + bucky.ensureUserCommentCounts, + bucky.ensureUserStatistics, function(req, res) { res.json({ - users: res.users + users: res.users, + userStats: res.userStats, }) }) app.get("/api/profile/:username", diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 8ff0b15..74caa66 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -579,6 +579,51 @@ var bucky = module.exports = { next() }) }, + ensureUserThreadCounts: function (req, res, next) { + db.getUserThreadCounts().then(function(counts){ + if (!counts) { + return res.sendStatus(404) + } + res.threadCounts = counts + next() + }) + }, + ensureUserCommentCounts: function (req, res, next) { + db.getUserCommentCounts().then(function(counts){ + if (!counts) { + return res.sendStatus(404) + } + res.commentCounts = counts + next() + }) + }, + ensureUserFileCounts: function (req, res, next) { + db.getUserFileCounts().then(function(counts){ + if (!counts) { + return res.sendStatus(404) + } + res.fileCounts = counts + next() + }) + }, + ensureUserStatistics: function (req, res, next) { + var stats = {} + res.threadCounts.forEach(function(user){ + stats[user.username] = stats[user.username] || {} + stats[user.username].threads = user.count + }) + res.commentCounts.forEach(function(user){ + stats[user.username] = stats[user.username] || {} + stats[user.username].comments = user.count + }) + res.fileCounts.forEach(function(user){ + stats[user.username] = stats[user.username] || {} + stats[user.username].files = user.count + stats[user.username].fileSize = user.size + }) + res.userStats = stats + next() + }, sanitizeUser: function(req, res, next) { res.user = util.sanitizeUser(res.user) next() diff --git a/bucky/app/pages.js b/bucky/app/pages.js index b007742..5d8551e 100644 --- a/bucky/app/pages.js +++ b/bucky/app/pages.js @@ -76,8 +76,12 @@ function route (app){ app.get("/users", middleware.ensureAuthenticated, function(req, res){ - res.render("pages/users", { - }) + res.render("pages/users", {}) + }) + app.get("/users/all", + middleware.ensureAuthenticated, + function(req, res){ + res.render("pages/users", {}) }) app.get("/search/", diff --git a/bucky/db/index.js b/bucky/db/index.js index 53e36dd..312ca13 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -55,7 +55,7 @@ db.createUser = function(data){ } db.getUsers = function () { return User.query(function(qb){ - qb.orderBy("username", "desc") + qb.orderBy("username", "asc") }).fetchAll({ columns: [ "id", "username", "realname", "firstseen", "lastseen", @@ -148,6 +148,15 @@ db.getThreadUsers = function(thread_id){ db.getUserThreadIds = function(user_id){ return ThreadUser.query("where", "user_id", "=", user_id).fetch() } +db.getUserThreadCounts = function(ids){ + return knex.column('username').count('* as count').select().from('threads').groupBy('username') +} +db.getUserCommentCounts = function(ids){ + return knex.column('username').count('* as count').select().from('comments').groupBy('username') +} +db.getUserFileCounts = function(ids){ + return knex.column('username').sum('size as size').count('* as count').select().from('files').groupBy('username') +} /* FILES */ -- cgit v1.2.3-70-g09d2