summaryrefslogtreecommitdiff
path: root/lib/bucky.js
blob: aed64940232dafae3aaa7749e4d7f0548e181dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var db = require('./db')

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()
      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,i){
        res.threads.at(i).set("file_count", c.count)
      })
      next()
    })
  },
  ensureFileSizeForThreads: function (req, res, next){
    db.getFileSizes(res.threads_ids).then(function(sizes){
      var lookup = {}
      sizes.forEach(function(c,i){
        res.threads.at(i).set("file_size", c.size)
      })
      next()
    })
  },
  ensureHootbox: function (req, res, next){
    db.getCommentsForThread(1, 50).then(function(hootbox){
      res.hootbox = hootbox.forEach(function(comment){
        comment.set("comment", comment.get("comment").toString() )
      })
      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()
    })
  },

}