Skip to content

Commit

Permalink
feat: add show overall grade setting (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
elijaholmos authored Dec 7, 2022
1 parent 950079c commit f5c03f3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions classes/HaloWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class HaloWatcher extends EventEmitter {
for (const grade of this.#locateDifferenceInArrays(new_grades, old_grades)) {
//if the user has already viewed the grade, don't send a notification
if (!!grade.userLastSeenDate) continue;
if (!Firebase.getUserSettingValue({ uid, setting_id: 1 })) continue; //check setting inside diff loop to ensure cache was updated
// if (!Firebase.getUserSettingValue({ uid, setting_id: 1 })) continue; //check setting inside diff loop to ensure cache was updated

//fetch the full grade feedback
this.emit(
Expand Down Expand Up @@ -268,7 +268,7 @@ export class HaloWatcher extends EventEmitter {
for (const post of this.#locateDifferenceInArrays(new_inbox_posts, old_inbox_posts)) {
//if !post.iRead && post.id is not in cache, then dispatch event
if (!!post.isRead) continue;
if (!Firebase.getUserSettingValue({ uid, setting_id: 2 })) continue; //check setting inside diff loop to ensure cache was updated
// if (!Firebase.getUserSettingValue({ uid, setting_id: 2 })) continue; //check setting inside diff loop to ensure cache was updated
this.emit('inbox_message', { ...post, metadata: { uid } });
}
}
Expand Down
2 changes: 1 addition & 1 deletion classes/services/AnnouncementService.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AnnouncementService {
//get all active users in the class and send the message to them
for (const uid of Firebase.getActiveUsersInClass(announcement.courseClassId)) {
try {
if (!Firebase.getUserSettingValue({ uid, setting_id: 0 })) continue;
// if (!Firebase.getUserSettingValue({ uid, setting_id: 0 })) continue;
const discord_uid = Firebase.getDiscordUid(uid);
const discord_user = await bot.users.fetch(discord_uid);
discord_user
Expand Down
38 changes: 21 additions & 17 deletions classes/services/GradeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ export class GradeService {
* @param {Object} grade A full Halo UserCourseClassAssessmentGrade object
*/
static processGrade = (grade) => {
this.#publishGrade({
grade,
message: this.#parseGradeData({ grade }),
});
this.#publishGrade({ grade });
};

/**
* @param {Object} args Desctructured arguments
* @param {Object} args.grade A full Halo UserCourseClassAssessmentGrade object
* @param {Object} args.message A parsed message object to be sent straight to Discord
* @returns {Promise<void>}
*/
static async #publishGrade({ grade, message }) {
static async #publishGrade({ grade }) {
try {
const discord_uid =
Firebase.getDiscordUid(grade?.metadata?.uid) ??
(await Firebase.getDiscordUidFromHaloUid(grade.user.id));
const show_overall_grade = Firebase.getUserSettingValue({
uid: Firebase.getHNSUid(discord_uid),
setting_id: 4,
});

const discord_user = await bot.users.fetch(discord_uid);
discord_user
.send(message)
.send(this.#parseGradeData({ grade, show_overall_grade }))
.catch((e) =>
Logger.error(`Error sending grade notification to ${discord_user.tag} (${discord_uid}): ${e}`)
);
Expand Down Expand Up @@ -72,9 +73,10 @@ export class GradeService {
/**
* @param {Object} args Desctructured arguments
* @param {Object} args.grade A full Halo UserCourseClassAssessmentGrade object
* @param {boolean} [args.show_overall_grade=true] Whether or not to include the overall class grade in the message embed
* @returns {Object} A message object to be sent straight to Discord
*/
static #parseGradeData({ grade }) {
static #parseGradeData({ grade, show_overall_grade = true }) {
const parsePercent = function (dividend, divisor) {
return divisor < 1 ? 'N/A' : `${round((dividend / divisor) * 100, 2)}%`;
};
Expand All @@ -98,15 +100,6 @@ export class GradeService {
{
name: 'Assignment Score:',
value: `**${finalPoints} / ${points}** (${parsePercent(finalPoints, points)})`,
inline: true,
},
{
name: 'Overall Grade:',
value: `**${totalFinalPoints} / ${maxPoints}** (${parsePercent(
totalFinalPoints,
maxPoints
)} \u200b ${gradeValue}) `,
inline: true,
},
{
name: `Feedback:`,
Expand All @@ -117,6 +110,17 @@ export class GradeService {
.replace(/<\/?[^>]+(>|$)/g, '')
: 'None',
},
...(show_overall_grade
? [
{
name: 'Overall Class Grade:',
value: `**${totalFinalPoints} / ${maxPoints}** (${parsePercent(
totalFinalPoints,
maxPoints
)} \u200b ${gradeValue}) `,
},
]
: []),
],
timestamp: Date.now(),
}),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "halo-discord-bot",
"version": "2.3.0",
"version": "2.3.1",
"author": "Elijah Olmos",
"license": "AGPL-3.0-only",
"main": "index.js",
Expand Down

0 comments on commit f5c03f3

Please sign in to comment.