2016-06-29 04:41:50 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
2017-09-16 23:24:26 +00:00
|
|
|
|
|
|
|
const {
|
2018-06-22 05:15:04 +00:00
|
|
|
getModDatabasePath,
|
|
|
|
getTransactionDatabase
|
2018-06-23 03:26:46 +00:00
|
|
|
} = require('./database.js');
|
2017-09-16 23:24:26 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const sqlite3 = require('sqlite3');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2016-06-29 04:41:50 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Module :TODO:
|
|
|
|
* Add ability to at least alternate formatStrings -- every other
|
2016-07-04 18:56:54 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-29 04:41:50 +00:00
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Onelinerz',
|
|
|
|
desc : 'Standard local onelinerz',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.onelinerz',
|
2016-06-29 04:41:50 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciViewIds = {
|
2018-07-10 02:27:09 +00:00
|
|
|
view : {
|
|
|
|
entries : 1,
|
|
|
|
addPrompt : 2,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-07-10 02:27:09 +00:00
|
|
|
add : {
|
|
|
|
newEntry : 1,
|
|
|
|
entryPreview : 2,
|
|
|
|
addPrompt : 3,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
2018-07-10 02:27:09 +00:00
|
|
|
view : 0,
|
|
|
|
add : 1,
|
2016-06-29 04:41:50 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class OnelinerzModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
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) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const oneliner = formData.value.oneliner.trim(); // remove any trailing ws
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.storeNewOneliner(oneliner, err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.message }, 'Failed saving oneliner');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.clearAddForm();
|
2018-06-23 03:26:46 +00:00
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// empty message - treat as if cancel was hit
|
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelAdd : function(formData, extraArgs, cb) {
|
|
|
|
self.clearAddForm();
|
2018-06-23 03:26:46 +00:00
|
|
|
return self.displayViewScreen(true, cb); // true=cls
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
|
|
|
return self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function display(callback) {
|
|
|
|
return self.displayViewScreen(false, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Handle me -- initSequence() should really take a completion callback
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayViewScreen(clearScreen, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2018-07-10 02:27:09 +00:00
|
|
|
function prepArtAndViewController(callback) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(self.viewControllers.add) {
|
|
|
|
self.viewControllers.add.setFocus(false);
|
|
|
|
}
|
|
|
|
|
2018-07-10 02:27:09 +00:00
|
|
|
return self.prepViewControllerWithArt(
|
|
|
|
'view',
|
|
|
|
FormIds.view,
|
|
|
|
{
|
|
|
|
clearScreen,
|
|
|
|
trailingLF : false
|
|
|
|
},
|
|
|
|
(err, artInfo, wasCreated) => {
|
2018-07-10 02:55:47 +00:00
|
|
|
if(!err && !wasCreated) {
|
2018-07-10 02:27:09 +00:00
|
|
|
self.viewControllers.view.setFocus(true);
|
|
|
|
self.viewControllers.view.getView(MciViewIds.view.addPrompt).redraw();
|
|
|
|
}
|
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function fetchEntries(callback) {
|
2018-07-10 02:27:09 +00:00
|
|
|
const entriesView = self.viewControllers.view.getView(MciViewIds.view.entries);
|
2018-06-22 05:15:04 +00:00
|
|
|
const limit = entriesView.dimens.height;
|
|
|
|
let entries = [];
|
|
|
|
|
|
|
|
self.db.each(
|
|
|
|
`SELECT *
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM (
|
|
|
|
SELECT *
|
|
|
|
FROM onelinerz
|
|
|
|
ORDER BY timestamp DESC
|
|
|
|
LIMIT ${limit}
|
|
|
|
)
|
|
|
|
ORDER BY timestamp ASC;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
|
|
|
if(!err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
row.timestamp = moment(row.timestamp); // convert -> moment
|
2018-06-22 05:15:04 +00:00
|
|
|
entries.push(row);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, entriesView, entries);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function populateEntries(entriesView, entries, callback) {
|
2018-07-10 02:27:09 +00:00
|
|
|
const tsFormat = self.menuConfig.config.timestampFormat || self.client.currentTheme.helpers.getDateFormat('short');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
entriesView.setItems(entries.map( e => {
|
2018-07-10 02:27:09 +00:00
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
userId : e.user_id,
|
2018-07-10 02:27:09 +00:00
|
|
|
userName : e.user_name,
|
2018-06-23 03:26:46 +00:00
|
|
|
oneliner : e.oneliner,
|
|
|
|
ts : e.timestamp.format(tsFormat),
|
2018-07-10 02:27:09 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
entriesView.redraw();
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function finalPrep(callback) {
|
2018-07-10 02:27:09 +00:00
|
|
|
const promptView = self.viewControllers.view.getView(MciViewIds.view.addPrompt);
|
2018-06-23 03:26:46 +00:00
|
|
|
promptView.setFocusItemIndex(1); // default to NO
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayAddScreen(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function clearAndDisplayArt(callback) {
|
|
|
|
self.viewControllers.view.setFocus(false);
|
|
|
|
|
2018-07-10 02:27:09 +00:00
|
|
|
return self.prepViewControllerWithArt(
|
|
|
|
'add',
|
|
|
|
FormIds.add,
|
|
|
|
{
|
|
|
|
clearScreen : true,
|
|
|
|
trailingLF : false
|
|
|
|
},
|
|
|
|
(err, artInfo, wasCreated) => {
|
|
|
|
if(!wasCreated) {
|
|
|
|
self.viewControllers.add.setFocus(true);
|
|
|
|
self.viewControllers.add.redrawAll();
|
|
|
|
self.viewControllers.add.switchFocus(MciViewIds.add.newEntry);
|
|
|
|
}
|
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2018-07-10 02:27:09 +00:00
|
|
|
function initPreviewUpdates(callback) {
|
|
|
|
const previewView = self.viewControllers.add.getView(MciViewIds.add.entryPreview);
|
|
|
|
const entryView = self.viewControllers.add.getView(MciViewIds.add.newEntry);
|
|
|
|
if(previewView) {
|
|
|
|
let timerId;
|
|
|
|
entryView.on('key press', () => {
|
|
|
|
clearTimeout(timerId);
|
|
|
|
timerId = setTimeout( () => {
|
|
|
|
const focused = self.viewControllers.add.getFocusedView();
|
|
|
|
if(focused === entryView) {
|
|
|
|
previewView.setText(entryView.getData());
|
|
|
|
focused.setFocus(true);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-07-10 02:27:09 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAddForm() {
|
2018-07-10 02:27:09 +00:00
|
|
|
this.setViewText('add', MciViewIds.add.newEntry, '');
|
|
|
|
this.setViewText('add', MciViewIds.add.entryPreview, '');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initDatabase(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function openDatabase(callback) {
|
|
|
|
self.db = getTransactionDatabase(new sqlite3.Database(
|
|
|
|
getModDatabasePath(exports.moduleInfo),
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
},
|
|
|
|
function createTables(callback) {
|
|
|
|
self.db.run(
|
|
|
|
`CREATE TABLE IF NOT EXISTS onelinerz (
|
2018-06-23 03:26:46 +00:00
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
user_id INTEGER_NOT NULL,
|
|
|
|
user_name VARCHAR NOT NULL,
|
|
|
|
oneliner VARCHAR NOT NULL,
|
|
|
|
timestamp DATETIME NOT NULL
|
|
|
|
);`
|
2018-06-22 05:15:04 +00:00
|
|
|
,
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
storeNewOneliner(oneliner, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const self = this;
|
|
|
|
const ts = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function addRec(callback) {
|
|
|
|
self.db.run(
|
|
|
|
`INSERT INTO onelinerz (user_id, user_name, oneliner, timestamp)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?, ?, ?);`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ self.client.user.userId, self.client.user.username, oneliner, ts ],
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function removeOld(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// keep 25 max most recent items - remove the older ones
|
2018-06-22 05:15:04 +00:00
|
|
|
self.db.run(
|
|
|
|
`DELETE FROM onelinerz
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE id IN (
|
|
|
|
SELECT id
|
|
|
|
FROM onelinerz
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT -1 OFFSET 25
|
|
|
|
);`,
|
2018-06-22 05:15:04 +00:00
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeArt(cb) {
|
|
|
|
super.beforeArt(err => {
|
|
|
|
return err ? cb(err) : this.initDatabase(cb);
|
|
|
|
});
|
|
|
|
}
|
2016-06-29 04:41:50 +00:00
|
|
|
};
|