summaryrefslogtreecommitdiff
path: root/bucky
diff options
context:
space:
mode:
Diffstat (limited to 'bucky')
-rw-r--r--bucky/app/api.js17
-rw-r--r--bucky/app/bucky.js40
-rw-r--r--bucky/db/index.js25
3 files changed, 82 insertions, 0 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')
}