Commit c712a7f4 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge changes from topic 'notify-plugins-(feature)'

* changes:
  Bug 1837194: Allow going directly to a message from a URL
  Bug 1837194: Notify plugins when all notifications are deleted.
  Bug 1837194: Notify plugins when a notification is deleted
  Bug 1837194: Notify plugins when a notification is marked read.
  Bug 1837194: Make user-to-user notifications available to plugins.
  Bug 1837194: Make newly created internal notifications available to plugins.
parents c5a64db0 ab0a0975
......@@ -131,6 +131,19 @@ jQuery(function($) {
});
JAVASCRIPT;
$externalmsg = param_integer('msg', 0);
$externalmsgtype = param_variable('msgtype', '');
$externalmsgtype = preg_replace('/[^a-z0-9_]+/i', '', $externalmsgtype);
if ($externalmsg && $externalmsgtype) {
$paginationjavascript .= '
jQuery(function($) {
$(document).ready(function() {
$("#notification-' . $externalmsgtype . '-' . $externalmsg . '").collapse("show");
});
});';
}
$deleteall = pieform(array(
'name' => 'delete_all_notifications',
'class' => 'form-deleteall sr-only',
......@@ -156,6 +169,15 @@ function delete_all_notifications_submit() {
$userid = $USER->get('id');
$type = param_variable('type', 'all');
$plugins = plugin_all_installed();
foreach ($plugins as $key => $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
safe_require($plugin->plugintype, $plugin->name);
if (!is_callable(array($classname, 'notification_delete'))) {
unset ($plugins[$key]);
}
}
db_begin();
// delete multirecipient-message separately
......@@ -184,6 +206,11 @@ function delete_all_notifications_submit() {
$msgids[] = $record->id;
}
delete_messages_mr($msgids, $userid);
foreach ($plugins as $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
call_static_method($classname, 'notification_delete', $msgids, $userid, 'module_multirecipient_notification');
}
}
$count = count($msgids);
}
......@@ -226,6 +253,12 @@ function delete_all_notifications_submit() {
);
// The update_unread_delete db trigger on notification_internal_activity
// will update the unread column on the usr table.
// And make sure any plugins that want to handle it can do so.
foreach ($plugins as $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
call_static_method($classname, 'notification_delete', $ids, $userid, 'notification_internal_activity');
}
}
db_commit();
......
......@@ -36,6 +36,13 @@ if ($readone) {
'newunreadcount' => $unread,
'newunreadcounttext' => get_string('unread', 'mahara', $unread)
);
foreach (plugin_all_installed() as $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
safe_require($plugin->plugintype, $plugin->name);
if (is_callable(array($classname, 'notification_read'))) {
call_static_method($classname, 'notification_read', array($readone), $USER->get('id'), $list);
}
}
json_reply(false, array('data' => $data));
}
......@@ -57,6 +64,14 @@ if ($markasread) {
$ids[$list][] = $m[2];
}
}
$plugins = plugin_all_installed();
foreach ($plugins as $key => $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
safe_require($plugin->plugintype, $plugin->name);
if (!is_callable(array($classname, 'notification_read'))) {
unset ($plugins[$key]);
}
}
foreach ($ids as $list => $idsperlist) {
if ($idsperlist) {
if ('module_multirecipient_notification' === $list) {
......@@ -72,6 +87,11 @@ if ($markasread) {
array($USER->get('id'))
);
$newunread = $USER->add_unread(-count($idsperlist));
foreach ($plugins as $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
call_static_method($classname, 'notification_read', $idsperlist, $USER->get('id'), $list);
}
}
}
$message = get_string('markedasread', 'activity');
......@@ -91,6 +111,16 @@ else if ($delete) {
db_begin();
$countdeleted = 0;
$plugins = plugin_all_installed();
foreach ($plugins as $key => $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
safe_require($plugin->plugintype, $plugin->name);
if (!is_callable(array($classname, 'notification_delete'))) {
unset ($plugins[$key]);
}
}
foreach ($ids as $list => $idsperlist) {
if ('module_multirecipient_notification' === $list) {
delete_messages_mr($idsperlist, $USER->get('id'));
......@@ -127,6 +157,11 @@ else if ($delete) {
}
}
$countdeleted += ($ids) ? count($ids) : count($idsperlist);
foreach ($plugins as $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
call_static_method($classname, 'notification_delete', $idsperlist, $USER->get('id'), $list);
}
}
db_commit();
$message = get_string('deletednotifications1', 'activity', $countdeleted);
......
......@@ -76,6 +76,8 @@ class ActivityTypeMultirecipientmessage extends ActivityTypeUsermessage {
* @param type $user
*/
public function notify_user($user) {
static $pluginlist = null;
$userdata = $this->to_stdclass();
$changes = new stdClass();
$userdata->usr = $user->id;
......@@ -111,6 +113,25 @@ class ActivityTypeMultirecipientmessage extends ActivityTypeUsermessage {
update_record('module_multirecipient_userrelation', $changes);
}
// If unread, check if any plugins want to do anything with this. (Handled this way as cheaper than using events.)
if (empty($userdata->read)) {
// Only do the include process once - don't even try to do inclusion if we know we already have.
if ($pluginlist === null) {
$pluginlist = plugin_all_installed();
foreach ($pluginlist as $plugin) {
safe_require($plugin->plugintype, $plugin->name);
}
}
foreach ($pluginlist as $key => $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
if (!is_callable(array($classname, 'notification_created'))) {
unset ($pluginlist[$key]);
continue;
}
call_static_method($classname, 'notification_created', $userdata->notification, $userdata, 'module_multirecipient_notification');
}
}
if (($user->method != 'internal') && ('sender' !== $userdata->role)) {
$method = $user->method;
safe_require('notification', $method);
......
......@@ -16,6 +16,8 @@ class PluginNotificationInternal extends PluginNotification {
static $userdata = array('urltext', 'subject', 'message');
public static function notify_user($user, $data) {
static $pluginlist = null;
$toinsert = new stdClass();
$toinsert->type = $data->type;
$toinsert->usr = $user->id;
......@@ -40,7 +42,28 @@ class PluginNotificationInternal extends PluginNotification {
$toinsert->from = $data->fromuser;
}
return insert_record('notification_internal_activity', $toinsert, 'id', true);
$messageid = insert_record('notification_internal_activity', $toinsert, 'id', true);
// If unread, check if any plugins want to do anything with this. (Handled this way as cheaper than using events.)
if (!$toinsert->read) {
// Only do the include process once - don't even try to do inclusion if we know we already have.
if ($pluginlist === null) {
$pluginlist = plugin_all_installed();
foreach ($pluginlist as $plugin) {
safe_require($plugin->plugintype, $plugin->name);
}
}
foreach ($pluginlist as $key => $plugin) {
$classname = generate_class_name($plugin->plugintype, $plugin->name);
if (!is_callable(array($classname, 'notification_created'))) {
unset ($pluginlist[$key]);
continue;
}
call_static_method($classname, 'notification_created', $messageid, $toinsert, 'notification_internal_activity');
}
}
return $messageid;
}
public static function postinst($prevversion) {
......
......@@ -246,4 +246,35 @@ class BehatNavigation extends BehatBase {
}
$node->click();
}
/**
* Goes directly to the URL to open a notification based on its author.
*
* @Given I go directly to the message from :author
* @param string $author The message's author
*/
public function i_go_directly_to_the_message($author = null) {
if (!$this->running_javascript()) {
return true;
}
// First go to the inbox, we'll find our message from there.
$this->visitPath("/module/multirecipientnotification/inbox.php");
// Now find the message itself.
$exception = new ExpectationException('The inbox item from ' . $author . ' could not be found.', $this->getSession());
$authorliteral = $this->escaper->escapeLiteral($author);
$xpath = "//span[@class='username'][contains(normalize-space(.),{$authorliteral})]/ancestor::a";
$node = $this->find('xpath', $xpath, new ExpectationException($xpath, $this->getSession()));
if (!$node->hasAttribute('data-id') || !$node->hasAttribute('data-list')) {
throw new ExpectationException('No data-id or data-list for inbox item "' . $author . '".', $this->getSession());
}
// Having found the message, let's go to it directly.
$msg = $node->getAttribute('data-id');
$msgtype = $node->getAttribute('data-list');
$this->visitPath("/module/multirecipientnotification/inbox.php?msgtype={$msgtype}&msg={$msg}");
}
}
......@@ -172,7 +172,6 @@ jQuery(function($) {
}
else if (self) {
$(self).removeClass('text-weight-bold js-card-unread').addClass('card');
$(self).find('.control').removeClass('control');
$(self).find('.unread').removeClass('unread'); // for inbox block
}
$('#selectall').attr('checked', false); // Need to uncheck bulk checkbox
......
@javascript @core @core_messages
Feature: Opening inbox messages from outside
In order to open messages in my inbox from a direct link
As a user
So I can click on a link in another platform to get to my message
Background:
Given the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| UserA | Kupuh1pa! | UserA@example.org | Angela | User | mahara | internal | member |
| UserB | Kupuh1pa! | UserB@example.org | Bob | User | mahara | internal | member |
| UserC | Kupuh1pa! | UserC@example.org | Cecilia | User | mahara | internal | member |
| UserD | Kupuh1pa! | UserD@example.org | Dmitri | User | mahara | internal | member |
And the following "messages" exist:
| emailtype | to | from | subject | messagebody | read | url | urltext |
| friendrequest | UserA | UserB | New friend request | Friend request from Bob | 1 | user/view.php?id=[from] | Requests |
| friendrequest | UserA | UserC | New friend request | Friend request from Cecilia | 1 | user/view.php?id=[from] | Requests |
| friendrequest | UserA | UserD | New friend request | Friend request from Dmitri | 1 | user/view.php?id=[from] | Requests |
| friendaccept | UserB | UserA | Friend request accepted - Bob | Friend request accepted from Bob | 1 | user/view.php?id=[to] | |
| friendaccept | UserC | UserA | Friend request accepted - Cecilia | Friend request accepted from Cecilia | 1 | user/view.php?id=[to] | |
| friendaccept | UserD | UserA | Friend request accepted - Dmitri | Friend request accepted from Dmitri | 1 | user/view.php?id=[to] | |
Scenario: Going to a direct link to a message will open the message directly in the inbox (Bug 1837194)
# Log in as users
Given I log in as "UserA" with password "Kupuh1pa!"
# Go to a specific message and verify its body is visible - but not another's body.
When I go directly to the message from "Cecilia User"
Then I should see "Friend request from Cecilia" in the "#activitylist" "css_element"
And I should not see "Friend request from Bob" in the "#activitylist" "css_element"
# Go to another specific message and verify its body is visible - but not another's body.
And I go directly to the message from "Bob User"
And I should see "Friend request from Bob" in the "#activitylist" "css_element"
And I should not see "Friend request from Cecilia" in the "#activitylist" "css_element"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment