summaryrefslogtreecommitdiff
path: root/bucky/app
diff options
context:
space:
mode:
Diffstat (limited to 'bucky/app')
-rw-r--r--bucky/app/bucky.js211
-rw-r--r--bucky/app/index.js63
-rw-r--r--bucky/app/router.js156
3 files changed, 430 insertions, 0 deletions
diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js
new file mode 100644
index 0000000..757592a
--- /dev/null
+++ b/bucky/app/bucky.js
@@ -0,0 +1,211 @@
+var db = require('../db')
+var util = require('../util/util')
+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){
+ if (lookup[thread.id]) {
+ 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, 15, 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()
+ })
+ },
+
+ /* COMMENTS */
+
+ createComment: function (req, res, next){
+ if (! req.body.comment || ! req.body.comment.length) {
+ res.json({ error: "no comment" })
+ return
+ }
+ var data = {
+ thread: res.thread.get('id'),
+ parent_id: req.body.parent_id || -1,
+ username: req.user.get('username'),
+ date: util.now(),
+ comment: req.body.comment,
+ }
+ db.createComment(data).then(function(comment){
+ res.comment = comment
+ 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()
+ })
+ },
+ ensureMessage: function(req, res, next){
+ db.getMessage(req.params.id).then(function(message){
+ var username = req.user.get('username')
+ if (username !== message.get('recipient') && username !== message.get('sender')) {
+ res.sendStatus(404)
+ return
+ }
+ res.message = message
+ next()
+ })
+ }
+} \ No newline at end of file
diff --git a/bucky/app/index.js b/bucky/app/index.js
new file mode 100644
index 0000000..03c5593
--- /dev/null
+++ b/bucky/app/index.js
@@ -0,0 +1,63 @@
+require('dotenv').load();
+var fs = require('fs')
+var app, express = require('express');
+var http = require('http');
+var https = require('https');
+var bodyParser = require('body-parser')
+var cookieParser = require('cookie-parser')
+var csurf = require('csurf')
+var path = require('path')
+var multiparty = require('multiparty')
+var ejs = require('ejs')
+var favicon = require('serve-favicon')
+var passport = require('passport')
+var sessionstore = require('sessionstore')
+var session = require('express-session')
+var multer = require('multer')
+
+var app, server
+
+var mongodb = require('mongodb')
+
+var site = module.exports = {}
+site.init = function(){
+ app = express()
+ app.set('port', 5000)
+ app.set('view engine', 'ejs')
+ app.set('views', path.join(__dirname, '../../views'))
+ app.use(express.static(path.join(__dirname, '../../public')))
+
+ app.use(favicon(__dirname + '../../public/favicon.ico'))
+ app.use(bodyParser.json())
+ app.use(bodyParser.urlencoded({ extended: false }))
+ app.use( multer({ dest:'./uploads/' }).single("file") )
+
+ app.use(session({
+ key: 'bucky.sid',
+ secret: 'argonauts',
+ cookie: { domain: '.' + process.env.HOST_NAME, maxAge: 43200000000 },
+ store: sessionstore.createSessionStore({
+ type: 'mongodb',
+ host: 'localhost',
+ port: 27017,
+ dbName: 'buckySessionDb',
+ collectionName: 'sessions',
+ timeout: 10000,
+ }),
+ resave: true,
+ saveUninitialized: false,
+ }))
+ app.use(csurf({ cookie: false }))
+
+ app.use(express.query())
+ app.use(passport.initialize())
+ app.use(passport.session())
+
+ server = http.createServer(app).listen(process.env.PORT || 5000, function () {
+ console.log('Bucky listening at http://5.k:%s', server.address().port)
+ })
+
+ site.route(app)
+}
+
+site.route = require('./router')
diff --git a/bucky/app/router.js b/bucky/app/router.js
new file mode 100644
index 0000000..c3af565
--- /dev/null
+++ b/bucky/app/router.js
@@ -0,0 +1,156 @@
+var auth = require('./auth')
+var middleware = require('./middleware')
+var fortune = require('./fortune')
+var bucky = require('./bucky')
+var db = require('./db')
+var util = require('./util')
+
+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.ensureLastlog,
+ middleware.ensureAuthenticated,
+ bucky.ensureLatestThreads,
+ bucky.ensureCommentCountsForThreads,
+ bucky.ensureFileCountsForThreads,
+ bucky.ensureKeywordsForThreads,
+ bucky.ensureHootbox,
+ function(req, res){
+ res.json({
+ threads: res.threads,
+ hootbox: res.hootbox,
+ lastlog: res.lastlog,
+ })
+ }
+ )
+ app.get("/api/thread/:id",
+ middleware.ensureAuthenticated,
+ 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",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ // make a new thread
+ })
+ app.post("/api/thread/:id/comment",
+ middleware.ensureAuthenticated,
+ bucky.ensureThread,
+ // ensure thread privacy
+ bucky.createComment,
+ function(req, res){
+ res.json({
+ comment: res.comment
+ })
+ })
+ app.delete("/api/thread/:id",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ // delete a thread
+ })
+ app.put("/api/comment/:id",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ // edit a comment
+ })
+ app.delete("/api/comment/:id",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ // delete a comment
+ })
+
+
+ app.get("/api/keyword/:keyword",
+ middleware.ensureAuthenticated,
+ bucky.ensureKeyword,
+ bucky.ensureThreadsForKeyword,
+ bucky.ensureCommentCountsForThreads,
+ bucky.ensureFileCountsForThreads,
+ bucky.ensureKeywordsForThreads,
+ function(req, res){
+ res.json({
+ keyword: res.keyword,
+ threads: res.threads,
+ })
+ }
+ )
+
+ app.get("/mail/",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ res.render("pages/mailbox", {title: "inbox" })
+ }
+ )
+ app.get("/mail/:box",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ res.render("pages/mailbox", { title: util.sanitize(req.params.box) })
+ }
+ )
+ app.get("/message/:id",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ res.render("pages/message", { title: util.sanitize(req.params.box) })
+ }
+ )
+ app.get("/api/mailbox/:box",
+ middleware.ensureAuthenticated,
+ bucky.ensureMailboxes,
+ bucky.ensureMailboxCounts,
+ bucky.ensureMessages,
+ function(req, res){
+ res.json({
+ user: { id: req.user.get("id"), username: req.user.get("username") },
+ messages: res.messages,
+ boxes: res.boxes,
+ })
+ }
+ )
+ app.get("/api/message/:id",
+ middleware.ensureAuthenticated,
+ bucky.ensureMessage,
+ function(req, res){
+ res.json({
+ message: res.message,
+ })
+ })
+ app.post("/mail/",
+ middleware.ensureAuthenticated,
+ function(req, res){
+ // send new mail
+ }
+ )
+
+}