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 1 commit
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
28 changes: 22 additions & 6 deletions plugins/newmail_notifier/newmail_notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
pabzm marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this whole thing. Neither "Daniel L +3" looks good nor "New Mail! (3)".

How about we show the sender/subject only when there's a single new mail message? And "(n) New Mail!" in other cases?

? ' +' +otherCount
// No sender name, e.g. "New Mail! (3)
: '('+otherCount + ')';
Copy link
Member

Choose a reason for hiding this comment

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

Please add spaces around the plus-signs. Or maybe you could rework these title-related lines a little, including line 40, to be a little less terse but more readable?

}
newmail_notifier_desktop(title, body);
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down
19 changes: 19 additions & 0 deletions plugins/newmail_notifier/newmail_notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

Not a big deal, but here you could use array_first().

if ($headers !== null) {
$from = $headers->from;
$subject = $headers->subject;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what sanity checks we should apply here. We probably should not use too long subjects. Also we should parse the From header content. We definitely should decode the content, not pass as-is.

See program/actions/mail/show.php for hints on what we normally do with subject and from header.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I'll take a look. I wasn't sure whether to limit the subject length here or on the client side.

Copy link
Author

Choose a reason for hiding this comment

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

We probably should not use too long subjects.

I tried with a long subject, and KDE makes them scrollable:

image

I haven't tested with Windows yet, but I think it truncates them.

We definitely should decode the content, not pass as-is. See program/actions/mail/show.php for hints on what we normally do with subject and from header.

Thanks for the pointer. I thought the headers would already be decoded here.

}
}

$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