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 ""; diff --git a/src/scheduler.js b/src/scheduler.js index abfb706..0267b58 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["day"].length > 0) { + cron.schedule('0 12 * * *', () => { + tasks["day"].map((task) => task(client)) + }); + } } export default runScheduler; 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/index.js b/src/tasks/index.js index b999066..b0f1a4a 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], + "day": [sendChannelReminder] +}; + +export default tasks; diff --git a/src/tasks/studySession/channelReminder.js b/src/tasks/studySession/channelReminder.js index 53dfe7a..87a2545 100644 --- a/src/tasks/studySession/channelReminder.js +++ b/src/tasks/studySession/channelReminder.js @@ -1,9 +1,7 @@ -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!*"; -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,17 +26,19 @@ export default function sendChannelReminder(client) { const mostRecentStudySessionMessage = studySessionMessages.first(); - if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) { - mostRecentStudySessionMessage.delete().then(() => { - makeStudySessionMessage().then((studyMessage) => { + 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) }); @@ -54,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) { @@ -79,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 ""; @@ -98,6 +100,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, 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) => {