summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-09-07 14:51:20 -0400
committerJules Laplace <jules@okfoc.us>2015-09-07 14:51:20 -0400
commit9957952d2761b1d2b216a9310339e28f25df9f6c (patch)
tree5195075b01434f327b73162eee0fa22825e506fd
parent43265c66f757222bbe8fefb670300c0a3d3b51bf (diff)
mailbox api
-rw-r--r--lib/bucky.js33
-rw-r--r--lib/db/index.js28
-rw-r--r--lib/router.js2
3 files changed, 52 insertions, 11 deletions
diff --git a/lib/bucky.js b/lib/bucky.js
index 742f74d..db1672c 100644
--- a/lib/bucky.js
+++ b/lib/bucky.js
@@ -16,8 +16,11 @@ var bucky = module.exports = {
ensureCommentCountsForThreads: function (req, res, next){
db.getCommentCounts(res.threads_ids).then(function(counts){
var lookup = {}
- counts.forEach(function(c,i){
- res.threads.at(i).set("comment_count", c.count)
+ counts.forEach(function(c){
+ lookup[c.thread] = c
+ })
+ res.threads.forEach(function(thread){
+ thread.set("comment_count", lookup[thread.id].count)
})
next()
})
@@ -134,27 +137,39 @@ var bucky = module.exports = {
/* MAIL */
- ensureMailbox: function (req, res, next){
+ ensureMailboxes: function (req, res, next){
+ var username = req.user.get('username')
var box = req.params.box
+ var mbox = username + "." + box
if (! box) {
res.sendStatus(404)
}
- db.getMailbox(req.user.username, box).then(function(box){
- if (! box) {
+ db.getMailboxes(username).then(function(boxes){
+ if (! boxes) {
return res.sendStatus(404)
}
+ if (! boxes.models.some(function(box){ return box.get('mbox') == mbox })) {
+ return res.sendStatus(404)
+ }
+ res.boxes = boxes
next()
})
},
ensureMailboxCounts: function (req, res, next){
- db.getMailboxes(req.user.username).then(function(boxes){
- res.boxes = boxes
+ db.getMailboxCounts(res.boxes.pluck("mbox")).then(function(counts){
+ var lookup = {}
+ counts.forEach(function(c){
+ lookup[c.mbox] = c
+ })
+ res.boxes.forEach(function(box){
+ var count = lookup[box.get('mbox')] ? lookup[box.get('mbox')].count : 0
+ box.set("count", count)
+ })
next()
})
},
ensureMessages: function (req, res, next){
- // todo: define offset
- db.getMessages(req.user.username, req.params.box, 50, 0).then(function(messages){
+ db.getMessages(req.user.get('username'), req.params.box, 50, 0).then(function(messages){
res.messages = messages
next()
})
diff --git a/lib/db/index.js b/lib/db/index.js
index 1ebae57..ab4cdcf 100644
--- a/lib/db/index.js
+++ b/lib/db/index.js
@@ -4,6 +4,7 @@ var connection = require("./bookshelf")
var bookshelf = connection.bookshelf
var knex = connection.knex
+
/* MODELS */
var User = db.User = bookshelf.Model.extend({
@@ -74,6 +75,7 @@ db.getThread = function (id) {
return Thread.query("where", "id", "=", id).fetch()
}
+
/* FILES */
db.getFilesForThread = function (id){
@@ -86,6 +88,7 @@ db.getFileSizes = function(ids){
return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
+
/* COMMENTS */
db.getCommentsForThread = function (id, limit, offset, order){
@@ -109,6 +112,7 @@ db.getCommentCounts = function(ids){
return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread')
}
+
/* KEYWORDS */
db.getKeywords = function (keywords){
@@ -119,8 +123,30 @@ db.getKeyword = function (keyword) {
}
-/* PRIVATE MESSAGES */
+/* MAILBOXES */
+
+db.getMailboxes = function(username){
+ return Mailbox.query("where", "owner", "=", username).fetchAll()
+}
+db.getMailboxCounts = function(boxes){
+ return knex.column('mbox').count('* as count').select().from('messages').where('mbox', 'in', boxes).groupBy('mbox')
+}
+
+
+/* MESSAGES */
+db.getMessages = function(username, box, limit, offset){
+ var mbox = username + "." + box
+ return Message.query(function(qb){
+ qb.column('id', 'mbox', 'unread', 'sender', 'recipient', 'date', 'subject').where("mbox", "=", mbox).orderBy("id", "desc")
+ if (limit) {
+ qb.limit(limit)
+ }
+ if (offset) {
+ qb.offset(offset)
+ }
+ }).fetchAll()
+}
db.getMessage = function (id){
var model = new Message({'id': id})
return model.fetch().then(function(message){
diff --git a/lib/router.js b/lib/router.js
index e2ba442..5d3dcb5 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -116,7 +116,7 @@ module.exports = function(app){
app.get("/api/mailbox/:box",
middleware.ensureAuthenticated,
- bucky.ensureMailbox,
+ bucky.ensureMailboxes,
bucky.ensureMailboxCounts,
bucky.ensureMessages,
function(req, res){