summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/bucky.js26
-rw-r--r--lib/db/index.js16
-rw-r--r--lib/index.js71
-rw-r--r--lib/router.js83
4 files changed, 119 insertions, 77 deletions
diff --git a/lib/bucky.js b/lib/bucky.js
index 9a43c0d..ad2018f 100644
--- a/lib/bucky.js
+++ b/lib/bucky.js
@@ -41,7 +41,6 @@ var bucky = module.exports = {
keywords.forEach(function(k){
lookup[k.get('keyword')] = k
})
- console.log(keywords)
res.threads.forEach(function(t){
var kw = t.get('keyword')
if (! kw) return
@@ -49,7 +48,6 @@ var bucky = module.exports = {
if (! k) return
if (! t.get("color")) {
t.set("color", k.get("color"))
- console.log(k.get("color"))
}
})
next()
@@ -71,13 +69,14 @@ var bucky = module.exports = {
/* DETAILS */
ensureThread: function (req, res, next){
- db.getThread(req.param.id).then(function(thread){
+ var id = req.params.id.replace(/\D/g, "")
+ db.getThread(id).then(function(thread){
if (thread) {
res.thread = thread
next()
}
else {
- res.sendCode(404)
+ res.sendStatus(404)
}
})
},
@@ -90,16 +89,31 @@ var bucky = module.exports = {
})
},
ensureCommentsForThread: function (req, res, next){
- db.getCommentsForThread(id).then(function(comments){
+ db.getCommentsForThread(res.thread.get('id')).then(function(comments){
res.comments = comments
next()
})
},
ensureFilesForThread: function (req, res, next){
- db.getCommentsForThread(id).then(function(files){
+ db.getFilesForThread(res.thread.get('id')).then(function(files){
res.files = files
next()
})
},
+
+ /* KEYWORDS */
+
+ ensureThreadsForKeyword: function (req, res, next){
+ var keyword = req.params.keyword
+ if (! keyword) {
+ res.sendStatus(404)
+ }
+ db.getThreadsForKeyword(keyword).then(function(threads){
+ res.threads = threads
+ res.threads_ids = res.threads.pluck("id").sort()
+ res.keywords = _.uniq(res.threads.pluck("keyword"))
+ next()
+ })
+ },
} \ No newline at end of file
diff --git a/lib/db/index.js b/lib/db/index.js
index 899abcc..7452f3d 100644
--- a/lib/db/index.js
+++ b/lib/db/index.js
@@ -65,6 +65,14 @@ db.getLatestThreads = function () {
qb.orderBy("id", "desc").limit(50)
}).fetchAll()
}
+db.getThreadsForKeyword = function (keyword) {
+ return Thread.query(function(qb){
+ qb.where("keyword", "=", keyword).orderBy("id", "desc")
+ }).fetchAll()
+}
+db.getThread = function (id) {
+ return Thread.query("where", "id", "=", id).fetch()
+}
/* FILES */
@@ -82,7 +90,7 @@ db.getFileSizes = function(ids){
db.getCommentsForThread = function (id, limit, offset){
return Comment.query(function(qb){
- qb.where("thread", "=", id).orderBy("id", "desc")
+ qb.where("thread", "=", id).orderBy("id", "asc")
if (limit) {
qb = qb.limit(limit)
}
@@ -101,9 +109,13 @@ db.getCommentCounts = function(ids){
}
/* KEYWORDS */
+
db.getKeywords = function (keywords){
return Keyword.query("where", "keyword", "in", keywords).fetchAll()
}
+db.getKeyword = function (keyword) {
+ return Keyword.query("where", "keyword", "=", keyword).fetch()
+}
/* PRIVATE MESSAGES */
@@ -114,4 +126,4 @@ db.getMessage = function (id){
message.set("body", message.get("body").toString() )
return message
})
-} \ No newline at end of file
+}
diff --git a/lib/index.js b/lib/index.js
index c1ded7e..ad97526 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -14,12 +14,6 @@ var sessionstore = require('sessionstore')
var session = require('express-session')
var multer = require('multer')
-var db = require('./db')
-var auth = require('./auth')
-var middleware = require('./middleware')
-var fortune = require('./fortune')
-var bucky = require('./bucky')
-
var app, server
var mongodb = require('mongodb')
@@ -56,72 +50,11 @@ site.init = function(){
app.use(passport.initialize())
app.use(passport.session())
- app.all('*', middleware.ensureLocals)
-
server = http.createServer(app).listen(5000, function () {
console.log('Bucky listening at http://5.k:%s', server.address().port)
})
- site.route()
+ site.route(app)
}
-site.route = function(){
- auth.init()
-
- app.get("/", middleware.ensureAuthenticated, function(req, res){
- res.redirect('/index')
- })
- app.get("/login", function(req, res){
- res.render("pages/login", {
- title: "login"
- })
- })
- app.get("/index", middleware.ensureAuthenticated, function(req, res){
- res.render("pages/index", {
- title: fortune("titles"),
- hoot_text: fortune("hoots"),
- })
- })
- app.post("/api/login", auth.loggedInLocal)
- app.get("/api/index",
- bucky.ensureLatestThreads,
- bucky.ensureCommentCountsForThreads,
- bucky.ensureFileCountsForThreads,
- bucky.ensureKeywordsForThreads,
- bucky.ensureHootbox,
- bucky.ensureLastlog,
- function(req, res){
- res.json({
- threads: res.threads,
- hootbox: res.hootbox,
- lastlog: res.lastlog,
- })
- }
- )
- app.get("/api/thread/:id", function(req, res){
- bucky.ensureThread,
- bucky.ensureKeywordForThread,
- bucky.ensureCommentsForThread,
- bucky.ensureFilesForThread,
- function(req, res){
- res.json({
- thread: res.thread,
- comments: res.comments,
- files: res.files,
- keyword: res.keyword,
- })
- }
- })
- app.post("/api/thread/:id", function(req, res){
- })
- app.post("/api/thread/:id/comment", function(req, res){
- })
- app.delete("/api/thread/:id", function(req, res){
- })
-
- app.put("/api/comment/:id", function(req, res){
- })
- app.delete("/api/thread/:id", function(req, res){
- })
-
-}
+site.route = require('./router')
diff --git a/lib/router.js b/lib/router.js
new file mode 100644
index 0000000..d8e75c0
--- /dev/null
+++ b/lib/router.js
@@ -0,0 +1,83 @@
+var auth = require('./auth')
+var middleware = require('./middleware')
+var fortune = require('./fortune')
+var bucky = require('./bucky')
+
+module.exports = function(app){
+ app.all('*', middleware.ensureLocals)
+
+ auth.init()
+
+ app.get("/", middleware.ensureAuthenticated, function(req, res){
+ res.redirect('/index')
+ })
+ app.get("/login", function(req, res){
+ res.render("pages/login", {
+ title: "login"
+ })
+ })
+ app.get("/index", middleware.ensureAuthenticated, function(req, res){
+ res.render("pages/index", {
+ title: fortune("titles"),
+ hoot_text: fortune("hoots"),
+ })
+ })
+ app.get("/details/:id", middleware.ensureAuthenticated, function(req, res){
+ res.render("pages/details", {})
+ })
+
+ app.post("/api/login", auth.loggedInLocal)
+ app.get("/api/index",
+ bucky.ensureLatestThreads,
+ bucky.ensureCommentCountsForThreads,
+ bucky.ensureFileCountsForThreads,
+ bucky.ensureKeywordsForThreads,
+ bucky.ensureHootbox,
+ bucky.ensureLastlog,
+ function(req, res){
+ res.json({
+ threads: res.threads,
+ hootbox: res.hootbox,
+ lastlog: res.lastlog,
+ })
+ }
+ )
+ app.get("/api/thread/:id",
+ bucky.ensureThread,
+ bucky.ensureKeywordForThread,
+ bucky.ensureCommentsForThread,
+ bucky.ensureFilesForThread,
+ function(req, res){
+ res.json({
+ thread: res.thread,
+ comments: res.comments,
+ files: res.files,
+ keyword: res.keyword,
+ })
+ }
+ )
+ app.post("/api/thread/:id", function(req, res){
+ })
+ app.post("/api/thread/:id/comment", function(req, res){
+ })
+ app.delete("/api/thread/:id", function(req, res){
+ })
+
+ app.get("/api/keyword/:keyword",
+ bucky.ensureThreadsForKeyword,
+ bucky.ensureCommentCountsForThreads,
+ bucky.ensureFileCountsForThreads,
+ bucky.ensureKeywordsForThreads,
+ function(req, res){
+ res.json({
+ threads: res.threads,
+ })
+ }
+ )
+
+ app.put("/api/comment/:id", function(req, res){
+ })
+ app.delete("/api/comment/:id", function(req, res){
+ })
+
+}