Skip to content

Commit

Permalink
fix: not notifying for pre-scheduled announcements (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
elijaholmos authored Jul 25, 2023
1 parent 3896655 commit 18e2b36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions classes/HaloWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,21 @@ export class HaloWatcher extends EventEmitter {
//write local cache to file, since changes were detected
await writeCacheFile({ filepath: class_id, data: new_announcements });

/**
* Determine if an announcement was published "recently" (determined by inner-function criteria)
* @returns {boolean} Whether or not the announcement was published recently
*/
const isRecentAnnouncement = function (announcement) {
const publishTime = new Date(announcement.publishDate).getTime();
const startTime = new Date(announcement.startDate).getTime();
const threshold = new Date().getTime() - 1000 * 60 * 60 * 48; // 48 hours

return publishTime > threshold || startTime > threshold;
};

// to prevent announcement spam upon bot restart, only emit announcements that were published in past 6 hours
for (const announcement of this.#locateDifferenceInArrays(new_announcements, old_announcements))
new Date(announcement.publishDate).getTime() > new Date().getTime() - 1000 * 60 * 60 * 6 &&
this.emit('announcement', announcement);
isRecentAnnouncement(announcement) && this.emit('announcement', announcement);
} catch (e) {
if (e.code === 401)
Logger.unauth(`Received 401 while fetching announcements for course ${course.courseCode}`);
Expand Down
1 change: 1 addition & 0 deletions classes/services/HaloService.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const getClassAnnouncements = async function ({ cookie, class_id, metadat
id
content
publishDate
startDate
title
postStatus
createdBy {
Expand Down

0 comments on commit 18e2b36

Please sign in to comment.