notifications: Refactor `process_notification` function.

Turned multiple if statements into a switch/case and rearranged
variable declarations.
This commit is contained in:
Daniil Fadeev 2023-07-26 20:04:58 +03:00 committed by Tim Abbott
parent 8b8f95b6f5
commit fe5d680f27
1 changed files with 24 additions and 25 deletions

View File

@ -265,23 +265,16 @@ function remove_sender_from_list_of_recipients(message) {
} }
export function process_notification(notification) { export function process_notification(notification) {
let notification_object;
const message = notification.message; const message = notification.message;
const content = get_notification_content(message); const content = get_notification_content(message);
const key = get_notification_key(message); const key = get_notification_key(message);
let notification_object;
let other_recipients; let other_recipients;
let title = message.sender_full_name; let title = message.sender_full_name;
let msg_count = 1; let msg_count = 1;
const topic = message.topic;
debug_notification_source_value(message); debug_notification_source_value(message);
if (message.type === "private" || message.type === "test-notification") {
// Remove the sender from the list of other recipients
other_recipients = remove_sender_from_list_of_recipients(message);
}
if (notice_memory.has(key)) { if (notice_memory.has(key)) {
msg_count = notice_memory.get(key).msg_count + 1; msg_count = notice_memory.get(key).msg_count + 1;
title = msg_count + " messages from " + title; title = msg_count + " messages from " + title;
@ -289,7 +282,12 @@ export function process_notification(notification) {
notification_object.close(); notification_object.close();
} }
if (message.type === "private") { switch (message.type) {
case "test-notification":
other_recipients = remove_sender_from_list_of_recipients(message);
break;
case "private":
other_recipients = remove_sender_from_list_of_recipients(message);
if (message.display_recipient.length > 2) { if (message.display_recipient.length > 2) {
// If the message has too many recipients to list them all... // If the message has too many recipients to list them all...
if (content.length + title.length + other_recipients.length > 230) { if (content.length + title.length + other_recipients.length > 230) {
@ -303,11 +301,12 @@ export function process_notification(notification) {
} else { } else {
title += " (to you)"; title += " (to you)";
} }
} break;
case "stream": {
if (message.type === "stream") {
const stream_name = stream_data.get_stream_name_from_id(message.stream_id); const stream_name = stream_data.get_stream_name_from_id(message.stream_id);
title += " (to " + stream_name + " > " + topic + ")"; title += " (to " + stream_name + " > " + message.topic + ")";
break;
}
} }
if (notification.desktop_notify) { if (notification.desktop_notify) {