Persist announcements in the database

This commit is contained in:
calzoneman 2014-02-01 12:41:06 -06:00
parent 6498f6431b
commit 2c6edb38b8
4 changed files with 63 additions and 39 deletions

View File

@ -16,20 +16,15 @@ var db = require("./database");
function handleAnnounce(user, data) {
var sv = Server.getServer();
sv.announcement = {
sv.announce({
title: data.title,
text: data.content,
from: user.name
};
sv.io.sockets.emit("announcement", sv.announcement);
if (sv.ioSecure) {
sv.ioSecure.sockets.emit("announcement", sv.announcement);
}
});
}
function handleAnnounceClear(user) {
Server.getServer().announcement = null;
Server.getServer().announce(null);
}
function handleGlobalBan(user, data) {

View File

@ -3,6 +3,7 @@ var bcrypt = require("bcrypt");
var $util = require("./utilities");
var Logger = require("./logger");
var Config = require("./config");
var Server = require("./server");
var pool = null;
var global_ipbans = {};
@ -148,7 +149,8 @@ module.exports.initGlobalTables = function () {
return;
}
require("./dbupdate").checkVersion();
require("./database/update").checkVersion();
module.exports.loadAnnouncement();
});
};
@ -670,3 +672,45 @@ module.exports.listStats = function (callback) {
};
/* END REGION */
/* Misc */
module.exports.loadAnnouncement = function () {
var query = "SELECT * FROM `meta` WHERE `key`='announcement'";
module.exports.query(query, function (err, rows) {
if (err) {
return;
}
if (rows.length === 0) {
return;
}
var announcement = rows[0].value;
try {
announcement = JSON.parse(announcement);
} catch (e) {
Logger.errlog.log("Invalid announcement data in database: " +
announcement.value);
module.exports.clearAnnouncement();
return;
}
var sv = Server.getServer();
sv.announcement = announcement;
sv.io.sockets.emit("announcement", announcement);
if (sv.ioSecure) {
sv.ioSecure.sockets.emit("announcement", announcement);
}
});
};
module.exports.setAnnouncement = function (data) {
var query = "INSERT INTO `meta` (`key`, `value`) VALUES ('announcement', ?) " +
"ON DUPLICATE KEY UPDATE `value`=?";
var repl = JSON.stringify(data);
module.exports.query(query, [repl, repl]);
};
module.exports.clearAnnouncement = function () {
module.exports.query("DELETE FROM `meta` WHERE `key`='announcement'");
};

View File

@ -1,5 +1,5 @@
var db = require("./database");
var Logger = require("./logger");
var db = require("../database");
var Logger = require("../logger");
const DB_VERSION = 1;

View File

@ -43,6 +43,7 @@ var Logger = require("./logger");
var Channel = require("./channel");
var User = require("./user");
var $util = require("./utilities");
var db = require("./database");
var Server = function () {
var self = this;
@ -70,18 +71,6 @@ var Server = function () {
"../httpaccess.log"));
self.express = express();
require("./web/webserver").init(self.express);
self.express.get("/old/:channel(*)", function (req, res, next) {
var c = req.params.channel;
if (!$util.isValidChannelName(c)) {
res.redirect("/" + c);
return;
}
self.logHTTP(req);
res.sendfile("channel.html", {
root: path.join(__dirname, "../www")
});
});
// http/https/sio server init -----------------------------------------
if (Config.get("https.enabled")) {
@ -225,22 +214,18 @@ Server.prototype.packChannel = function (c) {
return data;
};
Server.prototype.logHTTP = function (req, status) {
if (status === undefined)
status = 200;
var ip = this.getHTTPIP(req);
var url = req.url;
// Remove query
if(url.indexOf("?") != -1)
url = url.substring(0, url.lastIndexOf("?"));
this.httplog.log([
ip,
req.method,
url,
status,
req.header("user-agent")
].join(" "));
Server.prototype.announce = function (data) {
if (data == null) {
this.announcement = null;
db.clearAnnouncement();
} else {
this.announcement = data;
db.setAnnouncement(data);
this.io.sockets.emit("announcement", data);
if (this.ioSecure) {
this.ioSecure.sockets.emit("announcement", data);
}
}
};
Server.prototype.shutdown = function () {