From ef6a9af4471ed91c1b621995adefc74d9b000e7b Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Thu, 6 Dec 2018 09:44:42 +0100 Subject: [PATCH] mail request stub --- webmail.lua | 14 +++++++++++++- webmail/src/api/inbox.js | 16 ++++++---------- webmail/src/promise/login.js | 2 +- webmail/src/promise/playermessages.js | 26 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 webmail/src/promise/playermessages.js diff --git a/webmail.lua b/webmail.lua index 9f938df..85518bc 100644 --- a/webmail.lua +++ b/webmail.lua @@ -5,7 +5,7 @@ local MP = minetest.get_modpath(minetest.get_current_modname()) local Channel = dofile(MP .. "/util/channel.lua") local channel - +-- auth request from webmail local auth_handler = function(auth) local handler = minetest.get_auth_handler() @@ -24,12 +24,22 @@ local auth_handler = function(auth) }) end +-- send request from webmail local send_handler = function(sendmail) -- send mail from webclient minetest.log("action", "[webmail] sending mail from webclient: " .. sendmail.src .. " -> " .. sendmail.dst) mail.send(sendmail.src, sendmail.dst, sendmail.subject, sendmail.body) end +-- get player messages request from webmail +local get_player_messages_handler = function(playername) + channel.send({ + type = "player-messages", + playername = playername, + data = mail.messages[playername] + }) +end + -- called on mail saving to disk (every change) mail.webmail_save_hook = function() channel.send({ @@ -49,6 +59,8 @@ mail.webmail_init = function(http, url, key) auth_handler(data.data) elseif data.type == "send" then send_handler(data.data) + elseif data.type == "player-messages" then + get_player_messages_handler(data.data) end end) end diff --git a/webmail/src/api/inbox.js b/webmail/src/api/inbox.js index b2456f3..414feb2 100644 --- a/webmail/src/api/inbox.js +++ b/webmail/src/api/inbox.js @@ -1,20 +1,16 @@ const app = require("../app"); -const store = require("../store"); -const bodyParser = require('body-parser') -const jsonParser = bodyParser.json() +const bodyParser = require('body-parser'); +const jsonParser = bodyParser.json(); +const playermessages = require("../promise/playermessages"); app.get('/api/inbox/:name', function(req, res){ var name = req.params.name; - console.log(req.params, store); - - if (name && store.messages[name]) - res.json(store.messages[name]); - else - res.end(); - + playermessages(name) + .then(list => res.json(list)) + .catch(e => res.status(500).end); }); diff --git a/webmail/src/promise/login.js b/webmail/src/promise/login.js index 0acbcea..d44df93 100644 --- a/webmail/src/promise/login.js +++ b/webmail/src/promise/login.js @@ -25,5 +25,5 @@ module.exports = (username, password) => new Promise(function(resolve, reject){ var handle = setTimeout(function(){ events.removeListener("channel-recv", handleEvent); reject("mod-comm timeout"); - }, 2500); + }, 1000); }); \ No newline at end of file diff --git a/webmail/src/promise/playermessages.js b/webmail/src/promise/playermessages.js new file mode 100644 index 0000000..085f250 --- /dev/null +++ b/webmail/src/promise/playermessages.js @@ -0,0 +1,26 @@ + + +const events = require("../events"); + +module.exports = (playername) => new Promise(function(resolve, reject){ + + events.emit("channel-send", { + type: "player-messages", + data: playername + }); + + function handleEvent(result){ + if (result.type == "player-messages" && result.playername == playername){ + events.removeListener("channel-recv", handleEvent); + clearTimeout(handle); + resolve(result.data); + } + } + + events.on("channel-recv", handleEvent); + + var handle = setTimeout(function(){ + events.removeListener("channel-recv", handleEvent); + reject("mod-comm timeout"); + }, 1000); +}); \ No newline at end of file