Permalink
Browse files

chore(messages): unread count again updates when you view new messages

To display an accurate count, the count must be calculated after the new
message is displayed. Previously we were tied to the pagesetup event, but
that now fires early due to the use of resource views. Instead we add the
topbar menu item in the register hook.

BREAKING CHANGE:
If a plugin has removed or replaced messages_notifier to hide/alter the
inbox icon, the plugin must instead do the same for the topbar menu handler
(messages_register_topbar).

Fixes #8862
  • Loading branch information...
mrclay committed Aug 20, 2015
1 parent e9abcee commit 67cff4746d38c54905ba6ad3b8cc8f771d50feec
Showing with 41 additions and 20 deletions.
  1. +3 −0 docs/guides/upgrading.rst
  2. +38 −20 mod/messages/start.php
@@ -408,6 +408,9 @@ Just as before, the ``permissions_check:annotate`` hook is still called and may
Plugin Messages
---------------
If you've removed or replaced the handler function ``messages_notifier`` to hide/alter the inbox icon, you'll instead need to do the
same for the topbar menu handler ``messages_register_topbar``. ``messages_notifier`` is no longer used to add the menu link.
Messages will no longer get the metadata 'msg' for newly created messages. This means you can not rely on that metadata to exist.
Plugin Blog
View
@@ -31,8 +31,6 @@ function messages_init() {
));
}
elgg_register_event_handler('pagesetup', 'system', 'messages_notifier');
// Extend system CSS with our own styles, which are defined in the messages/css view
elgg_extend_view('elgg.css', 'messages/css');
elgg_extend_view('elgg.js', 'messages/js');
@@ -62,6 +60,10 @@ function messages_init() {
elgg_register_action("messages/send", "$action_path/send.php");
elgg_register_action("messages/delete", "$action_path/delete.php");
elgg_register_action("messages/process", "$action_path/process.php");
// Topbar menu. We assume this menu will render *after* a message is rendered. If a refactor/plugin
// causes it to render first, the unread count notification will not update until the next page.
elgg_register_plugin_hook_handler('register', 'menu:topbar', 'messages_register_topbar');
}
/**
@@ -125,28 +127,44 @@ function messages_page_handler($page) {
}
/**
* Display notification of new messages in topbar
* @deprecated Do not use this
*/
function messages_notifier() {
if (elgg_is_logged_in()) {
$text = elgg_view_icon("mail");
$tooltip = elgg_echo("messages");
// get unread messages
$num_messages = (int)messages_count_unread();
if ($num_messages != 0) {
$text .= "<span class=\"messages-new\">$num_messages</span>";
$tooltip .= " (" . elgg_echo("messages:unreadcount", array($num_messages)) . ")";
}
elgg_deprecated_notice( __FUNCTION__ . ' is no longer used and will be removed', '2.0');
}
elgg_register_menu_item('topbar', array(
'name' => 'messages',
'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username,
'text' => $text,
'priority' => 600,
'title' => $tooltip,
));
/**
* Add inbox link to topbar
*
* @param string $hook "register"
* @param string $type "menu:topbar"
* @param array $items Menu items
* @param array $params Hook params
* @return array
*/
function messages_register_topbar($hook, $type, $items, $params) {
if (!elgg_is_logged_in()) {
return;
}
$text = elgg_view_icon("mail");
$tooltip = elgg_echo("messages");
// get unread messages
$num_messages = (int)messages_count_unread();
if ($num_messages != 0) {
$text .= "<span class=\"messages-new\">$num_messages</span>";
$tooltip .= " (" . elgg_echo("messages:unreadcount", array($num_messages)) . ")";
}
$items[] = ElggMenuItem::factory([
'name' => 'messages',
'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username,
'text' => $text,
'priority' => 600,
'title' => $tooltip,
]);
return $items;
}
/**

0 comments on commit 67cff47

Please sign in to comment.