fix some anti pattern stuff
This commit is contained in:
+6
-6
@@ -10,7 +10,7 @@ export const setup = function (router, con, server) {
|
||||
router.use("/*", (req, res, next) => {
|
||||
res.set("Access-Control-Allow-Origin", "*"); //we'll allow it for now
|
||||
let unsigned;
|
||||
if (req.body.user == undefined || req.body.pass == undefined) {
|
||||
if (req.body.user === undefined || req.body.pass === undefined) {
|
||||
if(typeof req.get("ipost-auth-token") === "string") {
|
||||
try{
|
||||
req.body.auth = JSON.parse(req.get("ipost-auth-token"))
|
||||
@@ -18,7 +18,7 @@ export const setup = function (router, con, server) {
|
||||
console.log("error parsing header",err)
|
||||
}
|
||||
}
|
||||
if(req.body.auth !== undefined && req.originalUrl!=="/redeemauthcode") {
|
||||
if(req.body.auth !== undefined && req.originalUrl !== "/redeemauthcode") {
|
||||
if(typeof req.body.auth === "string") {
|
||||
try{
|
||||
req.body.auth = JSON.parse(req.body.auth)
|
||||
@@ -45,7 +45,7 @@ export const setup = function (router, con, server) {
|
||||
con.query(sql,[SHA256(req.body.auth.auth_token,req.body.auth.appid, HASHES_DB),SHA256(req.body.auth.secret,req.body.auth.appid, HASHES_DB),req.body.auth.appid],(err,result) => {
|
||||
if(err) throw err;
|
||||
|
||||
if(result.length != 1) {
|
||||
if(result.length !== 1) {
|
||||
res.status(420).send("invalid authentication object (or server error?)")
|
||||
return;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ export const setup = function (router, con, server) {
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_Name && result[0].User_Name == values[0]) {
|
||||
if (result[0] && result[0].User_Name && result[0].User_Name === values[0]) {
|
||||
|
||||
res.locals.userid = result[0].User_ID;
|
||||
res.locals.username = result[0].User_Name;
|
||||
@@ -105,13 +105,13 @@ export const setup = function (router, con, server) {
|
||||
|
||||
router.use("/api/*", (req, res, next) => {
|
||||
res.set("Access-Control-Allow-Origin", "*"); //we'll allow it for now
|
||||
if (config["allow_getotheruser_without_cookie"] && req.originalUrl.split("\?")[0] == "/api/getotheruser") {
|
||||
if (config["allow_getotheruser_without_cookie"] && req.originalUrl.split("\?")[0] === "/api/getotheruser") {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
if (!server.increaseAPICall(req, res))return;
|
||||
|
||||
if (res.locals.username != undefined) {
|
||||
if (res.locals.username !== undefined) {
|
||||
next();
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -4,7 +4,7 @@ export const setup = function (router, con, server) {
|
||||
router.get("/api/getPersonalPosts", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", "");
|
||||
let otherperson = encodeURIComponent(req.query.otherperson || "");
|
||||
if (typeof otherperson != "string" || otherperson.length > 100 || otherperson == "") {
|
||||
if (typeof otherperson !== "string" || otherperson.length > 100 || otherperson === "") {
|
||||
res.status(410).json({ "error": "invalid otherperson given" });
|
||||
return;
|
||||
}
|
||||
@@ -23,11 +23,8 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.get("/api/dms/conversations", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", "*");
|
||||
const columns = [
|
||||
"dms_user_name", "dms_receiver"
|
||||
];
|
||||
let uriencusername = encodeURIComponent(res.locals.username);
|
||||
let sql = `select ${columns.join(",")} from ipost.dms where ((dms_receiver = ?) or (dms_user_name = ?)) group by dms_receiver,dms_user_name;`;
|
||||
let sql = `select dms_user_name, dms_receiver from ipost.dms where ((dms_receiver = ?) or (dms_user_name = ?)) group by dms_receiver,dms_user_name;`;
|
||||
con.query(sql, [uriencusername, uriencusername], function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
+7
-25
@@ -4,7 +4,7 @@ export const setup = function (router, con, server) {
|
||||
|
||||
function createPID(){
|
||||
let pid = server.genstring(10); //collision chance is low enough, but we'll check anyways
|
||||
while (PIDS[pid] != undefined) {
|
||||
while (PIDS[pid] !== undefined) {
|
||||
pid = server.genstring(10);
|
||||
console.log(5, "pid collision");
|
||||
}
|
||||
@@ -26,17 +26,17 @@ export const setup = function (router, con, server) {
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.message) != "string") {
|
||||
if ((typeof req.body.message) !== "string") {
|
||||
res.status(411)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.pid) != "string") {
|
||||
if ((typeof req.body.pid) !== "string") {
|
||||
res.status(412)
|
||||
res.json({ "error": "no pid given" });
|
||||
return;
|
||||
}
|
||||
if (req.body.pid.length != 10 || PIDS[req.body.pid] !== true) {
|
||||
if (req.body.pid.length !== 10 || PIDS[req.body.pid] !== true) {
|
||||
res.status(413)
|
||||
res.json({ "error": "invalid pid given" });
|
||||
return;
|
||||
@@ -49,7 +49,7 @@ export const setup = function (router, con, server) {
|
||||
else {
|
||||
reply_id = req.body.reply_id;
|
||||
}
|
||||
if ((typeof reply_id) != "number") {
|
||||
if ((typeof reply_id) !== "number") {
|
||||
res.status(414)
|
||||
res.json({ "error": "no valid reply id given" });
|
||||
return;
|
||||
@@ -66,7 +66,7 @@ export const setup = function (router, con, server) {
|
||||
return;
|
||||
}
|
||||
req.body.receiver = encodeURIComponent(req.body.receiver || "");
|
||||
if (req.body.receiver == "" || req.body.receiver == encodeURIComponent(res.locals.username) || req.body.receiver.length > 100) {
|
||||
if (req.body.receiver === "" || req.body.receiver === encodeURIComponent(res.locals.username) || req.body.receiver.length > 100) {
|
||||
res.status(417).json({ "error": "invalid receiver given" });
|
||||
return;
|
||||
}
|
||||
@@ -85,28 +85,10 @@ export const setup = function (router, con, server) {
|
||||
console.error(err)
|
||||
return;
|
||||
}
|
||||
// let post_obj = {
|
||||
// post_user_name: encodeURIComponent(res.locals.username),
|
||||
// post_text: req.body.message,
|
||||
// post_time: Date.now(),
|
||||
// post_special_text: "",
|
||||
// post_receiver_name: req.body.receiver,
|
||||
// post_from_bot: res.locals.isbot,
|
||||
// post_reply_id: reply_id
|
||||
// }
|
||||
// let message = {
|
||||
// message: "new_post",
|
||||
// data: post_obj
|
||||
// }
|
||||
// let messagestr = JSON.stringify(message)
|
||||
// server.wss.clients.forEach(function(ws) {
|
||||
// if(ws.channel == decodeURIComponent(req.body.receiver)) {
|
||||
// ws.send(messagestr)
|
||||
// }
|
||||
// });
|
||||
res.json({ "success": "successfully posted dm" });
|
||||
console.log(5, `posted new dm by ${res.locals.username} to ${otherperson} : ${xor(encodeURIComponent(res.locals.username), otherperson)}`);
|
||||
});
|
||||
//TODO: bring dms up-to-date with normal posts
|
||||
});
|
||||
return createPID
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.get("/api/getPosts", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", "*");
|
||||
if (req.query.channel != undefined) {
|
||||
if (req.query.channel !== undefined) {
|
||||
let sql = `select post_user_name,post_text,post_time,post_special_text,post_id,post_from_bot,post_reply_id,User_Avatar,file_0,file_1,file_2,file_3,file_4 from ipost.posts inner join ipost.users on (User_Name = post_user_name) where post_receiver_name = ? group by post_id order by post_id desc limit 30;`;
|
||||
con.query(sql, [encodeURIComponent(req.query.channel)], function (err, result) {
|
||||
if (err)
|
||||
@@ -24,7 +24,7 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.get("/api/getPostsLowerThan", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", "*");
|
||||
if (req.query.channel != undefined) {
|
||||
if (req.query.channel !== undefined) {
|
||||
let sql = `select post_user_name,post_text,post_time,post_special_text,post_id,post_from_bot,post_reply_id,file_0,file_1,file_2,file_3,file_4 from ipost.posts where ((post_receiver_name = ?) and (post_id < ?)) group by post_id order by post_id desc limit 30;`;
|
||||
con.query(sql, [encodeURIComponent(req.query.channel), req.query.id], function (err, result) {
|
||||
if (err)
|
||||
|
||||
+2
-2
@@ -18,7 +18,7 @@ export const setup = function (router, con, server) {
|
||||
|
||||
function createPID(){
|
||||
let pid = server.genstring(10); //collision chance is low enough, but we'll check anyways
|
||||
while (PIDS[pid] != undefined) {
|
||||
while (PIDS[pid] !== undefined) {
|
||||
pid = server.genstring(10);
|
||||
console.log(5, "pid collision");
|
||||
}
|
||||
@@ -110,7 +110,7 @@ export const setup = function (router, con, server) {
|
||||
|
||||
function validateReceiver(rec) {
|
||||
let receiver = encodeURIComponent(rec || "");
|
||||
if (receiver == "")
|
||||
if (receiver === "")
|
||||
receiver = "everyone";
|
||||
return receiver
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ export const setup = function (router, con, server) {
|
||||
res.set("Access-Control-Allow-Origin", "");
|
||||
let type = req.query.type;
|
||||
let arg = encodeURIComponent(req.query.selector);
|
||||
if (type == "user") {
|
||||
if (type === "user") {
|
||||
let sql = `select User_Name,User_Bio,User_Avatar from ipost.users where User_Name like ? limit 10;`;
|
||||
con.query(sql, [`%${arg}%`], function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_Name) {
|
||||
if (result[0]) {
|
||||
result["message"] = "search has been deprecated as of 11/30/2022"
|
||||
res.json(result);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ export const setup = function (router, con, server) {
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (type == "post") {
|
||||
else if (type === "post") {
|
||||
let sql = `select post_user_name,post_text,post_time,post_special_text,post_id from ipost.posts where post_text like ? and (post_receiver_name is null or post_receiver_name = 'everyone') order by post_id desc limit 20;`;
|
||||
con.query(sql, [`%${arg}%`], function (err, result) {
|
||||
if (err)
|
||||
|
||||
@@ -11,7 +11,7 @@ export const setup = function (router, con, server) {
|
||||
res.json({ "error": "no setting to change" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.setting) != "string") {
|
||||
if ((typeof req.body.setting) !== "string") {
|
||||
res.status(411)
|
||||
res.json({ "error": "no setting to change" });
|
||||
return;
|
||||
@@ -20,7 +20,7 @@ export const setup = function (router, con, server) {
|
||||
let allowed = false;
|
||||
let got = typeof req.body.value;
|
||||
for (let index = 0; index < types.length; index++) {
|
||||
if (types[index] == got) {
|
||||
if (types[index] === got) {
|
||||
allowed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
+13
-13
@@ -29,7 +29,7 @@ export const setup = function (router, con, server) {
|
||||
} catch(ignored){}
|
||||
}
|
||||
let filename = genstring(95) + ".webp";
|
||||
while (existsSync(avatars + "/" + filename) || filename == ".webp") { //generate new filename until it's unique
|
||||
while (existsSync(avatars + "/" + filename) || filename === ".webp") { //generate new filename until it's unique
|
||||
filename = genstring(95) + ".webp";
|
||||
}
|
||||
sharp(avatar.data).resize({ //resize avatar to 100x100 and convert it to a webp, then store it
|
||||
@@ -54,7 +54,7 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.get("/api/getalluserinformation", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", ""); //we don't want that here
|
||||
let unsigned = getunsigned(req, res);
|
||||
let unsigned = getunsigned(req, res); //has to be asking for it via the cookie
|
||||
if (!unsigned)
|
||||
return;
|
||||
unsigned = decodeURIComponent(unsigned);
|
||||
@@ -64,7 +64,7 @@ export const setup = function (router, con, server) {
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_Name && result[0].User_Name == values[0]) {
|
||||
if (result[0]) {
|
||||
res.status(200);
|
||||
res.json(result[0]);
|
||||
}
|
||||
@@ -81,7 +81,7 @@ export const setup = function (router, con, server) {
|
||||
con.query(sql, [username], function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_Name && result[0].User_Name == username) {
|
||||
if (result[0]) {
|
||||
res.json({ "username": username, "bio": result[0].User_Bio, "avatar": result[0].User_Avatar, "publicKey": result[0].User_PublicKey });
|
||||
}
|
||||
else {
|
||||
@@ -112,11 +112,11 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.post("/api/changePW", (req, res) => {
|
||||
res.set("Access-Control-Allow-Origin", "");
|
||||
if ((typeof req.body.newPW) != "string") {
|
||||
if ((typeof req.body.newPW) !== "string") {
|
||||
res.json({ "error": "incorrect password" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.currentPW) != "string") {
|
||||
if ((typeof req.body.currentPW) !== "string") {
|
||||
res.json({ "error": "incorrect password" });
|
||||
return;
|
||||
}
|
||||
@@ -132,7 +132,7 @@ export const setup = function (router, con, server) {
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_Name && result[0].User_Name == res.locals.username) {
|
||||
if (result[0]) {
|
||||
let sql = `update ipost.users set User_PW=? where User_Name=? and User_PW=?;`;
|
||||
let values = [hashed_new_pw, res.locals.username, hashed_pw];
|
||||
con.query(sql, values, (err2) => {
|
||||
@@ -152,12 +152,12 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.post("/api/changeUsername", function (req, res) {
|
||||
res.set("Access-Control-Allow-Origin", "");
|
||||
if ((typeof req.body.newUsername) != "string") {
|
||||
if ((typeof req.body.newUsername) !== "string") {
|
||||
res.status(410);
|
||||
res.json({ "error": "incorrect username" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.currentPW) != "string") {
|
||||
if ((typeof req.body.currentPW) !== "string") {
|
||||
res.status(411);
|
||||
res.json({ "error": "incorrect password" });
|
||||
return;
|
||||
@@ -167,19 +167,19 @@ export const setup = function (router, con, server) {
|
||||
res.json({ "error": "username is too long" });
|
||||
return;
|
||||
}
|
||||
if (req.body.newUsername == res.locals.username) {
|
||||
if (req.body.newUsername === res.locals.username) {
|
||||
res.status(413);
|
||||
res.json({ "error": "username can't be the current one" });
|
||||
return;
|
||||
}
|
||||
let hashed_pw = SHA256(req.body.currentPW, res.locals.username, HASHES_DB);
|
||||
let hashed_new_pw = SHA256(req.body.currentPW, req.body.newUsername, HASHES_DB);
|
||||
let sql = `select * from ipost.users where User_Name=?;`; //check if pw is correct
|
||||
let values = [res.locals.username];
|
||||
let sql = `select * from ipost.users where User_Name=? and User_PW=?;`; //check if pw is correct
|
||||
let values = [res.locals.username,hashed_pw];
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (result[0] && result[0].User_PW == hashed_pw) {
|
||||
if (result[0]) {
|
||||
let sql = `select * from ipost.users where User_Name=?;`; //check if newUsername isn't already used
|
||||
let values = [req.body.newUsername];
|
||||
con.query(sql, values, function (err, result) {
|
||||
|
||||
+7
-7
@@ -19,12 +19,12 @@ export const setup = function (router, con, server) {
|
||||
return;
|
||||
}
|
||||
res.status(200);
|
||||
if ((typeof req.body.user) != "string") {
|
||||
if ((typeof req.body.user) !== "string") {
|
||||
res.status(416);
|
||||
res.json({ "error": "incorrect username" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.pass) != "string") {
|
||||
if ((typeof req.body.pass) !== "string") {
|
||||
res.status(417);
|
||||
res.json({ "error": "incorrect password" });
|
||||
return;
|
||||
@@ -37,7 +37,7 @@ export const setup = function (router, con, server) {
|
||||
res.redirect("/register?success=false&reason=username");
|
||||
return;
|
||||
}
|
||||
if (username == "") {
|
||||
if (username === "") {
|
||||
res.status(411);
|
||||
res.redirect("/register?success=false&reason=username");
|
||||
return;
|
||||
@@ -52,7 +52,7 @@ export const setup = function (router, con, server) {
|
||||
res.send("username is too long");
|
||||
return;
|
||||
}
|
||||
if (username.search("@") != -1) {
|
||||
if (username.search("@") !== -1) {
|
||||
res.status(414);
|
||||
res.send("username can't contain @-characters");
|
||||
return;
|
||||
@@ -93,12 +93,12 @@ export const setup = function (router, con, server) {
|
||||
router.post("/login", function (req, res) {
|
||||
if (!increaseAPICall(req, res))
|
||||
return;
|
||||
if ((typeof req.body.user) != "string") {
|
||||
if ((typeof req.body.user) !== "string") {
|
||||
res.status(416);
|
||||
res.json({ "error": "incorrect username" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.pass) != "string") {
|
||||
if ((typeof req.body.pass) !== "string") {
|
||||
res.status(417);
|
||||
res.json({ "error": "incorrect password" });
|
||||
return;
|
||||
@@ -150,7 +150,7 @@ export const setup = function (router, con, server) {
|
||||
let cookiesigned = signature.sign(setTo, cookiesecret + (!no_ip_lock ? ip : ""));
|
||||
res.cookie('AUTH_COOKIE', cookiesigned, { maxAge: Math.pow(10, 10), httpOnly: true, secure: DID_I_FINALLY_ADD_HTTPS });
|
||||
ip = SHA256(ip, setTo, HASHES_DB);
|
||||
if (result[0].User_LastIP != ip) {
|
||||
if (result[0].User_LastIP !== ip) {
|
||||
let sql = `update ipost.users set User_LastIP = ? where User_Name = ?;`;
|
||||
con.query(sql, [ip, encodeURIComponent(username)], function (error) {
|
||||
if (error)
|
||||
|
||||
+4
-4
@@ -57,7 +57,7 @@ export const setup = function (router, con, server) {
|
||||
let out = []
|
||||
|
||||
for(let channel of result){
|
||||
if(channel.post_receiver_name == "")continue;
|
||||
if(channel.post_receiver_name === "")continue;
|
||||
out[out.length] = channel.post_receiver_name
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ export const setup = function (router, con, server) {
|
||||
|
||||
async function handleUserFiles(request, response, overrideurl) {
|
||||
if (!increaseUSERCall(request, response))return;
|
||||
if(typeof overrideurl != "string")overrideurl = undefined;
|
||||
if(typeof overrideurl !== "string")overrideurl = undefined;
|
||||
|
||||
let originalUrl = overrideurl || request.originalUrl.split("?").shift();
|
||||
|
||||
@@ -177,13 +177,13 @@ export const setup = function (router, con, server) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(originalUrl == "/favicon.ico") {
|
||||
if(originalUrl === "/favicon.ico") {
|
||||
response.set('Cache-Control', 'public, max-age=2592000');
|
||||
response.sendFile(dir + "/views/favicon.ico")
|
||||
return
|
||||
}
|
||||
|
||||
if(originalUrl == "/api/documentation/") {
|
||||
if(originalUrl === "/api/documentation/") {
|
||||
readFile(path,function(_err,res){
|
||||
response.send(res.toString())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user