Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newmail_notifier: Include sender and subject in desktop notification #9492

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions plugins/newmail_notifier/newmail_notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ function newmail_notifier_run(prop) {
newmail_notifier_sound();
}
if (prop.desktop) {
newmail_notifier_desktop(rcmail.get_label('body', 'newmail_notifier'));
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);
}
}

Expand Down Expand Up @@ -96,11 +106,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,
Expand Down
24 changes: 24 additions & 0 deletions plugins/newmail_notifier/newmail_notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,36 @@ 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_first($headers);
if ($headers !== null) {
$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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name can be empty. In such case let's fallback to the email address. Also, in some corner cases From header might not exist at all.

}
}

$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(),
]
);
}
Expand Down