From 8fb21f36286e8342f091984090357f2f0c55ce3f Mon Sep 17 00:00:00 2001 From: Daniel Lo Nigro Date: Sun, 9 Jun 2024 17:09:44 -0700 Subject: [PATCH 1/2] newmail_notifier: Include sender and subject in desktop notification --- plugins/newmail_notifier/newmail_notifier.js | 28 +++++++++++++++---- plugins/newmail_notifier/newmail_notifier.php | 19 +++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/plugins/newmail_notifier/newmail_notifier.js b/plugins/newmail_notifier/newmail_notifier.js index f8e1edd52c0..3ff50f7e786 100644 --- a/plugins/newmail_notifier/newmail_notifier.js +++ b/plugins/newmail_notifier/newmail_notifier.js @@ -37,7 +37,19 @@ function newmail_notifier_run(prop) { newmail_notifier_sound(); } if (prop.desktop) { - newmail_notifier_desktop(rcmail.get_label('body', 'newmail_notifier')); + var title = prop.from || rcmail.get_label('title', 'newmail_notifier'); + var body = prop.subject || rcmail.get_label('body', 'newmail_notifier'); + + // If there's more than one unread email, show the number of other emails + var otherCount = prop.count - 1; + if (otherCount >= 1) { + title += prop.from + // Have sender name, e.g. "Daniel L +3" + ? ' +' +otherCount + // No sender name, e.g. "New Mail! (3) + : '('+otherCount + ')'; + } + newmail_notifier_desktop(title, body); } } @@ -96,11 +108,11 @@ function newmail_notifier_sound() { // Desktop notification // - Require window.Notification API support (Chrome 22+ or Firefox 22+) -function newmail_notifier_desktop(body, disabled_callback) { +function newmail_notifier_desktop(title, body, disabled_callback) { var timeout = rcmail.env.newmail_notifier_timeout || 10, icon = rcmail.assets_path('plugins/newmail_notifier/mail.png'), success_callback = function () { - var popup = new window.Notification(rcmail.get_label('title', 'newmail_notifier'), { + var popup = new window.Notification(title, { dir: 'auto', lang: '', body: body, @@ -134,9 +146,13 @@ function newmail_notifier_desktop(body, disabled_callback) { } function newmail_notifier_test_desktop() { - var status = newmail_notifier_desktop(rcmail.get_label('testbody', 'newmail_notifier'), function () { - rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error'); - }); + var status = newmail_notifier_desktop( + rcmail.get_label('title', 'newmail_notifier'), + rcmail.get_label('testbody', 'newmail_notifier'), + function () { + rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error'); + } + ); if (!status) { rcmail.display_message(rcmail.get_label('desktopunsupported', 'newmail_notifier'), 'error'); diff --git a/plugins/newmail_notifier/newmail_notifier.php b/plugins/newmail_notifier/newmail_notifier.php index cccf0fe207f..c745de068ce 100644 --- a/plugins/newmail_notifier/newmail_notifier.php +++ b/plugins/newmail_notifier/newmail_notifier.php @@ -203,12 +203,31 @@ public function notify($args) if ($unseen->count()) { $this->notified = true; + $from = null; + $subject = null; + // Attempt to fetch headers for the latest unseen message + $latest_uid = $unseen->max(); + if ($latest_uid !== null) { + $headers = $storage->fetch_headers($mbox, [$latest_uid]); + // fetch_headers returns an array, but we only care about the first one. + $headers = array_shift($headers); + if ($headers !== null) { + $from = $headers->from; + $subject = $headers->subject; + } + } + $this->rc->output->set_env('newmail_notifier_timeout', $this->rc->config->get('newmail_notifier_desktop_timeout')); $this->rc->output->command('plugin.newmail_notifier', [ + // Config 'basic' => $this->opt['basic'], 'sound' => $this->opt['sound'], 'desktop' => $this->opt['desktop'], + // Data + 'from' => $from, + 'subject' => $subject, + 'count' => $unseen->count(), ] ); } From de539202e5775a0c028b9127abfa10a338340e17 Mon Sep 17 00:00:00 2001 From: Daniel Lo Nigro Date: Sun, 7 Jul 2024 17:59:13 -0700 Subject: [PATCH 2/2] Code review comments - Show "(2) New Mail!" when there's more than one email - Decode from and subject headers Signed-off-by: Daniel Lo Nigro --- plugins/newmail_notifier/newmail_notifier.js | 30 ++++++++----------- plugins/newmail_notifier/newmail_notifier.php | 11 +++++-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/plugins/newmail_notifier/newmail_notifier.js b/plugins/newmail_notifier/newmail_notifier.js index 3ff50f7e786..0b066808a9e 100644 --- a/plugins/newmail_notifier/newmail_notifier.js +++ b/plugins/newmail_notifier/newmail_notifier.js @@ -37,18 +37,16 @@ function newmail_notifier_run(prop) { newmail_notifier_sound(); } if (prop.desktop) { - var title = prop.from || rcmail.get_label('title', 'newmail_notifier'); - var body = prop.subject || rcmail.get_label('body', 'newmail_notifier'); - - // If there's more than one unread email, show the number of other emails - var otherCount = prop.count - 1; - if (otherCount >= 1) { - title += prop.from - // Have sender name, e.g. "Daniel L +3" - ? ' +' +otherCount - // No sender name, e.g. "New Mail! (3) - : '('+otherCount + ')'; + var title; + var body; + if (prop.count === 1) { + title = prop.from || rcmail.get_label('title', 'newmail_notifier'); + body = prop.subject || rcmail.get_label('body', 'newmail_notifier'); + } else { + title = '(' + prop.count + ') ' + rcmail.get_label('title', 'newmail_notifier'); + body = rcmail.get_label('body', 'newmail_notifier'); } + newmail_notifier_desktop(title, body); } } @@ -146,13 +144,9 @@ function newmail_notifier_desktop(title, body, disabled_callback) { } function newmail_notifier_test_desktop() { - var status = newmail_notifier_desktop( - rcmail.get_label('title', 'newmail_notifier'), - rcmail.get_label('testbody', 'newmail_notifier'), - function () { - rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error'); - } - ); + var status = newmail_notifier_desktop(rcmail.get_label('testbody', 'newmail_notifier'), function () { + rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error'); + }); if (!status) { rcmail.display_message(rcmail.get_label('desktopunsupported', 'newmail_notifier'), 'error'); diff --git a/plugins/newmail_notifier/newmail_notifier.php b/plugins/newmail_notifier/newmail_notifier.php index c745de068ce..2f5f3c43d46 100644 --- a/plugins/newmail_notifier/newmail_notifier.php +++ b/plugins/newmail_notifier/newmail_notifier.php @@ -210,10 +210,15 @@ public function notify($args) if ($latest_uid !== null) { $headers = $storage->fetch_headers($mbox, [$latest_uid]); // fetch_headers returns an array, but we only care about the first one. - $headers = array_shift($headers); + $headers = array_first($headers); if ($headers !== null) { - $from = $headers->from; - $subject = $headers->subject; + $subject = trim( + rcube_mime::decode_header($headers->subject, $headers->charset) + ); + $from_details = array_first( + rcube_mime::decode_address_list($headers->from, 1, true, $headers->charset) + ); + $from = $from_details['name']; } }