From f8055775f3ac74e9e6054aef102673cefcf27986 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 25 Jan 2018 02:15:48 +0100 Subject: more on profile --- bucky/app/api.js | 17 ++++++++++++ bucky/app/bucky.js | 40 +++++++++++++++++++++++++++ bucky/db/index.js | 25 +++++++++++++++++ public/assets/css/bucky.css | 12 ++++++++ public/assets/js/lib/views/profile/profile.js | 20 ++++++++++++-- views/pages/profile.ejs | 39 +++++++++++++++++++------- 6 files changed, 140 insertions(+), 13 deletions(-) diff --git a/bucky/app/api.js b/bucky/app/api.js index 7a0e068..80cba16 100644 --- a/bucky/app/api.js +++ b/bucky/app/api.js @@ -32,6 +32,23 @@ function route (app){ function(req, res){ res.json(util.sanitizeUser(res.user)) }) + app.get("/api/profile/:username", + middleware.ensureAuthenticated, + bucky.ensureUser, + bucky.sanitizeUser, + bucky.ensureThreadsForUser, + // bucky.ensureTopThreadsForUser, + // bucky.ensureCommentsForUser, + bucky.ensureFilesForUser, + function(req, res) { + res.json({ + user: res.user, + threads: res.threads, + // topThreads: res.topThreads, + files: res.files, + // comments: res.comments, + }) + }) app.put("/api/checkUsernames", middleware.ensureAuthenticated, bucky.checkUsernames, diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 76c543a..2a362b5 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -312,6 +312,46 @@ var bucky = module.exports = { next() }) }, + ensureThreadsForUser: function (req, res, next){ + var username = res.user.username + var limit = parseInt(req.params.limit) || 10 + var offset = parseInt(req.params.offset) || 0 + if (! username) { + res.sendStatus(404) + } + db.getThreadsForUser(username, limit, offset).then(function(threads){ + res.threads = threads + res.threads_ids = res.threads.pluck("id").sort() + res.keywords = _.uniq(res.threads.pluck("keyword")) + next() + }) + }, + ensureTopThreadsForUser: function (req, res, next){ + var username = res.user.username + var limit = parseInt(req.params.limit) || 10 + var offset = parseInt(req.params.offset) || 0 + if (! username) { + res.sendStatus(404) + } + db.getTopThreadsForUser(username, limit, offset).then(function(top_threads){ + res.topThreads = top_threads + res.topThreads_ids = res.topThreads.pluck("id").sort() + res.topKeywords = _.uniq(res.topThreads.pluck("keyword")) + next() + }) + }, + ensureCommentsForUser: function (req, res, next){ + db.getCommentsForUser(res.user.username).then(function(comments){ + res.comments = comments || [] + next() + }) + }, + ensureFilesForUser: function (req, res, next){ + db.getFilesForUser(res.user.username).then(function(files){ + res.files = files || [] + next() + }) + }, createKeyword: function (req, res, next){ if (! req.body.keyword || ! req.body.keyword.length) { res.json({ error: "no keyword" }) diff --git a/bucky/db/index.js b/bucky/db/index.js index 935219f..a147cd8 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -93,6 +93,16 @@ db.getThreadsForKeyword = function (keyword) { qb.where("keyword", "=", keyword).orderBy("id", "desc") }).fetchAll() } +db.getThreadsForUser = function (username, limit, offset) { + return Thread.query(function(qb){ + qb.where("username", "=", username).orderBy("id", "desc").limit(limit || 40).offset(offset || 0) + }).fetchAll() +} +db.getTopThreadsForUser = function (username, limit, offset) { + return Thread.query(function(qb){ + qb.where("username", "=", username).orderBy("viewed", "desc").limit(limit || 40).offset(offset || 0) + }).fetchAll() +} db.getThread = function (id) { return Thread.query("where", "id", "=", id).fetch() } @@ -138,6 +148,14 @@ db.getFileById = function(id){ db.getFilesForThread = function (id){ return File.query("where", "thread", "=", id).fetchAll() } +db.getFilesForUser = function (username, limit, offset){ + return File.query(function(qb){ + qb.where("username", "=", username) + .orderBy('id', 'desc') + .limit(parseInt(limit) || 20) + .offset(parseInt(offset) || 0) + }).fetchAll() +} db.getFileCounts = function(ids){ return knex.column('thread').count('* as count').select().from('files').where('thread', 'in', ids).groupBy('thread') } @@ -225,6 +243,13 @@ db.getCommentById = function(id){ db.getCommentsById = function(ids){ return Comment.where("id", "in", ids).fetchAll() } +db.getCommentsForUser = function (username, limit){ + return Comment.query(function(qb){ + qb.where("username", "=", username) + .orderBy('date', 'desc') + .limit(parseInt(limit) || 20) + }).fetchAll() +} db.getCommentCounts = function(ids){ return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread') } diff --git a/public/assets/css/bucky.css b/public/assets/css/bucky.css index f699c01..60d4390 100644 --- a/public/assets/css/bucky.css +++ b/public/assets/css/bucky.css @@ -353,10 +353,12 @@ tr:nth-child(even) td.black { background-color: #eee; border-bottom-color: .older { color: #5D6464; } .quiet { color: #787878; } +#profile_rapper, #details_rapper { width: 100%; text-align: center; } +#profile .r, #details { display: flex; flex-direction: row; @@ -370,6 +372,13 @@ tr:nth-child(even) td.black { background-color: #eee; border-bottom-color: vertical-align: top; width: 50%; } +.r .left { + padding-right: 20px; + padding-bottom: 20px; +} +.r .right { + padding-bottom: 20px; +} #comments { width: 100%; @@ -656,6 +665,9 @@ pre br { max-width: 100%; max-height: 450px; } +#profile #gallery div a:first-child .thumb { + max-width: 200px; max-height: 200px; +} #gallery .thumb { max-width: 150px; } diff --git a/public/assets/js/lib/views/profile/profile.js b/public/assets/js/lib/views/profile/profile.js index 1064adb..4396414 100644 --- a/public/assets/js/lib/views/profile/profile.js +++ b/public/assets/js/lib/views/profile/profile.js @@ -5,19 +5,33 @@ var ProfileView = View.extend({ events: { }, - action: "/api/user/", + action: "/api/profile/", initialize: function(opt){ this.template = this.$(".template").html() + this.comments = new CommentsView ({ parent: this }) + this.files = new FilesView ({ parent: this }) + this.gallery = new GalleryView ({ parent: this }) + this.threadbox = new ThreadBox ({ parent: this }) }, load: function(username){ - $.get(this.action + username, this.populate.bind(this)) + $.get(this.action + username, this.loadProfile.bind(this)) + }, + + loadProfile: function(data){ + const { user, threads, topThreads, files, comments } = data + this.populate(user) + // this.populateComments(topComments) + // this.comments.load(data.comments, data.thread) + this.gallery.load(data.files) + this.files.load(data.files) + this.threadbox.load({ threads: data.threads }) }, populate: function(user){ $("body").removeClass('loading') - var $table = this.$("table") + var $table = this.$("table.profile_meta") var username = sanitizeHTML(user.username) var is_own_profile = (username === auth.user.username) if (is_own_profile) { diff --git a/views/pages/profile.ejs b/views/pages/profile.ejs index 7cb7d87..a9fde25 100644 --- a/views/pages/profile.ejs +++ b/views/pages/profile.ejs @@ -4,16 +4,35 @@ < Home · Edit Profile -
- - - -
+
+ +
+
+
+ + + +
+
+ +
+
+ <% include ../partials/gallery %> +
+
+ +
+
+ <% include ../partials/files %> +
+ <% include ../partials/threads %> +
+
<% include ../partials/footer %> -- cgit v1.2.3-70-g09d2