var db = require('./db') var _ = require('lodash') var bucky = module.exports = { /* INDEX */ ensureLatestThreads: function (req, res, next){ db.getLatestThreads().then(function(threads){ res.threads = threads res.threads_ids = res.threads.pluck("id").sort() res.keywords = _.uniq(res.threads.pluck("keyword")) next() }) }, 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) }) next() }) }, ensureFileCountsForThreads: function (req, res, next){ db.getFileCounts(res.threads_ids).then(function(counts){ var lookup = {} counts.forEach(function(c){ lookup[c.thread] = c }) res.threads.forEach(function(t){ var c = lookup[t.id] t.set("file_count", c ? c.count : 0) }) next() }) }, ensureKeywordsForThreads: function (req, res, next){ db.getKeywords(res.keywords).then(function(keywords){ var lookup = {} 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 var k = lookup[kw] if (! k) return if (! t.get("color")) { t.set("color", k.get("color")) console.log(k.get("color")) } }) next() }) }, ensureHootbox: function (req, res, next){ db.getCommentsForThread(1, 9).then(function(hootbox){ res.hootbox = hootbox next() }) }, ensureLastlog: function (req, res, next){ db.getLastlog(6).then(function(lastlog){ res.lastlog = lastlog next() }) }, /* DETAILS */ ensureThread: function (req, res, next){ db.getThread(req.param.id).then(function(thread){ if (thread) { res.thread = thread next() } else { res.sendCode(404) } }) }, ensureKeywordForThread: function (req, res, next){ var keyword = res.thread.get('keyword') if (! keyword) return next() db.getKeyword(keyword).then(function(keyword){ res.keyword = keyword next() }) }, ensureCommentsForThread: function (req, res, next){ db.getCommentsForThread(id).then(function(comments){ res.comments = comments next() }) }, ensureFilesForThread: function (req, res, next){ db.getCommentsForThread(id).then(function(files){ res.files = files next() }) }, }