From 1fb7b6e3309bef7e711145555122aa436ae41508 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 8 Nov 2015 05:02:39 +1100 Subject: [PATCH] Fix import/export for the new headless mode (Electron), which disallows synchronous XHR. --- source/plugins/export.js | 45 ++++++++++++++++++++++++---------------- source/plugins/import.js | 45 ++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/source/plugins/export.js b/source/plugins/export.js index ef94c78..9b0c940 100644 --- a/source/plugins/export.js +++ b/source/plugins/export.js @@ -1,31 +1,40 @@ (function() { "use strict"; - bot.addCommand({ - name : 'export', - fun : function(args) { - var req = new XMLHttpRequest(); - req.open('POST', 'https://api.github.com/gists', false); - req.send(JSON.stringify({ - files: { - 'bot.json': { - content: JSON.stringify(bot.memory.data) - } - } - })); - + var doExport = function(args) { + var req = new XMLHttpRequest(); + req.addEventListener('abort', function() { + args.reply('Failed: Gist request aborted by user (???)'); + }); + req.addEventListener('error', function() { + args.reply('Failed: Gist request encountered a network error'); + }); + req.addEventListener('load', function() { if (req.status !== 201) { var resp = ''; if (req.responseText) { resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n'); } - return 'Failed: ' + req.status + ': ' + req.statusText + resp; + args.reply('Failed: ' + req.status + ': ' + req.statusText + resp); } - + var resp = JSON.parse(req.responseText); - - return 'Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url; - }, + + args.reply('Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url); + }); + req.open('POST', 'https://api.github.com/gists', true); + req.send(JSON.stringify({ + files: { + 'bot.json': { + content: JSON.stringify(bot.memory.data) + } + } + })); + }; + + bot.addCommand({ + name : 'export', + fun : doExport, permissions : { del : 'NONE', use : 'OWNER' }, description : 'Blurts out a message with the persistent memory storage for export `/export`' }); diff --git a/source/plugins/import.js b/source/plugins/import.js index d254665..5904c74 100644 --- a/source/plugins/import.js +++ b/source/plugins/import.js @@ -1,34 +1,43 @@ (function() { "use strict"; + + var doImport = function (args) { + if (args.trim() === 'clear') { + bot.memory.clear(); - bot.addCommand({ - name : 'import', - fun : function (args) { - if (args.trim() === 'clear') { - bot.memory.clear(); - - return 'Bot memory cleared. Please restart the bot.'; - } - - var req = new XMLHttpRequest(); - req.open('GET', 'https://api.github.com/gists/' + args, false); - req.send(null); - + return 'Bot memory cleared. Please restart the bot.'; + } + + var req = new XMLHttpRequest(); + req.addEventListener('abort', function() { + args.reply('Failed: Gist request aborted by user (???)'); + }); + req.addEventListener('error', function() { + args.reply('Failed: Gist request encountered a network error'); + }); + req.addEventListener('load', function() { if (req.status !== 200) { var resp = ''; if (req.responseText) { resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n'); } - return 'Failed: ' + req.status + ': ' + req.statusText + resp; + args.reply('Failed: ' + req.status + ': ' + req.statusText + resp); } - + var resp = JSON.parse(req.responseText); - + bot.memory.data = JSON.parse(resp.files['bot.json'].content); bot.memory.save(); + + args.reply("Imported and persisted successfully. Please restart the bot."); + }); + req.open('GET', 'https://api.github.com/gists/' + args, true); + req.send(null); + }; - return "Imported and persisted successfully. Please restart the bot."; - }, + bot.addCommand({ + name : 'import', + fun : doImport, permissions : { del : 'NONE', use : 'OWNER' }, description : 'Imports the persistent memory described in args `/export `' });