From 3aa171fbaf05d0ee5b82673443da51ed43719475 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 25 Sep 2015 17:36:53 -0400 Subject: moving things around --- bucky/db/index.js | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 bucky/db/index.js (limited to 'bucky/db/index.js') diff --git a/bucky/db/index.js b/bucky/db/index.js new file mode 100644 index 0000000..b6fa235 --- /dev/null +++ b/bucky/db/index.js @@ -0,0 +1,156 @@ +var db = module.exports + +var connection = require("./bookshelf") +var bookshelf = connection.bookshelf +var knex = connection.knex + + +/* MODELS */ + +var User = db.User = bookshelf.Model.extend({ + tableName: 'users', + hasTimestamps: false, +}) +var Thread = db.Thread = bookshelf.Model.extend({ + tableName: 'threads', + hasTimestamps: false, +}) +var Comment = db.Comment = bookshelf.Model.extend({ + tableName: 'comments', + hasTimestamps: false, +}) +var File = db.File = bookshelf.Model.extend({ + tableName: 'files', + hasTimestamps: false, +}) +var Keyword = db.Keyword = bookshelf.Model.extend({ + tableName: 'keywords', + hasTimestamps: false, +}) +var Mailbox = db.Mailbox = bookshelf.Model.extend({ + tableName: 'boxes', + hasTimestamps: false, +}) +var Message = db.Message = bookshelf.Model.extend({ + tableName: 'messages', + hasTimestamps: false, +}) + +/* USERS */ + +db.createUser = function(data){ + return new db.User(data).save() +} +db.getUsers = function () { + return User.query(function(qb){ + qb.orderBy("id", "desc") + }).fetchAll() +} +db.getUser = function(id) { + var model = new User({'id': id}) + return model.fetch() +} +db.getUserByUsername = function(username) { + var model = new User({'username': username}) + return model.fetch() +} +db.getLastlog = function(limit){ + return knex.column('username').column('lastseen').select().from('users').orderBy('lastseen', 'desc').limit(limit || 10) +} + + +/* THREADS */ + +db.getLatestThreads = function () { + return Thread.query(function(qb){ + qb.orderBy("id", "desc").limit(50) + }).fetchAll() +} +db.getThreadsForKeyword = function (keyword) { + return Thread.query(function(qb){ + qb.where("keyword", "=", keyword).orderBy("id", "desc") + }).fetchAll() +} +db.getThread = function (id) { + return Thread.query("where", "id", "=", id).fetch() +} + + +/* FILES */ + +db.getFilesForThread = function (id){ + return File.query("where", "thread", "=", id).fetchAll() +} +db.getFileCounts = function(ids){ + return knex.column('thread').count('* as count').select().from('files').where('thread', 'in', ids).groupBy('thread') +} +db.getFileSizes = function(ids){ + return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread') +} + + +/* COMMENTS */ + +db.getCommentsForThread = function (id, limit, offset, order){ + order = order || "asc" + return Comment.query(function(qb){ + qb.where("thread", "=", id).orderBy("id", order) + if (limit) { + qb.limit(limit) + } + if (offset) { + qb.offset(offset) + } + }).fetchAll().then(function(comments){ + comments.forEach(function(comment){ + comment.set("comment", comment.get("comment").toString() ) + }) + return comments + }) +} +db.getCommentCounts = function(ids){ + return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread') +} + + +/* KEYWORDS */ + +db.getKeywords = function (keywords){ + return Keyword.query("where", "keyword", "in", keywords).fetchAll() +} +db.getKeyword = function (keyword) { + return Keyword.query("where", "keyword", "=", keyword).fetch() +} + + +/* MAILBOXES */ + +db.getMailboxes = function(username){ + return Mailbox.query("where", "owner", "=", username).fetchAll() +} +db.getMailboxCounts = function(boxes){ + return knex.column('mbox').count('* as count').select().from('messages').where('mbox', 'in', boxes).groupBy('mbox') +} + + +/* MESSAGES */ + +db.getMessages = function(username, box, limit, offset){ + var mbox = username + "." + box + return Message.query(function(qb){ + qb.column('id', 'mbox', 'unread', 'sender', 'recipient', 'date', 'subject', knex.raw("CHAR_LENGTH(body) as size")).where("mbox", "=", mbox).orderBy("id", "desc") + if (limit) { + qb.limit(limit) + } + if (offset) { + qb.offset(offset) + } + }).fetchAll() +} +db.getMessage = function (id){ + var model = new Message({'id': id}) + return model.fetch().then(function(message){ + message.set("body", message.get("body").toString() ) + return message + }) +} -- cgit v1.2.3-70-g09d2 From ed083ad3191a5d7d61abb21fa7dafec5121ecaab Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 25 Sep 2015 18:05:37 -0400 Subject: post comment --- bucky/app/bucky.js | 22 ++++++++++++++- bucky/app/index.js | 4 +-- bucky/app/router.js | 8 +++++- bucky/db/index.js | 3 ++ bucky/util/util.js | 2 ++ public/assets/js/lib/views/index/hootbox.js | 23 +++++++++++++-- public/assets/js/vendor/view/formview.js | 44 ++++++++++++++--------------- views/partials/hootbox.ejs | 2 +- 8 files changed, 78 insertions(+), 30 deletions(-) (limited to 'bucky/db/index.js') diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 43799fe..008427d 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -1,5 +1,5 @@ var db = require('../db') -var util = require('../lib/util') +var util = require('../util/util') var _ = require('lodash') var bucky = module.exports = { @@ -136,6 +136,26 @@ var bucky = module.exports = { }) }, + /* 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){ diff --git a/bucky/app/index.js b/bucky/app/index.js index ad97526..d06b018 100644 --- a/bucky/app/index.js +++ b/bucky/app/index.js @@ -23,8 +23,8 @@ 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.set('views', path.join(__dirname, '../../views')) + app.use(express.static(path.join(__dirname, '../../public'))) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: false })) app.use( multer({ dest:'./uploads/' }).single("file") ) diff --git a/bucky/app/router.js b/bucky/app/router.js index 7ac6599..fe7d336 100644 --- a/bucky/app/router.js +++ b/bucky/app/router.js @@ -66,8 +66,14 @@ module.exports = function(app){ }) app.post("/api/thread/:id/comment", middleware.ensureAuthenticated, + bucky.ensureThread, + // ensure thread privacy + bucky.createComment, + // add comments and files function(req, res){ - // add comments and files + res.json({ + comment: res.comment + }) }) app.delete("/api/thread/:id", middleware.ensureAuthenticated, diff --git a/bucky/db/index.js b/bucky/db/index.js index b6fa235..77e94fa 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -111,6 +111,9 @@ db.getCommentsForThread = function (id, limit, offset, order){ db.getCommentCounts = function(ids){ return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread') } +db.createComment = function(comment){ + return new Comment(comment).save() +} /* KEYWORDS */ diff --git a/bucky/util/util.js b/bucky/util/util.js index e67488b..d4b6b8a 100644 --- a/bucky/util/util.js +++ b/bucky/util/util.js @@ -2,3 +2,5 @@ var util = module.exports = {} util.sanitizeName = function (s){ return (s || "").replace(new RegExp("[^-_a-zA-Z0-9]", 'g'), "") } util.sanitize = function (s){ return (s || "").replace(/<>&/g, "") } + +util.now = function(){ return Math.floor( (+ new Date()) / 1000 ) } diff --git a/public/assets/js/lib/views/index/hootbox.js b/public/assets/js/lib/views/index/hootbox.js index 9d1cc3d..4a23d32 100644 --- a/public/assets/js/lib/views/index/hootbox.js +++ b/public/assets/js/lib/views/index/hootbox.js @@ -5,10 +5,13 @@ var HootBox = FormView.extend({ events: { }, + action: "/api/thread/1/comment", + initialize: function(){ this.__super__.initialize.call(this) this.template = this.$(".template").html() this.$hoots = this.$("#hoots") + this.$comment = this.$("[name=comment]") }, load: function(comments){ @@ -31,7 +34,23 @@ var HootBox = FormView.extend({ this.$hoots.append($el) }, - success: function(){ - this.prependComment(comment) + validate: function(){ + var errors = [] + if (! this.$comment.val()) { + errors.push("no comment") + return errors + } + return null + }, + + beforeSend: function(){ + this.$comment.val("") + }, + + showErrors: function(){ + }, + + success: function(data){ + this.prependComment(data.comment) } }) \ No newline at end of file diff --git a/public/assets/js/vendor/view/formview.js b/public/assets/js/vendor/view/formview.js index 05b1ecb..7e77500 100644 --- a/public/assets/js/vendor/view/formview.js +++ b/public/assets/js/vendor/view/formview.js @@ -80,7 +80,7 @@ var FormView = View.extend({ var action = typeof this.action == "function" ? this.action() : this.action if (! action) return - + var request = $.ajax({ url: action, type: this.method, @@ -90,30 +90,27 @@ var FormView = View.extend({ processData: false, contentType: false, success: function(response){ - - if (response.error) { - var errors = [] - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); - } - if (errorCallback) { - errorCallback(errors) - } - else { - this.showErrors(errors) - } - return + if (response.error) { + var errors = [] + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + if (errorCallback) { + errorCallback(errors) } else { - if (successCallback) { - successCallback(response) - } - if (this.success) { - this.success(response) - } + this.showErrors(errors) } - - + return + } + else { + if (successCallback) { + successCallback(response) + } + if (this.success) { + this.success(response) + } + } }.bind(this), error: function(response){ }.bind(this), @@ -127,7 +124,8 @@ var FormView = View.extend({ if (this.useMinotaur) { Minotaur.show() } - + + this.beforeSend && this.beforeSend() }, }) diff --git a/views/partials/hootbox.ejs b/views/partials/hootbox.ejs index c65d264..872ca68 100644 --- a/views/partials/hootbox.ejs +++ b/views/partials/hootbox.ejs @@ -1,6 +1,6 @@
- +
-- cgit v1.2.3-70-g09d2