From 5adc510371bada1cac83637f856f17cf503c46cf Mon Sep 17 00:00:00 2001 From: Fox Islam Date: Thu, 3 Jun 2021 19:47:46 +0100 Subject: [PATCH 1/5] Add a check for changes in study session message before reposting If people want to have notifications on for the events channel it might get annoying having the ping go off even if there are no changes --- src/tasks/studySession/channelReminder.js | 27 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/tasks/studySession/channelReminder.js b/src/tasks/studySession/channelReminder.js index 53dfe7a..78ce8d7 100644 --- a/src/tasks/studySession/channelReminder.js +++ b/src/tasks/studySession/channelReminder.js @@ -29,12 +29,16 @@ export default function sendChannelReminder(client) { const mostRecentStudySessionMessage = studySessionMessages.first(); if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) { - mostRecentStudySessionMessage.delete().then(() => { - makeStudySessionMessage().then((studyMessage) => { - studySessionChannel.send(studyMessage).catch((error) => { + makeStudySessionMessage().then((studyMessage) => { + if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) { + mostRecentStudySessionMessage.delete().then(() => { + studySessionChannel.send(studyMessage).catch((error) => { + console.log(error); + }); + }).catch((error) => { console.log(error); }); - }); + } }).catch((error) => { console.log(error); }); @@ -98,6 +102,21 @@ function getUpcomingStudySessionSummary(message) { return truncatedString; } +function upcomingSessionsAreDifferent(oldMessage, newMessage) { + const oldMessageEmbed = oldMessage.embeds[0]; + const newMessageEmbed = newMessage.embed; + if (oldMessageEmbed.title !== newMessageEmbed.title) { + return true; + } + if (oldMessageEmbed.fields.length !== newMessageEmbed.fields.length) { + return true; + } + return oldMessageEmbed.fields.some((oldMessageEmbedField, index) => { + return newMessageEmbed.fields[index].name !== oldMessageEmbedField.name || + newMessageEmbed.fields[index].value !== oldMessageEmbedField.value + }); +} + function createNotFoundMessage() { return { content: upcomingStudySessionMessageContent, From 4d4dff6804b62ad857794b25d7255b2bb36182a3 Mon Sep 17 00:00:00 2001 From: Fox Islam Date: Sat, 5 Jun 2021 01:14:07 +0100 Subject: [PATCH 2/5] Create additional scheduler to handle channel reminder Explicitly running the reminder task every 5 hours instead of running it every minute and trying to handle the timing from within the task logic --- src/scheduler.js | 20 +++++++++++--- src/tasks/index.js | 11 +++++++- src/tasks/studySession/channelReminder.js | 30 ++++++++------------- src/tasks/studySession/notifySubscribers.js | 2 +- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/scheduler.js b/src/scheduler.js index abfb706..f33f85a 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -3,9 +3,23 @@ import tasks from './tasks'; function runScheduler(client) { // Run every minutes - cron.schedule('* * * * *', () => { - tasks.map((task) => task(client)) - }); + if (tasks["minute"].length > 0) { + cron.schedule('* * * * *', () => { + tasks["minute"].map((task) => task(client)) + }); + } + + if (tasks["hour"].length > 0) { + cron.schedule('0 */1 * * *', () => { + tasks["hour"].map((task) => task(client)) + }); + } + + if (tasks["five-hour"].length > 0) { + cron.schedule('0 */5 * * *', () => { + tasks["five-hour"].map((task) => task(client)) + }); + } } export default runScheduler; diff --git a/src/tasks/index.js b/src/tasks/index.js index b999066..f08e11f 100644 --- a/src/tasks/index.js +++ b/src/tasks/index.js @@ -1,3 +1,12 @@ import studySessionTasks from './studySession'; -export default [...studySessionTasks]; +const notifySubscribers = studySessionTasks[0]; +const sendChannelReminder = studySessionTasks[1]; + +const tasks = { + "minute": [], + "hour": [notifySubscribers], + "five-hour": [sendChannelReminder] +}; + +export default tasks; diff --git a/src/tasks/studySession/channelReminder.js b/src/tasks/studySession/channelReminder.js index 78ce8d7..794758b 100644 --- a/src/tasks/studySession/channelReminder.js +++ b/src/tasks/studySession/channelReminder.js @@ -2,8 +2,6 @@ const { getUpcomingStudySessionsForScheduler } = require('../../scripts/activiti const { getUTCFullDate } = require("../../utils/date"); const upcomingStudySessionMessageContent = "Here are the upcoming study sessions:\n*Make sure to check the time zones!*"; -const oneHour = 60 * 60 * 1000; -const interval = oneHour * 5; export default function sendChannelReminder(client) { const studySessionChannel = client.channels.cache.get(process.env.STUDY_SESSION_CHANNEL); @@ -28,21 +26,19 @@ export default function sendChannelReminder(client) { const mostRecentStudySessionMessage = studySessionMessages.first(); - if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) { - makeStudySessionMessage().then((studyMessage) => { - if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) { - mostRecentStudySessionMessage.delete().then(() => { - studySessionChannel.send(studyMessage).catch((error) => { - console.log(error); - }); - }).catch((error) => { + makeStudySessionMessage().then((studyMessage) => { + if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) { + mostRecentStudySessionMessage.delete().then(() => { + studySessionChannel.send(studyMessage).catch((error) => { console.log(error); }); - } - }).catch((error) => { - console.log(error); - }); - } + }).catch((error) => { + console.log(error); + }); + } + }).catch((error) => { + console.log(error); + }); }).catch(function (error) { console.log(error) }); @@ -58,10 +54,6 @@ function isStudySessionMessage(message) { return message.content === upcomingStudySessionMessageContent; } -function isMessageOlderThan5HoursAgo(message) { - return (new Date() - message.createdAt) > interval; -} - function makeStudySessionMessage() { return getUpcomingStudySessionsForScheduler().then((upcomingStudySessions) => { if (upcomingStudySessions.length === 0) { diff --git a/src/tasks/studySession/notifySubscribers.js b/src/tasks/studySession/notifySubscribers.js index e953351..507ad39 100644 --- a/src/tasks/studySession/notifySubscribers.js +++ b/src/tasks/studySession/notifySubscribers.js @@ -6,7 +6,7 @@ const StudySession = mongoose.model("StudySession"); export default function notifySubscribersTask(client) { const now = new Date(); const limit = new Date().setHours(now.getHours() + 1); - StudySession.find({startDate: {$gt: now, $lt: limit}, notificationSent: false}, (error, studySessions) => { + StudySession.find({ startDate: { $gt: now, $lte: limit }, notificationSent: false }, (error, studySessions) => { if (error) console.error(error); if (studySessions.length === 0) return null; studySessions.map((studySession) => { From 1a0a22dc44a4bc1267a2cdb057ce96853aa03428 Mon Sep 17 00:00:00 2001 From: Fox Islam Date: Sat, 5 Jun 2021 19:05:16 +0100 Subject: [PATCH 3/5] Use session title in embed title --- src/constants/studySession.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/constants/studySession.js b/src/constants/studySession.js index 0d54e6e..df4209d 100644 --- a/src/constants/studySession.js +++ b/src/constants/studySession.js @@ -4,7 +4,7 @@ const { getUTCFullDate, getUTCFullTime } = require("../utils/date"); const STUDY_SESSION = { CREATE: { SUCCESS: (session) => ({ - title: "STUDY SESSION", + title: getCreatedSessionTitle(session.message), content: "Study session has been registered successfully!", description: `šŸ“† ${getUTCFullDate(session.startDate, "date")} at ${getUTCFullDate(session.startDate, "time")} *(UTC)*\nšŸ•‘ Estimated length: ${session.estimatedLength} minutes.\n\n${getStudySessionText(session.message)}\n\n*If anybody wants to join the session, subscribe using the ā­ button\nIf you want to cancel the session, delete the message used to create it*`, withAuthor: true, @@ -87,6 +87,20 @@ const STUDY_SESSION = { }, }; +function getCreatedSessionTitle(message) { + if (!message || !message.text) { + return ""; + } + + const text = message.text; + const firstLine = text.split("\n", 1)[0].trim(); + if (firstLine.length > 0) { + return firstLine; + } + + return "STUDY SESSION"; +} + function getStudySessionText(message) { if (!message || !message.text) { return ""; From 1d3c951bd0a0df6e80d6d20425d6f6360bc1ffec Mon Sep 17 00:00:00 2001 From: Fox Islam Date: Sat, 5 Jun 2021 23:25:24 +0100 Subject: [PATCH 4/5] Update upcoming sessions post whenever a session is created --- src/scripts/activities/study-session.js | 4 +++- src/tasks/studySession/channelReminder.js | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/scripts/activities/study-session.js b/src/scripts/activities/study-session.js index dfd9050..35f5733 100644 --- a/src/scripts/activities/study-session.js +++ b/src/scripts/activities/study-session.js @@ -1,7 +1,8 @@ +const sendChannelReminder = require('./../../tasks/studySession/channelReminder').default; const StudySession = require("mongoose").model("StudySession"); const { react } = require("../../utils/react"); const { STUDY_SESSION } = require("../../constants/studySession"); -const { replyInfo, replySuccess, replyError, replySurvey, sendDirectMessage } = require("../../utils/message"); +const { replySuccess, replyError, sendDirectMessage } = require("../../utils/message"); function getStudySessionDate(text) { // Regex Declaration @@ -58,6 +59,7 @@ function createStudySession(message) { .then(() => { replySuccess(message, STUDY_SESSION.CREATE.SUCCESS(studySession)); react(message, null, ["ā­"]); + sendChannelReminder(message.author.client); }) .catch((error) => replyError(message, STUDY_SESSION.CREATE.ERROR(error))); } diff --git a/src/tasks/studySession/channelReminder.js b/src/tasks/studySession/channelReminder.js index 794758b..87a2545 100644 --- a/src/tasks/studySession/channelReminder.js +++ b/src/tasks/studySession/channelReminder.js @@ -1,4 +1,4 @@ -const { getUpcomingStudySessionsForScheduler } = require('../../scripts/activities/study-session'); +const StudySession = require("mongoose").model("StudySession"); const { getUTCFullDate } = require("../../utils/date"); const upcomingStudySessionMessageContent = "Here are the upcoming study sessions:\n*Make sure to check the time zones!*"; @@ -75,6 +75,12 @@ function makeStudySessionMessage() { }); } +function getUpcomingStudySessionsForScheduler() { + return StudySession.find({ startDate: { $gt: new Date() } }, null, { sort: "startDate" }).limit(5).catch((error) => { + console.log(error); + }); +} + function getUpcomingStudySessionSummary(message) { if (!message || !message.text) { return ""; From 38bcdf1355ba011cc0aced7070f67dabcb41763c Mon Sep 17 00:00:00 2001 From: Fox Islam Date: Sat, 5 Jun 2021 23:26:03 +0100 Subject: [PATCH 5/5] Increase time between scheduled event reminders to once per day --- src/scheduler.js | 6 +++--- src/tasks/index.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/scheduler.js b/src/scheduler.js index f33f85a..0267b58 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -15,9 +15,9 @@ function runScheduler(client) { }); } - if (tasks["five-hour"].length > 0) { - cron.schedule('0 */5 * * *', () => { - tasks["five-hour"].map((task) => task(client)) + if (tasks["day"].length > 0) { + cron.schedule('0 12 * * *', () => { + tasks["day"].map((task) => task(client)) }); } } diff --git a/src/tasks/index.js b/src/tasks/index.js index f08e11f..b0f1a4a 100644 --- a/src/tasks/index.js +++ b/src/tasks/index.js @@ -6,7 +6,7 @@ const sendChannelReminder = studySessionTasks[1]; const tasks = { "minute": [], "hour": [notifySubscribers], - "five-hour": [sendChannelReminder] + "day": [sendChannelReminder] }; export default tasks;