From aad0b5e372654916ec910456484616930120aacc Mon Sep 17 00:00:00 2001 From: reality Date: Fri, 12 Apr 2013 22:30:45 +0000 Subject: [PATCH] Some cleanup of quotes module postdatabank [#331] Addition of addQuote API and updated places what use it [#349] --- modules/quotes/commands.js | 47 +++++++++------------------------- modules/quotes/quotes.js | 52 ++++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/modules/quotes/commands.js b/modules/quotes/commands.js index 69a5a79..d7e6de3 100644 --- a/modules/quotes/commands.js +++ b/modules/quotes/commands.js @@ -3,43 +3,24 @@ var _ = require('underscore')._, uuid = require('node-uuid'); var commands = function(dbot) { - var quotes = dbot.db.quoteArrs; var commands = { /*** Quote Addition ***/ // Add a quote to a category '~qadd': function(event) { - var key = event.input[1].toLowerCase().trim(), - quote = event.input[2], - newCount, - category = false; + var key = event.input[1].toLowerCase().trim(); + quote = event.input[2]; - this.db.search('quote_category', { 'name': key }, function(result) { - category = result; - }, function(err) { - if(!category) { - var id = uuid.v4(); - category = { - 'id': id, - 'name': key, - 'quotes': [], - 'creator': event.user - }; - } - - newCount = category.quotes.push(quote); - this.db.save('quote_category', category.id, category, function(err) { - this.rmAllowed = true; - dbot.api.event.emit('~qadd', { - 'key': key, - 'text': quote - }); - event.reply(dbot.t('quote_saved', { - 'category': key, - 'count': newCount - })); + this.api.addQuote(key, quote, event.user, function(newCount) { + dbot.api.event.emit('~qadd', { + 'key': key, + 'text': quote }); - }.bind(this)); + event.reply(dbot.t('quote_saved', { + 'category': key, + 'count': newCount + })); + }); }, /*** Quote Retrieval ***/ @@ -102,10 +83,7 @@ var commands = function(dbot) { var rmCacheCount = rmCache.length; _.each(rmCache, function(quote, index) { - //TODO: Add quote add API func - var qadd = _.clone(event); - qadd.message = '~qadd ' + quote.key + '=' + quote.quote; - dbot.instance.emit(qadd); + this.api.addQuote(quote.key, quote.quote, event.user, function(newCount) { }); }); rmCache.length = 0; @@ -233,7 +211,6 @@ var commands = function(dbot) { } }, function(err) { if(matches.length > 0) { - console.log(matches); event.reply(dbot.t('search_results', { 'category': matches[0].category, 'needle': needle, diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index 48e36cf..b996614 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -1,16 +1,16 @@ -var _ = require('underscore')._; +var _ = require('underscore')._, + uuid = require('node-uuid'); var quotes = function(dbot) { dbot.sessionData.rmCache = []; - this.quotes = dbot.db.quoteArrs, - this.addStack = [], - this.rmAllowed = true, - this.rmCache = dbot.sessionData.rmCache, + this.rmCache = dbot.sessionData.rmCache; + this.quotes = dbot.db.quoteArrs; + this.rmAllowed = true; this.rmTimer; this.internalAPI = { + // Parse quote interpolations 'interpolatedQuote': function(server, channel, key, quote, callback) { - // Parse quote interpolations var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g); if(quoteRefs) { var ref = dbot.cleanNick(quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim()); @@ -58,9 +58,36 @@ var quotes = function(dbot) { }; this.api = { + 'addQuote': function(key, quote, user, callback) { + var key = key.toLowerCase().trim(), + newCount, + category = false; + + this.db.search('quote_category', { 'name': key }, function(result) { + category = result; + }, function(err) { + if(!category) { + var id = uuid.v4(); + category = { + 'id': id, + 'name': key, + 'quotes': [], + 'owner': user + }; + } + + newCount = category.quotes.push(quote); + this.db.save('quote_category', category.id, category, function(err) { + this.rmAllowed = true; + callback(newCount); + }.bind(this)); + }.bind(this)); + + }, + 'getQuote': function(key, callback) { - var category = false; - key = key.trim().toLowerCase(), + var category = false, + key = key.trim().toLowerCase(); this.db.search('quote_category', { 'name': key }, function(result) { category = result; @@ -93,13 +120,12 @@ var quotes = function(dbot) { } if(once) { - event.message = '~qadd realityonce=reality ' + once[1]; - event.action = 'PRIVMSG'; - event.params = event.message.split(' '); - dbot.instance.emit(event); + this.api.addQuote('realityonce', 'reality' + once[1], event.user, function(newCount) { + event.reply('\'reality ' + once[1] + '\' saved (' + newCount + ').'); + }); } } else if(event.action == 'JOIN') { - var userQuote = this.api.getQuote(event.user, function(quote) { + var userQuote = this.api.addQuote(event.user, function(quote) { if(quote) { event.reply(event.user + ': ' + quote); }