Finish fixing api.js

This commit is contained in:
calzoneman 2013-08-16 13:19:00 -05:00
parent f46169fbe3
commit 3f26fc80e0
1 changed files with 136 additions and 89 deletions

225
api.js
View File

@ -401,17 +401,6 @@ module.exports = function (Server) {
var text = req.body.profile_text; var text = req.body.profile_text;
db.userLoginSession(name, session, function (err, row) { db.userLoginSession(name, session, function (err, row) {
var row = Auth.login(name, pw, session);
if(!row) {
res.jsonp({
success: false,
error: "Invalid login"
});
return;
}
db.setUserProfile(name, { image: img, text: text },
function (err, dbres) {
if(err) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
@ -420,21 +409,32 @@ module.exports = function (Server) {
return; return;
} }
res.jsonp({ success: true }); db.setUserProfile(name, { image: img, text: text },
name = name.toLowerCase(); function (err, dbres) {
for(var i in Server.channels) { if(err) {
var chan = Server.channels[i]; res.jsonp({
for(var j in chan.users) { success: false,
var user = chan.users[j]; error: err
if(user.name.toLowerCase() == name) { });
user.profile = { return;
image: img, }
text: text
}; res.jsonp({ success: true });
chan.broadcastUserUpdate(user); name = name.toLowerCase();
for(var i in Server.channels) {
var chan = Server.channels[i];
for(var j in chan.users) {
var user = chan.users[j];
if(user.name.toLowerCase() == name) {
user.profile = {
image: img,
text: text
};
chan.broadcastUserUpdate(user);
}
} }
} }
} });
}); });
}); });
@ -461,16 +461,7 @@ module.exports = function (Server) {
return; return;
} }
var row = Auth.login(name, pw, ""); db.userLoginPassword(name, pw, function (err, row) {
if(!row) {
res.jsonp({
success: false,
error: "Invalid login credentials"
});
return;
}
db.setUserEmail(name, email, function (err, dbres) {
if(err) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
@ -479,10 +470,20 @@ module.exports = function (Server) {
return; return;
} }
ActionLog.record(getIP(req), name, "email-update", email); db.setUserEmail(name, email, function (err, dbres) {
res.jsonp({ if(err) {
success: true, res.jsonp({
session: row.session_hash success: false,
error: err
});
return;
}
ActionLog.record(getIP(req), name, "email-update", email);
res.jsonp({
success: true,
session: row.session_hash
});
}); });
}); });
}); });
@ -493,29 +494,31 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row) {
res.jsonp({
success: false,
error: "Invalid login"
});
return;
}
db.listUserChannels(name, function (err, res) {
if(err) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
channels: [] error: err
}); });
return; return;
} }
res.jsonp({ db.listUserChannels(name, function (err, res) {
success: true, if(err) {
channels: res res.jsonp({
success: false,
channels: []
});
return;
}
res.jsonp({
success: true,
channels: res
});
}); });
}); });
}); });
/* END REGION */ /* END REGION */
@ -529,15 +532,26 @@ module.exports = function (Server) {
var session = req.query.session; var session = req.query.session;
var types = req.query.actions; var types = req.query.actions;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
res.send(403); if(err !== "Invalid session" &&
return; err !== "Session expired") {
} res.send(500);
} else {
res.send(403);
}
return;
}
types = types.split(","); if(row.global_rank < 255) {
var actions = ActionLog.readLog(types); res.send(403);
res.jsonp(actions); return;
}
types = types.split(",");
var actions = ActionLog.readLog(types);
res.jsonp(actions);
});
}); });
/* helper function to pipe the last N bytes of a file */ /* helper function to pipe the last N bytes of a file */
@ -564,13 +578,24 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
res.send(403); if(err !== "Invalid session" &&
return; err !== "Session expired") {
} res.send(500);
} else {
res.send(403);
}
return;
}
pipeLast(res, "sys.log", 1048576); if(row.global_rank < 255) {
res.send(403);
return;
}
pipeLast(res, "sys.log", 1048576);
});
}); });
app.get("/api/logging/errorlog", function (req, res) { app.get("/api/logging/errorlog", function (req, res) {
@ -580,13 +605,24 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
res.send(403); if(err !== "Invalid session" &&
return; err !== "Session expired") {
} res.send(500);
} else {
res.send(403);
}
return;
}
pipeLast(res, "error.log", 1048576); if(row.global_rank < 255) {
res.send(403);
return;
}
pipeLast(res, "error.log", 1048576);
});
}); });
app.get("/api/logging/channels/:channel", function (req, res) { app.get("/api/logging/channels/:channel", function (req, res) {
@ -596,24 +632,35 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
res.send(403); if(err !== "Invalid session" &&
return; err !== "Session expired") {
} res.send(500);
} else {
var chan = req.params.channel || ""; res.send(403);
if(!chan.match(/^[\w-_]+$/)) { }
res.send(400); return;
return;
}
fs.exists("chanlogs/" + chan + ".log", function(exists) {
if(exists) {
pipeLast(res, "chanlogs/" + chan + ".log", 1048576);
} else {
res.send(404);
} }
if(row.global_rank < 255) {
res.send(403);
return;
}
var chan = req.params.channel || "";
if(!$util.isValidChannelName(chan)) {
res.send(400);
return;
}
fs.exists("chanlogs/" + chan + ".log", function(exists) {
if(exists) {
pipeLast(res, "chanlogs/" + chan + ".log", 1048576);
} else {
res.send(404);
}
});
}); });
}); });