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) { function handleAnnounce(user, data) {
var sv = Server.getServer(); var sv = Server.getServer();
sv.announcement = { sv.announce({
title: data.title, title: data.title,
text: data.content, text: data.content,
from: user.name from: user.name
}; });
sv.io.sockets.emit("announcement", sv.announcement);
if (sv.ioSecure) {
sv.ioSecure.sockets.emit("announcement", sv.announcement);
}
} }
function handleAnnounceClear(user) { function handleAnnounceClear(user) {
Server.getServer().announcement = null; Server.getServer().announce(null);
} }
function handleGlobalBan(user, data) { function handleGlobalBan(user, data) {

View File

@ -3,6 +3,7 @@ var bcrypt = require("bcrypt");
var $util = require("./utilities"); var $util = require("./utilities");
var Logger = require("./logger"); var Logger = require("./logger");
var Config = require("./config"); var Config = require("./config");
var Server = require("./server");
var pool = null; var pool = null;
var global_ipbans = {}; var global_ipbans = {};
@ -148,7 +149,8 @@ module.exports.initGlobalTables = function () {
return; return;
} }
require("./dbupdate").checkVersion(); require("./database/update").checkVersion();
module.exports.loadAnnouncement();
}); });
}; };
@ -670,3 +672,45 @@ module.exports.listStats = function (callback) {
}; };
/* END REGION */ /* 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 db = require("../database");
var Logger = require("./logger"); var Logger = require("../logger");
const DB_VERSION = 1; const DB_VERSION = 1;

View File

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