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,27 +282,33 @@ export function process_notification(notification) {
notification_object.close(); notification_object.close();
} }
if (message.type === "private") { switch (message.type) {
if (message.display_recipient.length > 2) { case "test-notification":
// If the message has too many recipients to list them all... other_recipients = remove_sender_from_list_of_recipients(message);
if (content.length + title.length + other_recipients.length > 230) { break;
// Then count how many people are in the conversation and summarize case "private":
// by saying the conversation is with "you and [number] other people" other_recipients = remove_sender_from_list_of_recipients(message);
other_recipients = if (message.display_recipient.length > 2) {
other_recipients.replaceAll(/[^,]/g, "").length + " other people"; // If the message has too many recipients to list them all...
if (content.length + title.length + other_recipients.length > 230) {
// Then count how many people are in the conversation and summarize
// by saying the conversation is with "you and [number] other people"
other_recipients =
other_recipients.replaceAll(/[^,]/g, "").length + " other people";
}
title += " (to you and " + other_recipients + ")";
} else {
title += " (to you)";
} }
break;
title += " (to you and " + other_recipients + ")"; case "stream": {
} else { const stream_name = stream_data.get_stream_name_from_id(message.stream_id);
title += " (to you)"; title += " (to " + stream_name + " > " + message.topic + ")";
break;
} }
} }
if (message.type === "stream") {
const stream_name = stream_data.get_stream_name_from_id(message.stream_id);
title += " (to " + stream_name + " > " + topic + ")";
}
if (notification.desktop_notify) { if (notification.desktop_notify) {
const icon_url = people.small_avatar_url(message); const icon_url = people.small_avatar_url(message);
notification_object = new NotificationAPI(title, { notification_object = new NotificationAPI(title, {