* A bit of experimental work on Message obj

This commit is contained in:
Bryan Ashby 2015-07-12 00:32:31 -06:00
parent 1bb997133f
commit ab12974430
4 changed files with 97 additions and 10 deletions

View File

@ -1,6 +1,93 @@
/* jslint node: true */
'use strict';
var msgDb = require('./database.js').dbs.message;
var uuid = require('node-uuid');
var async = require('async');
var _ = require('lodash');
function Message(options) {
var self = this;
this.fromExisting = function(opts) {
self.messageId = opts.messageId;
self.areaId = opts.areaId;
self.uuid = opts.uuid;
self.replyToMsgId = opts.replyToMsgId;
self.toUserName = opts.toUserName;
self.fromUserName = opts.fromUserName;
self.subject = opts.subject;
self.message = opts.message;
self.modTimestamp = opts.modTimestamp;
};
this.isValid = function() {
// :TODO: validate as much as possible
return true;
};
this.createMessageTimestamp = function() {
return new Date().toISOString();
};
}
Message.prototype.persist = function(cb) {
if(!this.isValid()) {
cb(new Error('Cannot persist invalid message!'));
return;
}
var self = this;
async.series(
[
function beginTransaction(callback) {
msgDb.run('BEGIN;', function transBegin(err) {
callback(err);
});
},
function storeMessage(callback) {
msgDb.run(
'INSERT INTO message (area_id, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, message, modified_timestamp) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?, ?);', [ self.areaId, self.uuid, self.replyToMsgId, self.toUserName, self.fromUserName, self.subject, self.message, self.createMessageTimestamp() ],
function msgInsert(err) {
if(!err) {
self.messageId = this.lastID;
}
callback(err);
}
);
},
function storeMeta(callback) {
if(!self.meta) {
callback(null);
} else {
// :TODO: this should be it's own method such that meta can be updated
var metaStmt = msgDb.prepare(
'INSERT INTO message_meta (message_id, meta_name, meta_value) ' +
'VALUES (?, ?, ?);');
async.each(Object.keys(self.meta), function meta(metaName, next) {
metaStmt.run(self.messageId, metaName, self.meta[metaName], function insRan(err) {
next(err);
});
}, function complete(err) {
if(!err) {
metaStmt.finalize(function finalized() {
callback(null);
});
} else {
callback(err);
}
});
}
}
]
);
};

View File

@ -367,15 +367,13 @@ function FullScreenEditorModule(options) {
},
editModeMenuSave : function(formData, extraArgs) {
},
editModeMenuQuote : function(formData, extraArgs) {
},
editModeMenuHelp : function(formData, extraArgs) {
self.viewControllers.footerEditMenu.setFocus(false);
self.displayHelp();
},
editModeMenu : function(formData, extraArgs) {
console.log('menu ' + formData.value['1'])
}
};
}

View File

@ -576,14 +576,15 @@
"HM1" : {
"mci" : {
"HM1" : {
"items" : [ "Save", "Discard", "Menu 3", "Help" ]
// :TODO: Continue, Save, Discard, Clear, Quote, Help
"items" : [ "Save", "Discard", "Quote", "Help" ]
}
},
"submit" : {
"*" : [
{
"value" : { "1" : 0 },
"action" : "@method:editModeMenu"
"action" : "@method:editModeMenuSave"
},
{
"value" : { "1" : 1 },
@ -591,7 +592,7 @@
},
{
"value" : { "1" : 2 },
"action" : "@method:editModeMenu"
"action" : "@method:editModeMenuQuote"
},
{
"value" : { "1" : 3 },

View File

@ -13,7 +13,8 @@
"sqlite3" : "3.0.x",
"ssh2" : "0.4.x",
"strip-json-comments" : "1.0.x",
"gapbuffer" : "0.0.2"
"gapbuffer" : "0.0.2",
"node-uuid" : "1.4.x"
},
"engine" : "node >= 0.12.2"
}