Skip to content

Commit

Permalink
MDL-63211 core_message: deprecated api::is_user_blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 3, 2018
1 parent b8dad79 commit c886e2c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
17 changes: 12 additions & 5 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,14 @@ public static function can_post_message($recipient, $sender = null) {
if ($sender !== null && isset($sender->id)) {
$senderid = $sender->id;
}

$systemcontext = \context_system::instance();
if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return true;
}

// The recipient has specifically blocked this sender.
if (self::is_user_blocked($recipient->id, $senderid)) {
if (self::is_blocked($recipient->id, $senderid)) {
return false;
}

Expand Down Expand Up @@ -959,12 +965,16 @@ public static function is_user_non_contact_blocked($recipient, $sender = null) {
* Note: This function will always return false if the sender has the
* readallmessages capability at the system context level.
*
* @deprecated since 3.6
* @param int $recipientid User ID of the recipient.
* @param int $senderid User ID of the sender.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_blocked($recipientid, $senderid = null) {
global $USER, $DB;
debugging('\core_message\api::is_user_blocked is deprecated and should not be used.',
DEBUG_DEVELOPER);

global $USER;

if (is_null($senderid)) {
// The message is from the logged in user, unless otherwise specified.
Expand Down Expand Up @@ -1552,9 +1562,6 @@ public static function get_contact(int $userid, int $contactid) {
/**
* Checks if a user is already blocked.
*
* This is different than self::is_user_blocked() as it does not check any capabilities.
* It simply checks if an entry exists in the DB.
*
* @param int $userid
* @param int $blockeduserid
* @return bool Returns true if they are a blocked, false otherwise
Expand Down
3 changes: 2 additions & 1 deletion message/classes/output/messagearea/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public function export_for_template(\renderer_base $output) {
$data->messages[] = $message->export_for_template($output);
}

$data->isblocked = api::is_user_blocked($this->currentuserid, $this->otheruserid);
$blockeduserid = $this->otheruserid ?? $USER->id;
$data->isblocked = api::is_blocked($this->currentuserid, $blockeduserid);

return $data;
}
Expand Down
4 changes: 4 additions & 0 deletions message/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,16 +1388,19 @@ public function test_is_user_blocked() {

// User shouldn't be blocked.
$this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();

// Block the user.
\core_message\api::block_user($user1->id, $user2->id);

// User should be blocked.
$this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();

// Unblock the user.
\core_message\api::unblock_user($user1->id, $user2->id);
$this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();
}

/**
Expand All @@ -1418,6 +1421,7 @@ public function test_is_user_blocked_as_admin() {

// As the admin you should still be able to send messages to the user.
$this->assertFalse(\core_message\api::is_user_blocked($user1->id));
$this->assertDebuggingCalled();
}

/*
Expand Down
2 changes: 2 additions & 0 deletions message/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ information provided here is intended especially for developers.
- message_block_contact()
- message_get_contact()
Please see their declaration in lib/deprecatedlib.php to view their alternatives (if applicable).
* The following methods have been deprecated and should not be used any more:
- \core_message\api::is_user_blocked()
* The following web services have been deprecated. Please do not call these any more.
- core_message_external::block_contacts, please use core_message_external::block_user instead.
- core_message_external::unblock_contacts, please use core_message_external::unblock_user instead.
Expand Down

0 comments on commit c886e2c

Please sign in to comment.