Skip to content

Commit

Permalink
added calendar link to menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jcolson committed Mar 11, 2021
1 parent dcd10cf commit d5d238a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
10 changes: 3 additions & 7 deletions handlers/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ const he = require('he');

/**
*
* @param {*} request
* @param {*} response
* @param {*} url
* @param {String} userID
* @param {Array} excludeGuild
*/
async function handleCalendarRequest(requestUrl) {
console.log('handling calendar request: search params: ', requestUrl.searchParams);
const userID = requestUrl.searchParams.get('userID');
const excludeGuild = requestUrl.searchParams.get('exclude') ? requestUrl.searchParams.get('exclude').split(',') : [];
async function handleCalendarRequest(userID, excludeGuild) {
if (!userID) {
throw new Error('No userID passed!');
}
Expand Down
3 changes: 3 additions & 0 deletions http_views/includes/menu.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
a(href= '/') Home
| | 
a(href= '/timezones') Timezones
if discordMe
| | 
a(href= '/calendar?userID='+discordMe.id) Calendar ICS
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ let server = app
}
})
.get(ROUTE_ROOT, function (request, response) {
response.render('index', { title: 'Home', Config: Config, discordMe: request.session.discordMe })
response.render('index', { title: 'Home', Config: Config, discordMe: request.session.discordMe });
})
.get(ROUTE_TIMEZONESSET, async function (request, response) {
try {
Expand Down Expand Up @@ -174,10 +174,26 @@ let server = app
.get(ROUTE_CALENDAR, async (request, response) => {
try {
console.log('serving ' + ROUTE_CALENDAR);
let requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
let responseContent = await calendar.handleCalendarRequest(requestUrl);
response.setHeader('Content-Type', 'text/calendar');
response.end(responseContent);
const requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
let userID = requestUrl.searchParams.get('userID');
const excludeGuild = requestUrl.searchParams.get('exclude') ? requestUrl.searchParams.get('exclude').split(',') : [];
if (!userID && request.session.discordMe) {
// console.log(`have discordMe, setting userID`);
userID = request.session.discordMe.id;
}
if (!userID) {
// console.log(`don't have userID, redirecting to discord to login`);
request.query.destination = ROUTE_CALENDAR;
response.redirect(url.format({
pathname: grant.config.discord.prefix + "/discord",
query: request.query,
}));
} else {
// console.log(`have userid, heading to handleCalendarRequest`);
let responseContent = await calendar.handleCalendarRequest(userID, excludeGuild);
response.setHeader('Content-Type', 'text/calendar');
response.end(responseContent);
}
} catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
Expand Down

0 comments on commit d5d238a

Please sign in to comment.