Skip to content

Commit

Permalink
Create a 'title' section for study sessions (#47)
Browse files Browse the repository at this point in the history
The first line of a study session can be used as a title to be
used in the 'upcoming study' message so that it doesn't get too
cluttered with information. If there's no title supplied the
message is still truncated to fit on a line or two

```
!study Do some reading
2021/07/12 23:00 1 hour
We'll be reading some beginner Korean texts from example.com
```
Would result in the same study session message showing up on
creation but when running `!upcoming study` it would show up like
this:
```
User's study session
Date Time (Length)
Do some reading - Subscribe here
```
As opposed to
```
User's study session
Date Time (Length)
Do some reading
2021/07/12 23:00 1 hour
We'll be reading some beginner Korean texts from example.com - Subscribe here
```
  • Loading branch information
Fox-Islam authored Jun 4, 2021
1 parent 5dabdcf commit 1741c82
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
34 changes: 32 additions & 2 deletions src/constants/studySession.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const STUDY_SESSION = {
SUCCESS: (session) => ({
title: "STUDY SESSION",
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${session.message?.text}\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*`,
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,
}),
ERROR: (error) => ({
Expand All @@ -32,7 +32,7 @@ const STUDY_SESSION = {
content: "Here are the upcoming study sessions:\n*Make sure to check the time zones!*",
fields: sessions.map((session) => ({
name: `${session.author.username}'s study session`,
value: `*${getUTCFullDate(session.startDate)} UTC (${session.estimatedLength} min)*\n${session.message?.text} - Subscribe [here](${session.message?.link})`,
value: `*${getUTCFullDate(session.startDate)} UTC (${session.estimatedLength} min)*\n${getUpcomingStudySessionSummary(session.message)} - Subscribe [here](${session.message?.link})`,
})),
}),
ERROR: (error) => ({
Expand Down Expand Up @@ -87,4 +87,34 @@ const STUDY_SESSION = {
},
};

function getStudySessionText(message) {
if (!message || !message.text) {
return "";
}
const lines = message.text.split("\n");
if (lines.length < 2) {
return lines[0];
}
return lines.splice(1, lines.length - 1).join("\n");
}

function getUpcomingStudySessionSummary(message) {
if (!message || !message.text) {
return "";
}

const text = message.text;
const firstLine = text.split("\n", 1)[0].trim();
if (firstLine.length > 0) {
return firstLine;
}

const contentOnSingleLine = text.split("\n").join(" ").trim();
const truncatedString = contentOnSingleLine.split(" ").splice(0, 8).join(" ");
if (contentOnSingleLine.length > truncatedString.length + 3) {
return truncatedString + "...";
}
return truncatedString;
}

module.exports = { STUDY_SESSION };
4 changes: 3 additions & 1 deletion src/scripts/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ function handleHelpStudyCommand(message) {
name: 'Format',
value: `
The study command requires
- a title placed on the first line (beside the \`!study\` command)
- a date in the format YYYY/MM/DD
- a UTC time in HH:mm
- a session length in H hours, mm minutes or a combination of both
Expand All @@ -235,7 +236,8 @@ This message should also contain a description of the session
value: `
Sending a message like this:
\`\`\`
!study 2022/03/05 at 13:30 for 1 hour
!study Grammar lesson
2022/03/05 at 13:30 for 1 hour
We'll be studying some fundamental Korean grammar!
\`\`\`
Results in the bot creating a study session and responding with a message that looks like this:
Expand Down
21 changes: 20 additions & 1 deletion src/tasks/studySession/channelReminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function makeStudySessionMessage() {
title: "UPCOMING STUDY SESSIONS",
fields: upcomingStudySessions.map((session) => ({
name: `${session.author.username}'s study session`,
value: `*${getUTCFullDate(session.startDate)} UTC (${session.estimatedLength} min)*\n${session.message?.text} - Subscribe [here](${session.message?.link})`,
value: `*${getUTCFullDate(session.startDate)} UTC (${session.estimatedLength} min)*\n${getUpcomingStudySessionSummary(session.message)} - Subscribe [here](${session.message?.link})`,
})),
color: "GREEN"
}
Expand All @@ -79,6 +79,25 @@ function makeStudySessionMessage() {
});
}

function getUpcomingStudySessionSummary(message) {
if (!message || !message.text) {
return "";
}

const text = message.text;
const firstLine = text.split("\n", 1)[0].trim();
if (firstLine.length > 0) {
return firstLine;
}

const contentOnSingleLine = text.split("\n").join(" ").trim();
const truncatedString = contentOnSingleLine.split(" ").splice(0, 8).join(" ");
if (contentOnSingleLine.length > truncatedString.length + 3) {
return truncatedString + "...";
}
return truncatedString;
}

function createNotFoundMessage() {
return {
content: upcomingStudySessionMessageContent,
Expand Down

0 comments on commit 1741c82

Please sign in to comment.