Skip to content

Commit

Permalink
Merge pull request #132 from Riott/issue-130
Browse files Browse the repository at this point in the history
Issue 130 !sch handle all day events
  • Loading branch information
11k authored Dec 26, 2024
2 parents e79b452 + f7d14cb commit d1efc0c
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 61 deletions.
31 changes: 24 additions & 7 deletions lib/commands/implementations/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@ function getNextStreamDate(input, services) {
.findNextStreamDay()
.then((nextStreamDay) => {
if (!nextStreamDay) {
return new CommandOutput(null, `Nothing scheduled ${services.schedule.link()}`);
return new CommandOutput(null, `Nothing is scheduled. ${services.schedule.link()}`);
}

return new CommandOutput(
null,
`${nextStreamDay.name}${
nextStreamDay.allDay ? ', an all-day event,' : ''
} scheduled to begin ${moment(nextStreamDay.start).fromNow()} ${services.schedule.link()}`,
);
const { allDay, name, start, childEvent } = nextStreamDay;
const isHappening = start.isBefore(moment());
let scheduleOutput = '';

if (isHappening && allDay) {
scheduleOutput = `${name}, an all-day event, is scheduled today.`;
if (childEvent) {
const childEventIsHappening = moment(childEvent.start).isBefore(moment());

// prettier-ignore
scheduleOutput += childEventIsHappening
? ` ${childEvent.name} is currently happening! It started ${childEvent.start.fromNow()}.`
: ` Coming up ${childEvent.start.fromNow()} is ${childEvent.name}.`;
}
} else if (isHappening) {
scheduleOutput = `${name} is currently happening! It started ${start.fromNow()}.`;
} else {
scheduleOutput = `${name} is the next scheduled event. It starts ${start.fromNow()}.`;
}

scheduleOutput += ` ${services.schedule.link()}`;

return new CommandOutput(null, scheduleOutput);
})
.catch((err) => new CommandOutput(err, "Oops. Something didn't work. Check the logs."));
}
Expand Down
60 changes: 39 additions & 21 deletions lib/services/schedule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
const google = require('googleapis').google; // eslint-disable-line prefer-destructuring
const moment = require('moment-timezone');

/**
* @typedef {Object} Event
* @property {boolean} allDay
* @property {moment.Moment} start
* @property {string} name
*/

/**
* @returns {Event}
*/
function buildEvent(event) {
// All-day events don't have a start time, only a start day.
const allDay = !event.start.dateTime;

const start = allDay ? moment.utc(event.start.date) : moment.utc(event.start.dateTime);

return {
allDay,
start,
name: event.summary,
};
}

class GoogleCal {
constructor(configuration) {
this.configuration = configuration;
Expand All @@ -12,29 +35,23 @@ class GoogleCal {
}

findNextStreamDay() {
return this.getListOfUpcomingEvents(this.configuration.GOOGLE_CALENDAR_ID).then((events) => {
const now = moment.tz('UTC');
const nextEvent = events.find((event) => {
if (event.start.date !== undefined) {
return true;
return this.getListOfUpcomingEvents(this.configuration.GOOGLE_CALENDAR_ID).then(
(googleEvents) => {
const events = googleEvents.map(buildEvent);
if (!events.length) {
return null;
}

return !(
moment.tz(event.end.dateTime, 'YYYY-MM-DDTHH:mm:ss', event.end.timeZone).isAfter(now) &&
moment.tz(event.start.dateTime, 'YYYY-MM-DDTHH:mm:ss', event.start.timeZone).isBefore(now)
);
});

if (!nextEvent) {
return null;
}

return {
start: nextEvent.start.dateTime ?? nextEvent.start.date,
name: nextEvent.summary,
allDay: !nextEvent.start.dateTime, // All-day events don't have a start time, only a start day.
};
});
const nextEvent = events[0];
if (nextEvent.allDay) {
nextEvent.childEvent = events.find((event) => {
return !event.allDay && event.start.isSame(nextEvent.start, 'day');
});
}

return nextEvent;
},
);
}

link() {
Expand All @@ -49,6 +66,7 @@ class GoogleCal {
singleEvents: true,
timeMin: new Date().toISOString(),
maxResults: 5,
timeZone: 'UTC',
})
.then((response) => response.data.items);
}
Expand Down
184 changes: 167 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bunyan": "^1.8.14",
"fast-levenshtein": "^3.0.0",
"google-auth-library": "^6.1.1",
"googleapis": "^61.0.0",
"googleapis": "^118.0.0",
"inspector-influx": "^2.7.0",
"inspector-metrics": "^1.21.0",
"lodash": "^4.17.20",
Expand Down
Loading

0 comments on commit d1efc0c

Please sign in to comment.