MDL-63283 core_message: skip processors in non-individual conversations

Disable the processor for messages being sent to conversation types
other than individual.
This commit is contained in:
Jake Dallimore 2018-11-14 11:01:26 +08:00
parent dccda6546b
commit 267b4f03e7
2 changed files with 48 additions and 69 deletions

View File

@ -192,52 +192,55 @@ class manager {
}
// Fill in the array of processors to be used based on default and user preferences.
// This applies only to individual conversations. Messages to group conversations ignore processors.
$processorlist = [];
foreach ($processors as $processor) {
// Skip adding processors for internal user, if processor doesn't support sending message to internal user.
if (!$usertoisrealuser && !$processor->object->can_send_to_any_users()) {
continue;
}
if ($conv->type == \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
foreach ($processors as $processor) {
// Skip adding processors for internal user, if processor doesn't support sending message to internal user.
if (!$usertoisrealuser && !$processor->object->can_send_to_any_users()) {
continue;
}
// First find out permissions.
$defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
} else {
// MDL-25114 They supplied an $eventdata->component $eventdata->name combination which doesn't
// exist in the message_provider table (thus there is no default settings for them).
$preferrormsg = "Could not load preference $defaultpreference. Make sure the component and name you supplied
// First find out permissions.
$defaultpreference = $processor->name . '_provider_' . $preferencebase . '_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
} else {
// MDL-25114 They supplied an $eventdata->component $eventdata->name combination which doesn't
// exist in the message_provider table (thus there is no default settings for them).
$preferrormsg = "Could not load preference $defaultpreference. Make sure the component and name you supplied
to message_send() are valid.";
throw new coding_exception($preferrormsg);
}
throw new coding_exception($preferrormsg);
}
// Find out if user has configured this output.
// Some processors cannot function without settings from the user.
$userisconfigured = $processor->object->is_user_configured($recipient);
// Find out if user has configured this output.
// Some processors cannot function without settings from the user.
$userisconfigured = $processor->object->is_user_configured($recipient);
// DEBUG: notify if we are forcing unconfigured output.
if ($permitted == 'forced' && !$userisconfigured) {
debugging('Attempt to force message delivery to user who has "'.$processor->name.'" output unconfigured',
DEBUG_NORMAL);
}
// DEBUG: notify if we are forcing unconfigured output.
if ($permitted == 'forced' && !$userisconfigured) {
debugging('Attempt to force message delivery to user who has "' . $processor->name .
'" output unconfigured', DEBUG_NORMAL);
}
// Populate the list of processors we will be using.
if (!$eventdata->notification && $processor->object->force_process_messages()) {
$processorlist[] = $processor->name;
} else if ($permitted == 'forced' && $userisconfigured) {
// An admin is forcing users to use this message processor. Use this processor unconditionally.
$processorlist[] = $processor->name;
} else if ($permitted == 'permitted' && $userisconfigured && !$recipient->emailstop) {
// User has not disabled notifications.
// See if user set any notification preferences, otherwise use site default ones.
$userpreferencename = 'message_provider_'.$preferencebase.'_'.$userstate;
if ($userpreference = get_user_preferences($userpreferencename, null, $recipient)) {
if (in_array($processor->name, explode(',', $userpreference))) {
$processorlist[] = $processor->name;
}
} else if (isset($defaultpreferences->{$userpreferencename})) {
if (in_array($processor->name, explode(',', $defaultpreferences->{$userpreferencename}))) {
$processorlist[] = $processor->name;
// Populate the list of processors we will be using.
if (!$eventdata->notification && $processor->object->force_process_messages()) {
$processorlist[] = $processor->name;
} else if ($permitted == 'forced' && $userisconfigured) {
// An admin is forcing users to use this message processor. Use this processor unconditionally.
$processorlist[] = $processor->name;
} else if ($permitted == 'permitted' && $userisconfigured && !$recipient->emailstop) {
// User has not disabled notifications.
// See if user set any notification preferences, otherwise use site default ones.
$userpreferencename = 'message_provider_' . $preferencebase . '_' . $userstate;
if ($userpreference = get_user_preferences($userpreferencename, null, $recipient)) {
if (in_array($processor->name, explode(',', $userpreference))) {
$processorlist[] = $processor->name;
}
} else if (isset($defaultpreferences->{$userpreferencename})) {
if (in_array($processor->name, explode(',', $defaultpreferences->{$userpreferencename}))) {
$processorlist[] = $processor->name;
}
}
}
}

View File

@ -832,7 +832,7 @@ class core_messagelib_testcase extends advanced_testcase {
* need to be sure this is covered.
*/
public function test_message_send_to_conversation_group() {
global $DB, $CFG, $SITE;
global $DB;
$this->preventResetByRollback();
$this->resetAfterTest();
@ -862,45 +862,21 @@ class core_messagelib_testcase extends advanced_testcase {
$content = array('*' => array('header' => ' test ', 'footer' => ' test '));
$message->set_additional_content('email', $content);
// Ensure we're going to hit the email processor for the recipient users.
// Ensure the email processor is enabled for the recipient users.
$DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user3);
// Now, send a message and verify the message processors (in this case, email) are hit.
// Now, send a message and verify the email processor is NOT hit.
$sink = $this->redirectEmails();
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(2, $emails);
$this->assertCount(0, $emails);
// Verify the record was created in 'messages'.
$recordexists = $DB->record_exists('messages', ['id' => $messageid]);
$this->assertTrue($recordexists);
// Verify the email information. Ordering is not guaranteed.
$members = [$user2->email => '', $user3->email => ''];
$email = $emails[0];
$this->assertSame($user1->email, $email->from);
$this->assertArrayHasKey($email->to, $members);
unset($members[$email->to]);
$email = $emails[1];
$this->assertSame($user1->email, $email->from);
$this->assertArrayHasKey($email->to, $members);
unset($members[$email->to]);
// The message subject is generated during the call for conversation messages,
// as the conversation may have many members having different lang preferences.
$tmp = (object) ['name' => fullname($user1), 'conversationname' => $conversation->name];
$this->assertSame(get_string('unreadnewgroupconversationmessage', 'message', $tmp), $email->subject);
// The email content will have had an emailtagline appended to it, based on lang prefs,
// so verify the expected beginning and ends.
$this->assertNotEmpty($email->header);
$this->assertNotEmpty($email->body);
$this->assertRegExp('/test message body.*test/s', $email->body);
$sink->clear();
// Now, send the message again, and verify that the event fired includes the courseid and conversationid.
$eventsink = $this->redirectEvents();
$messageid = message_send($message);
@ -976,7 +952,7 @@ class core_messagelib_testcase extends advanced_testcase {
$transaction->allow_commit();
$events = $eventsink->get_events();
$emails = $sink->get_messages();
$this->assertCount(2, $emails);
$this->assertCount(0, $emails); // Email processor is disabled for messages to group conversations.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\group_message_sent', $events[0]);
}