summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphael Kabo <raphael@Raphaels-MacBook-Pro.local>2019-08-01 18:50:33 +0100
committerRaphael Kabo <raphael@Raphaels-MacBook-Pro.local>2019-08-01 18:50:33 +0100
commitd55727a6f7d8e3a6d4c5dfe588d4e1332654a1bf (patch)
tree1499b36718859dd068bb628d44ded842ea5203ad
parentcc6ac96df25c909ec2a256b0a81fe91ce30ce0ce (diff)
parenta8fa61379b96f2179887c43d6aa870fdaba35a3c (diff)
Merge branch 'master' of https://github.com/lowercasename/gathio into timezones
-rw-r--r--config/api-example.js2
-rwxr-xr-xroutes.js307
2 files changed, 161 insertions, 148 deletions
diff --git a/config/api-example.js b/config/api-example.js
index 08bd845..4df7ba3 100644
--- a/config/api-example.js
+++ b/config/api-example.js
@@ -1,3 +1,3 @@
module.exports = {
- 'sendgrid' : 'SENDGRID API KEY GOES HERE'
+ 'sendgrid' : '' // SendGrid API key goes here
};
diff --git a/routes.js b/routes.js
index e394a61..020c819 100755
--- a/routes.js
+++ b/routes.js
@@ -21,10 +21,15 @@ const marked = require('marked');
const ical = require('ical');
+const sgMail = require('@sendgrid/mail');
+
const apiCredentials = require('./config/api.js');
-const sgMail = require('@sendgrid/mail');
-sgMail.setApiKey(apiCredentials.sendgrid);
+let sendEmails = false;
+if (apiCredentials.sendgrid) { // Only set up Sendgrid if an API key is set
+ sgMail.setApiKey(apiCredentials.sendgrid);
+ sendEmails = true;
+}
const fileUpload = require('express-fileupload');
var Jimp = require('jimp');
@@ -128,13 +133,10 @@ router.get('/new/event/public', (req, res) => {
})
router.get('/:eventID', (req, res) => {
- console.log("Showing event")
- console.log("req.params.eventID",req.params.eventID)
Event.findOne({
id: req.params.eventID
})
.then((event) => {
- console.log("event",event)
if (event) {
parsedLocation = event.location.replace(/\s+/g, '+');
if (moment(event.end).isSame(event.start, 'day')){
@@ -261,28 +263,29 @@ router.post('/newevent', (req, res) => {
.then(() => {
addToLog("createEvent", "success", "Event " + eventID + "created");
// Send email with edit link
- const msg = {
- to: req.body.creatorEmail,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-00330b8278ab463e9f88c16566487d97',
- dynamic_template_data: {
- subject: 'gathio: ' + req.body.eventName,
- eventID: eventID,
- editToken: editToken
- },
- };
- sgMail.send(msg).then(() => {
- res.writeHead(302, {
- 'Location': '/' + eventID + '?e=' + editToken
+ if (sendEmails) {
+ const msg = {
+ to: req.body.creatorEmail,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-00330b8278ab463e9f88c16566487d97',
+ dynamic_template_data: {
+ subject: 'gathio: ' + req.body.eventName,
+ eventID: eventID,
+ editToken: editToken
+ },
+ };
+ sgMail.send(msg).catch(e => {
+ console.error(e.toString());
+ res.status(500).end();
});
- res.end();
- }).catch(e => {
- console.error(e.toString());
- res.status(500).end();
+ }
+ res.writeHead(302, {
+ 'Location': '/' + eventID + '?e=' + editToken
});
+ res.end();
})
.catch((err) => { res.send('Database error, please try again :('); addToLog("createEvent", "error", "Attempt to create event failed with error: " + err);});
});
@@ -318,30 +321,31 @@ router.post('/importevent', (req, res) => {
});
event.save()
.then(() => {
- addToLog("createEvent", "success", "Event " + eventID + "created");
+ addToLog("createEvent", "success", "Event " + eventID + " created");
// Send email with edit link
- const msg = {
- to: creatorEmail,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-00330b8278ab463e9f88c16566487d97',
- dynamic_template_data: {
- subject: 'gathio: ' + req.body.eventName,
- eventID: eventID,
- editToken: editToken
- },
- };
- sgMail.send(msg).then(() => {
- res.writeHead(302, {
- 'Location': '/' + eventID + '?e=' + editToken
+ if (sendEmails) {
+ const msg = {
+ to: creatorEmail,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-00330b8278ab463e9f88c16566487d97',
+ dynamic_template_data: {
+ subject: 'gathio: ' + req.body.eventName,
+ eventID: eventID,
+ editToken: editToken
+ },
+ };
+ sgMail.send(msg).catch(e => {
+ console.error(e.toString());
+ res.status(500).end();
});
- res.end();
- }).catch(e => {
- console.error(e.toString());
- res.status(500).end();
+ }
+ res.writeHead(302, {
+ 'Location': '/' + eventID + '?e=' + editToken
});
+ res.end();
})
.catch((err) => { res.send('Database error, please try again :('); addToLog("createEvent", "error", "Attempt to create event failed with error: " + err);});
}
@@ -395,30 +399,32 @@ router.post('/editevent/:eventID/:editToken', (req, res) => {
})
.then(() => {
addToLog("editEvent", "success", "Event " + req.params.eventID + " edited");
- Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
- attendeeEmails = ids;
- if (!error && attendeeEmails != ""){
- console.log("Sending emails to: " + attendeeEmails);
- const msg = {
- to: attendeeEmails,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-e21f3ca49d82476b94ddd8892c72a162',
- dynamic_template_data: {
- subject: 'gathio: Event edited',
- actionType: 'edited',
- eventExists: true,
- eventID: req.params.eventID
+ if (sendEmails) {
+ Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
+ attendeeEmails = ids;
+ if (!error && attendeeEmails != ""){
+ console.log("Sending emails to: " + attendeeEmails);
+ const msg = {
+ to: attendeeEmails,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-e21f3ca49d82476b94ddd8892c72a162',
+ dynamic_template_data: {
+ subject: 'gathio: Event edited',
+ actionType: 'edited',
+ eventExists: true,
+ eventID: req.params.eventID
+ }
}
+ sgMail.sendMultiple(msg);
}
- sgMail.sendMultiple(msg);
- }
- else {
- console.log("Nothing to send!");
- }
- })
+ else {
+ console.log("Nothing to send!");
+ }
+ })
+ }
res.writeHead(302, {
'Location': '/' + req.params.eventID + '?e=' + req.params.editToken
});
@@ -449,31 +455,32 @@ router.post('/deleteevent/:eventID/:editToken', (req, res) => {
}
// Send emails here otherwise they don't exist lol
-
- Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
- attendeeEmails = ids;
- if (!error){
- console.log("Sending emails to: " + attendeeEmails);
- const msg = {
- to: attendeeEmails,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-e21f3ca49d82476b94ddd8892c72a162',
- dynamic_template_data: {
- subject: 'gathio: Event "' + event.name + '" deleted',
- actionType: 'deleted',
- eventExists: false,
- eventID: req.params.eventID
+ if (sendEmails) {
+ Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
+ attendeeEmails = ids;
+ if (!error){
+ console.log("Sending emails to: " + attendeeEmails);
+ const msg = {
+ to: attendeeEmails,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-e21f3ca49d82476b94ddd8892c72a162',
+ dynamic_template_data: {
+ subject: 'gathio: Event "' + event.name + '" deleted',
+ actionType: 'deleted',
+ eventExists: false,
+ eventID: req.params.eventID
+ }
}
+ sgMail.sendMultiple(msg);
}
- sgMail.sendMultiple(msg);
- }
- else {
- console.log("Nothing to send!");
- }
- });
+ else {
+ console.log("Nothing to send!");
+ }
+ });
+ }
Event.deleteOne({id: req.params.eventID}, function(err, raw) {
if (err) {
@@ -523,20 +530,22 @@ router.post('/attendevent/:eventID', (req, res) => {
event.save()
.then(() => {
addToLog("addEventAttendee", "success", "Attendee added to event " + req.params.eventID);
- if (req.body.attendeeEmail){
- const msg = {
- to: req.body.attendeeEmail,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-977612474bba49c48b58e269f04f927c',
- dynamic_template_data: {
- subject: 'gathio: ' + event.name,
- eventID: req.params.eventID
- },
- };
- sgMail.send(msg);
+ if (sendEmails) {
+ if (req.body.attendeeEmail){
+ const msg = {
+ to: req.body.attendeeEmail,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-977612474bba49c48b58e269f04f927c',
+ dynamic_template_data: {
+ subject: 'gathio: ' + event.name,
+ eventID: req.params.eventID
+ },
+ };
+ sgMail.send(msg);
+ }
}
res.writeHead(302, {
@@ -564,29 +573,31 @@ router.post('/post/comment/:eventID', (req, res) => {
event.save()
.then(() => {
addToLog("addEventComment", "success", "Comment added to event " + req.params.eventID);
- Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
- attendeeEmails = ids;
- if (!error){
- console.log("Sending emails to: " + attendeeEmails);
- const msg = {
- to: attendeeEmails,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-756d078561e047aba307155f02b6686d',
- dynamic_template_data: {
- subject: 'gathio: New comment in ' + event.name,
- commentAuthor: req.body.commentAuthor,
- eventID: req.params.eventID
+ if (sendEmails) {
+ Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
+ attendeeEmails = ids;
+ if (!error){
+ console.log("Sending emails to: " + attendeeEmails);
+ const msg = {
+ to: attendeeEmails,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-756d078561e047aba307155f02b6686d',
+ dynamic_template_data: {
+ subject: 'gathio: New comment in ' + event.name,
+ commentAuthor: req.body.commentAuthor,
+ eventID: req.params.eventID
+ }
}
+ sgMail.sendMultiple(msg);
}
- sgMail.sendMultiple(msg);
- }
- else {
- console.log("Nothing to send!");
- }
- });
+ else {
+ console.log("Nothing to send!");
+ }
+ });
+ }
res.writeHead(302, {
'Location': '/' + req.params.eventID
});
@@ -613,29 +624,31 @@ router.post('/post/reply/:eventID/:commentID', (req, res) => {
event.save()
.then(() => {
addToLog("addEventReply", "success", "Reply added to comment " + commentID + " in event " + req.params.eventID);
- Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
- attendeeEmails = ids;
- if (!error){
- console.log("Sending emails to: " + attendeeEmails);
- const msg = {
- to: attendeeEmails,
- from: {
- name: 'Gathio',
- email: 'notifications@gath.io',
- },
- templateId: 'd-756d078561e047aba307155f02b6686d',
- dynamic_template_data: {
- subject: 'gathio: New comment in ' + event.name,
- commentAuthor: req.body.commentAuthor,
- eventID: req.params.eventID
+ if (sendEmails) {
+ Event.findOne({id: req.params.eventID}).distinct('attendees.email', function(error, ids) {
+ attendeeEmails = ids;
+ if (!error){
+ console.log("Sending emails to: " + attendeeEmails);
+ const msg = {
+ to: attendeeEmails,
+ from: {
+ name: 'Gathio',
+ email: 'notifications@gath.io',
+ },
+ templateId: 'd-756d078561e047aba307155f02b6686d',
+ dynamic_template_data: {
+ subject: 'gathio: New comment in ' + event.name,
+ commentAuthor: req.body.commentAuthor,
+ eventID: req.params.eventID
+ }
}
+ sgMail.sendMultiple(msg);
}
- sgMail.sendMultiple(msg);
- }
- else {
- console.log("Nothing to send!");
- }
- });
+ else {
+ console.log("Nothing to send!");
+ }
+ });
+ }
res.writeHead(302, {
'Location': '/' + req.params.eventID
});