mark read

This commit is contained in:
Thomas Rudin 2018-12-12 13:28:40 +01:00
parent 70d66c876d
commit 6ef2f57a84
9 changed files with 95 additions and 10 deletions

View File

@ -28,4 +28,16 @@ function mail.save()
minetest.log("error","[mail] Save failed - messages may be lost!")
return false
end
end
end
-- save periodically
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < 30 then return end
timer=0
mail.save()
end)

View File

@ -17,6 +17,15 @@ api.deleteMail = function(index){
});
}
api.markRead = function(index){
return m.request({
method: "POST",
url: "api/markread",
data: { index: index },
headers: { "authorization": webmail.token }
});
}
api.verifyToken = function(){
return m.request({
url: "api/verify",

View File

@ -6,7 +6,7 @@
return m("div", "Loading...");
var id = m.route.param("id");
var mail = webmail.mails[id-1];
var mail = webmail.service.readMail(id);
console.log(mail);//XXX

View File

@ -20,17 +20,22 @@
timeStr = time_m.format("YYYY-MM-DD HH:mm:ss") + " (" + durationStr + ")";
}
return m("tr", [
var rowClass = vnode.attrs.row.unread ? "table-primary" : "";
return m("tr", {class: rowClass}, [
m("td", vnode.attrs.row.sender),
m("td", vnode.attrs.row.subject),
m("td", timeStr),
m("td", vnode.attrs.row.unread),
m("td", [
m("div", { class: "btn-group" }, [
m("button[type=button]", { class: "btn btn-primary", onclick: openMail }, "Open"),
m("button[type=button]", { class: "btn btn-secondary" }, "Mark unread"),
m("button[type=button]", { class: "btn btn-secondary" }, "Mark read"),
m("button[type=button]", { class: "btn btn-danger", onclick: deleteMail }, "Remove")
m("button[type=button]", { class: "btn btn-primary", onclick: openMail },[
m("i", {class:"fa fa-envelope-open"}),
"Open"
]),
m("button[type=button]", { class: "btn btn-danger", onclick: deleteMail },[
m("i", {class:"fa fa-trash"}),
"Remove"
])
])
])
]);
@ -47,7 +52,6 @@
m("th", "Sender"),
m("th", "Subject"),
m("th", "Sent"),
m("th", "Status"),
m("th", "Action")
]));

View File

@ -4,12 +4,16 @@
function NavLinks(){
return m("ul", {class:"navbar-nav"}, [
m("a", {class:"nav-link", href:"#!/login"}, "Login"),
m("a", {class:"nav-link", href:"#!/messages"}, "Messages")
m("a", {class:"nav-link", href:"#!/messages"}, [
"Messages",
m("span", {class: "badge badge-light"}, webmail.service.countUnread())
])
]);
}
function NavBarContent(){
return [
m("i", {class:"fa fa-envelope"}),
m("a", {class:"navbar-brand", href:"#"}, "Minetest webmail"),
m("div", {class:"navbar-collapse"}, NavLinks())
];

View File

@ -62,7 +62,31 @@ service.fetchMails = function(){
webmail.mails = result;
});
}
}
service.countUnread = function(){
var count = 0;
if (webmail.mails && webmail.mails.length){
webmail.mails.forEach(function(mail){
if (mail.unread)
count++;
});
}
return count;
}
service.readMail = function(index){
if (webmail.mails && webmail.mails.length){
//mark as read with api
webmail.api.markRead(index);
//mark read locally
var mail = webmail.mails[index-1];
mail.unread = false;
return mail;
}
}
service.deleteMail = function(index){

View File

@ -2,3 +2,4 @@ require("./minetest/index");
require("./login");
require("./verify");
require("./inbox");
require("./markread");

View File

@ -0,0 +1,17 @@
const app = require("../app");
const tokencheck = require("./tokencheck");
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const markmessage = require("../promise/markmessage");
app.post('/api/markread', jsonParser, function(req, res){
var payload = tokencheck(req, res);
if (payload && req.body.index) {
markmessage(payload.username, req.body.index);
}
res.status(200).end();
});

View File

@ -0,0 +1,14 @@
const events = require("../events");
module.exports = (playername, index) => new Promise(function(resolve, reject){
events.emit("channel-send", {
type: "mark-mail-read",
playername: playername,
index: +index
});
resolve();
});