summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/bucky.js46
-rw-r--r--lib/db/index.js29
-rw-r--r--lib/fortune.js28
-rw-r--r--lib/index.js47
-rw-r--r--lib/middleware.js1
5 files changed, 131 insertions, 20 deletions
diff --git a/lib/bucky.js b/lib/bucky.js
new file mode 100644
index 0000000..6022bb2
--- /dev/null
+++ b/lib/bucky.js
@@ -0,0 +1,46 @@
+var db = require('./db')
+
+var bucky = module.exports = {
+
+ /* INDEX */
+
+ ensureLatestThreads: function (req, res, next){
+ db.getLatestThreads().then(function(threads){
+ res.threads = threads
+ next()
+ })
+ },
+ ensureCommentCountsForThreads: function (req, res, next){
+ var ids = res.threads.pluck("id")
+ db.getCommentCounts(ids).then(function(counts){
+ console.log(counts)
+ next()
+ })
+ },
+ ensureFileCountsForThreads: function (req, res, next){
+ db.getFileCounts(ids).then(function(counts){
+ next()
+ })
+ },
+
+
+ /* DETAILS */
+
+ ensureThread: function (req, res, next){
+ db.getThread(req.param.id).then(function(thread){
+ if (thread) {
+ res.thread = thread
+ }
+ else {
+ res.sendCode(404)
+ }
+ })
+ },
+ ensureCommentsForThread: function (req, res, next){
+ return db.getCommentsForThread(id).fetch()
+ },
+ ensureFilesForThread: function (req, res, next){
+ return db.getFilesForThread(id).fetch()
+ },
+
+} \ No newline at end of file
diff --git a/lib/db/index.js b/lib/db/index.js
index f539b90..3761417 100644
--- a/lib/db/index.js
+++ b/lib/db/index.js
@@ -35,7 +35,7 @@ var Message = db.Message = bookshelf.Model.extend({
hasTimestamps: false,
})
-/* PICTURES */
+/* USERS */
db.createUser = function(data){
return new db.User(data).save()
@@ -53,8 +53,27 @@ db.getUserByUsername = function(username) {
var model = new User({'username': username})
return model.fetch()
}
-db.getThreads = function () {
- return User.query(function(qb){
- qb.orderBy("id", "desc")
+
+/* THREADS */
+
+db.getLatestThreads = function () {
+ return Thread.query(function(qb){
+ qb.orderBy("id", "desc").limit(50)
}).fetchAll()
-} \ No newline at end of file
+}
+
+/* FILES */
+db.getFilesForThread = function (id){
+ return File.query("where", "thread", "=", id).fetchAll()
+}
+db.getFileCounts = function(ids){
+ return knex.column('thread', 'count(*)').select().from('comments').where('thread', 'in', ids).groupBy('thread')
+}
+
+/* COMMENTS */
+db.getCommentsForThread = function (id){
+ return Comment.query("where", "thread", "=", id).fetchAll()
+}
+db.getCommentCounts = function(){
+ return knex.column('thread', 'count(*)').select().from('files').where('thread', 'in', ids).groupBy('thread')
+}
diff --git a/lib/fortune.js b/lib/fortune.js
new file mode 100644
index 0000000..7adba5a
--- /dev/null
+++ b/lib/fortune.js
@@ -0,0 +1,28 @@
+function choice (a){ return a[ Math.floor(Math.random()*a.length) ] }
+
+var fs = require("fs"), path = require("path")
+var fortunes = {}
+var dir = "fortune"
+
+fs.readdirSync(path.resolve(dir)).forEach(function(fn){
+
+ var file = dir + '/' + fn
+ var stat = fs.statSync(file)
+
+ if (stat && ! stat.isDirectory()) {
+ fortunes[fn] = fs.readFileSync(file)
+ .toString()
+ .split("\n")
+ .filter(function(s){ return !! s })
+ }
+
+})
+
+module.exports = function(tag){
+ if (tag in fortunes) {
+ return choice(fortunes[tag])
+ }
+ else {
+ return "bucky"
+ }
+} \ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
index 0489c77..20ccce5 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -17,6 +17,8 @@ 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
@@ -39,11 +41,11 @@ site.init = function(){
cookie: { domain: '.' + process.env.HOST_NAME, maxAge: 43200000000 },
store: sessionstore.createSessionStore({
type: 'mongodb',
- host: 'localhost', // optional
- port: 27017, // optional
- dbName: 'sessionDb', // optional
- collectionName: 'sessions',// optional
- timeout: 10000 // optional
+ host: 'localhost',
+ port: 27017,
+ dbName: 'sessionDb',
+ collectionName: 'sessions',
+ timeout: 10000
}),
resave: true,
saveUninitialized: false,
@@ -57,32 +59,47 @@ site.init = function(){
app.all('*', middleware.ensureLocals)
server = http.createServer(app).listen(5000, function () {
- console.log('Bucky listening at https://lvh.me:%s', server.address().port)
+ console.log('Bucky listening at http://5.k:%s', server.address().port)
})
site.route()
}
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", {})
+ res.render("pages/login", { title: "" })
})
app.get("/index", middleware.ensureAuthenticated, function(req, res){
- res.render("pages/index", {})
+ res.render("pages/index", { title: fortune("titles") })
})
-
app.post("/api/login", auth.loggedInLocal)
- app.get("/api/index", function(req, res){
- })
- app.get("/api/thread", function(req, res){
- })
- app.post("/api/thread", function(req, res){
+ app.get("/api/index",
+ bucky.ensureLatestThreads,
+ bucky.ensureCommentCountsForThreads,
+ bucky.ensureFileCountsForThreads,
+ function(req, res){
+ res.json({
+ threads: res.threads,
+ })
+ }
+ )
+
+ app.get("/api/thread/:id", function(req, res){
+ bucky.ensureThread,
+ bucky.ensureCommentsForThread,
+ bucky.ensureFilesForThread,
+ function(req, res){
+ res.json({
+ thread: res.thread,
+ comments: res.comments,
+ files: res.files,
+ })
+ }
})
app.post("/api/thread/:id", function(req, res){
})
diff --git a/lib/middleware.js b/lib/middleware.js
index 840718e..b38cec3 100644
--- a/lib/middleware.js
+++ b/lib/middleware.js
@@ -10,6 +10,7 @@ var middleware = module.exports = {
ensureLocals: function (req, res, next) {
res.locals.csrfToken = req.csrfToken()
+ res.locals.title = "bucky"
next()
},