diff --git a/calEvent.html b/calEvent.html index 2b1b137..b1d30f6 100644 --- a/calEvent.html +++ b/calEvent.html @@ -230,9 +230,12 @@ xhrsess.onloadend = (e) => { const sessionData = JSON.parse(xhrsess.response); - const userId = JSON.parse(sessionData.userId); + var userId = JSON.parse(sessionData.userId); const delObjKeys = sessionStorage.getItem('delObjKeys'); - + if (sessionStorage.getItem('ref') == 'guild') { + userId = serverSettings.Id; + } + //Send the data to the server var xhr = new XMLHttpRequest(); xhr.open('post', 'http://www.selmerbot.com/sendData/', true); diff --git a/calendar.html b/calendar.html index 02ffccb..352c77b 100644 --- a/calendar.html +++ b/calendar.html @@ -283,6 +283,7 @@ w.sessionStorage.setItem("evlist", evjs); w.sessionStorage.setItem('day', day); + w.sessionStorage.setItem('ref', 'user'); w.location.reload(); w.document.write('Editing ${(new Date(Number(ev.time)))}'); @@ -296,6 +297,7 @@ if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); } w.sessionStorage.setItem("day", day); + w.sessionStorage.setItem("ref", 'user'); w.location.reload(); // w.document.write('Editing ${(' + String(day) + ')}'); diff --git a/dashboard.html b/dashboard.html index 5b2e9c2..b16b423 100644 --- a/dashboard.html +++ b/dashboard.html @@ -279,6 +279,7 @@ w.sessionStorage.setItem("evlist", evjs); w.sessionStorage.setItem('day', day); + w.sessionStorage.setItem('ref', 'guild'); w.location.reload(); w.document.write('Editing ${(new Date(Number(ev.time)))}'); @@ -294,6 +295,7 @@ if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); } w.sessionStorage.setItem("day", day); + w.sessionStorage.setItem('ref', 'guild'); w.location.reload(); // w.document.write('Editing ${(' + String(day) + ')}'); @@ -472,12 +474,12 @@ xhr2.open('get', 'http://www.selmerbot.com/getCal/', true); xhr2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); - xhr2.setRequestHeader('userId', id); + xhr2.setRequestHeader('guildId', id); xhr2.onloadend = (e) => { const calList = new Map(); - const res = JSON.parse(xhr2.response); + const res = JSON.parse(xhr2.response); console.log(res, typeof res); const times = res[0]; const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; document.getElementById('monthName').innerText = `Editing Reminders for the Month of ${monthNames[(new Date()).getMonth()]}`; diff --git a/main.js b/main.js index b169d24..34b00d3 100644 --- a/main.js +++ b/main.js @@ -153,24 +153,30 @@ app.get('/getChannels', async (req, res) => { app.get('/getCal', async (req, res) => { - const userId = req.headers.userid; + const userId = req.headers.userid || false; + const guildId = req.headers.guildid || false; connection.then((client) => { var times; const dbo = client.db('main').collection('reminderKeys'); - dbo.findOne({ userId: userId }).then((doc) => { + dbo.findOne({$or: [ {userId: userId}, {userId: guildId} ]}).then((doc) => { if (!doc) { return res.send([[], []]); } times = doc.times; let tbo = client.db('main').collection('reminders'); - tbo.find({ $where: function() { return (times.indexOf(this.time) != -1 && this.userId == userId); }}).toArray((err, docs) => { + // { $where: function() { return (times.indexOf(this.time) != -1 && this.userId == userId); }} + // tbo.find({time: {$in: times}}).toArray((err, docs) => { console.log("A", docs); throw 1; }); + + tbo.find({time: {$in: times}}).toArray((err, docs) => { + //There's gotta be a better way let newdoc = []; for (let i = 0; i < docs.length; i ++) { newdoc.push({}); for (let j in docs[i]) { - if (docs[i][j].userId == userId) { + if (!isNaN(j) && (docs[i][j].userId == userId || docs[i][j].guildId == guildId)) { + // console.log(`${docs[i][j].userId} == ${userId}`, `${docs[i][j].guildId} == ${guildId}`); newdoc[i][j] = docs[i][j]; } } @@ -182,7 +188,6 @@ app.get('/getCal', async (req, res) => { //If there's nothing on that date, skip // if (newdoc[i].amt == 0) { console.log(newdoc[i]); } } - res.send(JSON.stringify([times, newdoc])); }); }); @@ -260,12 +265,19 @@ app.post('/sendData', async (req, res) => { // }); return console.log(obj.time); doc.amt --; - - kbo.findOne({ 'userId': doc[obj.eventInd].userId }).then((kdoc) => { + // console.log(doc[obj.eventInd]); return; + var searchId; + if (doc[obj.eventInd].userId != null) { + searchId = doc[obj.eventInd].userId; + } else if (doc[obj.eventInd].guildId != null) { + searchId = doc[obj.eventInd].guildId; + } + + kbo.findOne({ 'userId': searchId }).then((kdoc) => { if ((kdoc.times.length - 1) > 0) { - kbo.updateOne({ 'userId': doc[obj.eventInd].userId }, {$pull: { times: obj.time }}); + kbo.updateOne({ 'userId': searchId }, {$pull: { times: obj.time }}); } else { - kbo.deleteOne({ 'userId': doc[obj.eventInd].userId }); + kbo.deleteOne({ 'userId': searchId }); } }); @@ -314,25 +326,30 @@ app.post('/sendData', async (req, res) => { //Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" } } app.post('/newCalEvent', async (req, res) => { if (req.headers.newcalevent) { - try { const obj = JSON.parse(req.headers.newcalevent); // console.log(obj.time, typeof obj.time); return; + var searchId; + if (obj.event.userId != null) { + searchId = obj.event.userId; + } else if (obj.event.guildId != null) { + searchId = obj.event.guildId; + } else { return res.sendStatus(400); } connection.then((client) => { // Update the Key object first to check if the time is already there const kbo = client.db('main').collection('reminderKeys'); - kbo.findOne(({ 'userId': obj.event.userId })).then((doc) => { + kbo.findOne({ 'userId': searchId }).then((doc) => { if (doc) { if (doc.times.indexOf(obj.time) == -1) { - kbo.updateOne({ 'userId': obj.event.userId }, { $push: { times: obj.time } }) + kbo.updateOne({ 'userId': searchId }, { $push: { times: obj.time } }) .catch((err) => { console.error(err); res.sendStatus(500); }); } else { return res.sendStatus(409); } } else { - doc = { userId: obj.event.userId, times: [obj.time] } + doc = { 'userId': searchId, times: [obj.time] } kbo.insertOne(doc); } diff --git a/newCalEvent.html b/newCalEvent.html index 128adae..dcbf21d 100644 --- a/newCalEvent.html +++ b/newCalEvent.html @@ -125,19 +125,23 @@ function getSessionData() { - var xhrsess = new XMLHttpRequest(); - xhrsess.open('post', 'http://www.selmerbot.com/getSessionInfo/', true); - xhrsess.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); - xhrsess.setRequestHeader('session', window.localStorage.getItem('sessionId')); + return new Promise((resolve, reject) => { + var xhrsess = new XMLHttpRequest(); + xhrsess.open('post', 'http://www.selmerbot.com/getSessionInfo/', true); + xhrsess.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); + xhrsess.setRequestHeader('session', window.localStorage.getItem('sessionId')); - //Reminder that this will return a map of strings (you'll have to use JSON.parse() again) - xhrsess.onloadend = (e) => { - return JSON.parse(xhrsess.response); - } + //Reminder that this will return a map of strings (you'll have to use JSON.parse() again) + xhrsess.onloadend = (e) => { + resolve(JSON.parse(xhrsess.response)); + } + + xhrsess.send(); + }); } - function sendData(event) { + async function sendData(event) { event.preventDefault(); try { @@ -174,12 +178,19 @@ const d = new Date(new Date().getFullYear(), new Date().getMonth(), day, hour, minute); const serverSettings = JSON.parse(window.localStorage.getItem('serverInfo')); - const sessionDataPromise = await getSessionData(); + const sessionDataPromise = getSessionData(); sessionDataPromise.then((sessionData) => { - const id = sessionData.userId; - + var uid = null; + var gid = null; + + if (sessionStorage.getItem("ref") == "user") { + uid = sessionData.userId; + } else if (serverSettings != undefined) { + gid = serverSettings.Id; + } + //Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" location: "Some location" } } - let obj = { time: String(d.getTime() - offsetTS), event: { guildId: serverSettings.Id, userId: id, name: name, description: desc, offset: offset, link: url, location: loc } } + let obj = { time: String(d.getTime() - offsetTS), event: { guildId: gid, userId: uid, name: name, description: desc, offset: offset, link: url, location: loc } } //Send the data to the server var xhr = new XMLHttpRequest();