2016-06-29 04:41:50 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('../core/menu_module.js').MenuModule;
|
2017-09-16 23:24:26 +00:00
|
|
|
|
|
|
|
const {
|
2017-09-16 23:13:11 +00:00
|
|
|
getModDatabasePath,
|
|
|
|
getTransactionDatabase
|
2017-09-16 23:24:26 +00:00
|
|
|
} = require('../core/database.js');
|
|
|
|
|
2016-06-29 04:41:50 +00:00
|
|
|
const ViewController = require('../core/view_controller.js').ViewController;
|
|
|
|
const theme = require('../core/theme.js');
|
|
|
|
const ansi = require('../core/ansi_term.js');
|
2016-08-27 03:30:51 +00:00
|
|
|
const stringFormat = require('../core/string_format.js');
|
2016-06-29 04:41:50 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const sqlite3 = require('sqlite3');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2016-06-29 05:53:30 +00:00
|
|
|
const moment = require('moment');
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2016-07-04 18:56:54 +00:00
|
|
|
/*
|
|
|
|
Module :TODO:
|
|
|
|
* Add pipe code support
|
|
|
|
- override max length & monitor *display* len as user types in order to allow for actual display len with color
|
|
|
|
* Add preview control: Shows preview with pipe codes resolved
|
|
|
|
* Add ability to at least alternate formatStrings -- every other
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-06-29 04:41:50 +00:00
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Onelinerz',
|
|
|
|
desc : 'Standard local onelinerz',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.onelinerz',
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciViewIds = {
|
2016-06-29 04:41:50 +00:00
|
|
|
ViewForm : {
|
|
|
|
Entries : 1,
|
|
|
|
AddPrompt : 2,
|
|
|
|
},
|
|
|
|
AddForm : {
|
2016-06-29 05:53:30 +00:00
|
|
|
NewEntry : 1,
|
|
|
|
EntryPreview : 2,
|
|
|
|
AddPrompt : 3,
|
2016-06-29 04:41:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
|
|
|
View : 0,
|
|
|
|
Add : 1,
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class OnelinerzModule extends MenuModule {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
viewAddScreen : function(formData, extraArgs, cb) {
|
|
|
|
return self.displayAddScreen(cb);
|
|
|
|
},
|
|
|
|
|
|
|
|
addEntry : function(formData, extraArgs, cb) {
|
|
|
|
if(_.isString(formData.value.oneliner) && formData.value.oneliner.length > 0) {
|
|
|
|
const oneliner = formData.value.oneliner.trim(); // remove any trailing ws
|
|
|
|
|
|
|
|
self.storeNewOneliner(oneliner, err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.message }, 'Failed saving oneliner');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.clearAddForm();
|
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
|
|
|
});
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
} else {
|
|
|
|
// empty message - treat as if cancel was hit
|
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
|
|
|
}
|
|
|
|
},
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
cancelAdd : function(formData, extraArgs, cb) {
|
|
|
|
self.clearAddForm();
|
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
2016-06-29 04:41:50 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
return self.beforeArt(callback);
|
2016-06-29 04:41:50 +00:00
|
|
|
},
|
|
|
|
function display(callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
return self.displayViewScreen(false, callback);
|
2016-06-29 04:41:50 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
// :TODO: Handle me -- initSequence() should really take a completion callback
|
|
|
|
}
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayViewScreen(clearScreen, cb) {
|
|
|
|
const self = this;
|
2016-06-29 04:41:50 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function clearAndDisplayArt(callback) {
|
|
|
|
if(self.viewControllers.add) {
|
|
|
|
self.viewControllers.add.setFocus(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(clearScreen) {
|
|
|
|
self.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
2017-01-26 05:18:05 +00:00
|
|
|
self.menuConfig.config.art.entries,
|
2016-06-29 04:41:50 +00:00
|
|
|
self.client,
|
|
|
|
{ font : self.menuConfig.font, trailingLF : false },
|
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function initOrRedrawViewController(artData, callback) {
|
|
|
|
if(_.isUndefined(self.viewControllers.add)) {
|
|
|
|
const vc = self.addViewController(
|
|
|
|
'view',
|
|
|
|
new ViewController( { client : self.client, formId : FormIds.View } )
|
|
|
|
);
|
|
|
|
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : FormIds.View,
|
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
} else {
|
|
|
|
self.viewControllers.view.setFocus(true);
|
2017-01-26 05:18:05 +00:00
|
|
|
self.viewControllers.view.getView(MciViewIds.ViewForm.AddPrompt).redraw();
|
2016-06-29 04:41:50 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function fetchEntries(callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
const entriesView = self.viewControllers.view.getView(MciViewIds.ViewForm.Entries);
|
2016-06-29 04:41:50 +00:00
|
|
|
const limit = entriesView.dimens.height;
|
|
|
|
let entries = [];
|
|
|
|
|
|
|
|
self.db.each(
|
2016-06-30 05:23:06 +00:00
|
|
|
`SELECT *
|
|
|
|
FROM (
|
|
|
|
SELECT *
|
|
|
|
FROM onelinerz
|
|
|
|
ORDER BY timestamp DESC
|
|
|
|
LIMIT ${limit}
|
|
|
|
)
|
|
|
|
ORDER BY timestamp ASC;`,
|
2016-06-29 04:41:50 +00:00
|
|
|
(err, row) => {
|
|
|
|
if(!err) {
|
2016-06-29 05:53:30 +00:00
|
|
|
row.timestamp = moment(row.timestamp); // convert -> moment
|
2016-06-29 04:41:50 +00:00
|
|
|
entries.push(row);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, entriesView, entries);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function populateEntries(entriesView, entries, callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
const listFormat = self.menuConfig.config.listFormat || '{username}@{ts}: {oneliner}';// :TODO: should be userName to be consistent
|
|
|
|
const tsFormat = self.menuConfig.config.timestampFormat || 'ddd h:mma';
|
2016-06-29 04:41:50 +00:00
|
|
|
|
|
|
|
entriesView.setItems(entries.map( e => {
|
2016-08-27 03:30:51 +00:00
|
|
|
return stringFormat(listFormat, {
|
2016-06-29 04:41:50 +00:00
|
|
|
userId : e.user_id,
|
|
|
|
username : e.user_name,
|
|
|
|
oneliner : e.oneliner,
|
2016-06-30 05:23:06 +00:00
|
|
|
ts : e.timestamp.format(tsFormat),
|
2016-06-29 04:41:50 +00:00
|
|
|
} );
|
|
|
|
}));
|
|
|
|
|
|
|
|
entriesView.redraw();
|
|
|
|
|
2016-07-04 18:56:54 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function finalPrep(callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
const promptView = self.viewControllers.view.getView(MciViewIds.ViewForm.AddPrompt);
|
2016-07-04 18:56:54 +00:00
|
|
|
promptView.setFocusItemIndex(1); // default to NO
|
2016-06-29 04:41:50 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
2016-06-29 05:53:30 +00:00
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayAddScreen(cb) {
|
|
|
|
const self = this;
|
2016-06-29 04:41:50 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function clearAndDisplayArt(callback) {
|
|
|
|
self.viewControllers.view.setFocus(false);
|
|
|
|
self.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
2017-01-26 05:18:05 +00:00
|
|
|
self.menuConfig.config.art.add,
|
2016-06-29 04:41:50 +00:00
|
|
|
self.client,
|
|
|
|
{ font : self.menuConfig.font },
|
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function initOrRedrawViewController(artData, callback) {
|
|
|
|
if(_.isUndefined(self.viewControllers.add)) {
|
|
|
|
const vc = self.addViewController(
|
|
|
|
'add',
|
|
|
|
new ViewController( { client : self.client, formId : FormIds.Add } )
|
|
|
|
);
|
|
|
|
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : FormIds.Add,
|
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
} else {
|
2016-06-29 05:53:30 +00:00
|
|
|
self.viewControllers.add.setFocus(true);
|
2016-06-29 04:41:50 +00:00
|
|
|
self.viewControllers.add.redrawAll();
|
2017-01-26 05:18:05 +00:00
|
|
|
self.viewControllers.add.switchFocus(MciViewIds.AddForm.NewEntry);
|
2016-06-29 04:41:50 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2016-06-29 05:53:30 +00:00
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2016-06-29 05:53:30 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
clearAddForm() {
|
|
|
|
this.setViewText('add', MciViewIds.AddForm.NewEntry, '');
|
|
|
|
this.setViewText('add', MciViewIds.AddForm.EntryPreview, '');
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
initDatabase(cb) {
|
|
|
|
const self = this;
|
2016-06-29 04:41:50 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function openDatabase(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
self.db = getTransactionDatabase(new sqlite3.Database(
|
2016-06-29 04:41:50 +00:00
|
|
|
getModDatabasePath(exports.moduleInfo),
|
2017-01-26 05:18:05 +00:00
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-09-16 23:13:11 +00:00
|
|
|
));
|
2016-06-29 04:41:50 +00:00
|
|
|
},
|
|
|
|
function createTables(callback) {
|
2017-01-26 05:18:05 +00:00
|
|
|
self.db.run(
|
|
|
|
`CREATE TABLE IF NOT EXISTS onelinerz (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
user_id INTEGER_NOT NULL,
|
|
|
|
user_name VARCHAR NOT NULL,
|
|
|
|
oneliner VARCHAR NOT NULL,
|
|
|
|
timestamp DATETIME NOT NULL
|
|
|
|
);`
|
|
|
|
,
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
2016-06-29 04:41:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
2017-01-26 05:18:05 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2016-06-29 05:53:30 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
storeNewOneliner(oneliner, cb) {
|
|
|
|
const self = this;
|
|
|
|
const ts = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ');
|
2016-06-29 05:53:30 +00:00
|
|
|
|
2016-06-30 05:23:06 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function addRec(callback) {
|
|
|
|
self.db.run(
|
|
|
|
`INSERT INTO onelinerz (user_id, user_name, oneliner, timestamp)
|
|
|
|
VALUES (?, ?, ?, ?);`,
|
|
|
|
[ self.client.user.userId, self.client.user.username, oneliner, ts ],
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function removeOld(callback) {
|
|
|
|
// keep 25 max most recent items - remove the older ones
|
|
|
|
self.db.run(
|
|
|
|
`DELETE FROM onelinerz
|
|
|
|
WHERE id IN (
|
|
|
|
SELECT id
|
|
|
|
FROM onelinerz
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT -1 OFFSET 25
|
|
|
|
);`,
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
2017-01-26 05:18:05 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-06-30 05:23:06 +00:00
|
|
|
);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
beforeArt(cb) {
|
|
|
|
super.beforeArt(err => {
|
|
|
|
return err ? cb(err) : this.initDatabase(cb);
|
|
|
|
});
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
};
|