diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php
index 0c24640bab2..79775c89f73 100644
--- a/lib/deprecatedlib.php
+++ b/lib/deprecatedlib.php
@@ -5616,7 +5616,11 @@ function message_is_user_blocked($recipient, $sender = null) {
debugging('message_is_user_blocked() is deprecated and is no longer used, please use
\core_message\api::is_user_blocked() instead.', DEBUG_DEVELOPER);
- return \core_message\api::is_user_blocked($recipient, $sender);
+ $senderid = null;
+ if ($sender !== null && isset($sender->id)) {
+ $senderid = $sender->id;
+ }
+ return \core_message\api::is_user_blocked($recipient->id, $senderid);
}
/**
diff --git a/message/classes/api.php b/message/classes/api.php
index eabe7d7379b..0d90dae5f71 100644
--- a/message/classes/api.php
+++ b/message/classes/api.php
@@ -601,8 +601,12 @@ class api {
return false;
}
+ $senderid = null;
+ if ($sender !== null && isset($sender->id)) {
+ $senderid = $sender->id;
+ }
// The recipient has specifically blocked this sender.
- if (self::is_user_blocked($recipient, $sender)) {
+ if (self::is_user_blocked($recipient->id, $senderid)) {
return false;
}
@@ -648,27 +652,25 @@ class api {
* Note: This function will always return false if the sender has the
* readallmessages capability at the system context level.
*
- * @param object $recipient User object.
- * @param object $sender User object.
+ * @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($recipient, $sender = null) {
+ public static function is_user_blocked($recipientid, $senderid = null) {
global $USER, $DB;
- if (is_null($sender)) {
+ if (is_null($senderid)) {
// The message is from the logged in user, unless otherwise specified.
- $sender = $USER;
+ $senderid = $USER->id;
}
$systemcontext = \context_system::instance();
- if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) {
+ if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return false;
}
- if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) {
- if ($contact->blocked) {
- return true;
- }
+ if ($DB->get_field('message_contacts', 'blocked', ['userid' => $recipientid, 'contactid' => $senderid])) {
+ return true;
}
return false;
diff --git a/message/classes/output/messagearea/messages.php b/message/classes/output/messagearea/messages.php
index 368d1d08ec6..e967ec15dcd 100644
--- a/message/classes/output/messagearea/messages.php
+++ b/message/classes/output/messagearea/messages.php
@@ -26,6 +26,7 @@ namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
+use core_message\api;
use renderable;
use templatable;
@@ -95,6 +96,8 @@ class messages implements templatable, renderable {
$data->messages[] = $message->export_for_template($output);
}
+ $data->isblocked = api::is_user_blocked($this->currentuserid, $this->otheruserid);
+
return $data;
}
}
diff --git a/message/externallib.php b/message/externallib.php
index 2ecaf66b101..1d394bcac98 100644
--- a/message/externallib.php
+++ b/message/externallib.php
@@ -970,7 +970,8 @@ class core_message_external extends external_api {
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'messages' => new external_multiple_structure(
self::get_messagearea_message_structure()
- )
+ ),
+ 'isblocked' => new external_value(PARAM_BOOL, 'Is this user blocked by the current user?', VALUE_DEFAULT, false),
)
);
}
diff --git a/message/templates/message_area_messages_area.mustache b/message/templates/message_area_messages_area.mustache
index acfa4494a37..ebafaec1c79 100644
--- a/message/templates/message_area_messages_area.mustache
+++ b/message/templates/message_area_messages_area.mustache
@@ -14,6 +14,37 @@
You should have received a copy of the GNU General Public License
along with Moodle. If not, see