summaryrefslogtreecommitdiff
path: root/lib/bucky.js
blob: db1672c84cf6d74bf488ed9857e8f266df7fee86 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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){
        lookup[c.thread] = c
      })
      res.threads.forEach(function(thread){
        thread.set("comment_count", lookup[thread.id].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
      })
      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"))
        }
      })
      next()
    })
  },
  ensureHootbox: function (req, res, next){
    db.getCommentsForThread(1, 9, 0, "desc").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){
    var id = req.params.id.replace(/\D/g, "")
    if (! id) {
      return res.sendStatus(404)
    }
    db.getThread(id).then(function(thread){
      if (thread) {
        res.thread = thread
        next()
      }
      else {
        res.sendStatus(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(res.thread.get('id')).then(function(comments){
      res.comments = comments
      next()
    })
  },
  ensureFilesForThread: function (req, res, next){
    db.getFilesForThread(res.thread.get('id')).then(function(files){
      res.files = files
      next()
    })
  },
  
  /* KEYWORDS */

  ensureKeyword: function (req, res, next){
    var keyword = req.params.keyword
    if (! keyword) {
      return res.sendStatus(404)
    }
    db.getKeyword(keyword).then(function(k){
      if (! k) {
        return res.sendStatus(404)
      }
      res.keyword = k
      next()
    })
  },
  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()
    })
  },
  
  /* MAIL */

  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.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.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){
    db.getMessages(req.user.get('username'), req.params.box, 50, 0).then(function(messages){
      res.messages = messages
      next()
    })
  },
}