This commit is contained in:
Andrew Nicols 2023-01-19 09:05:46 +08:00
commit 46eb8da969
6 changed files with 26 additions and 459 deletions

View File

@ -3203,6 +3203,13 @@ function make_categories_options() {
return core_course_category::make_categories_list('', 0, ' / ');
}
/**
* @deprecated since 3.10
*/
function message_count_unread_messages() {
throw new coding_exception('message_count_unread_messages has been removed.');
}
/**
* Checks if current user is shown any extra fields when listing users.
*

View File

@ -1072,88 +1072,17 @@ class api {
}
/**
* Returns the an array of the users the given user is in a conversation
* with who are a contact and the number of unread messages.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
* @return array
*/
public static function get_contacts_with_unread_message_count($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;
debugging('\core_message\api::get_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfieldsapi = \core_user\fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {message_contacts} mc
INNER JOIN {user} u
ON (u.id = mc.contactid OR u.id = mc.userid)
LEFT JOIN {messages} m
ON ((m.useridfrom = mc.contactid OR m.useridfrom = mc.userid) AND m.useridfrom != ?)
LEFT JOIN {message_conversation_members} mcm
ON mcm.conversationid = m.conversationid AND mcm.userid = ? AND mcm.userid != m.useridfrom
LEFT JOIN {message_user_actions} mua
ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
LEFT JOIN {message_users_blocked} mub
ON (mub.userid = ? AND mub.blockeduserid = u.id)
WHERE mua.id is NULL
AND mub.id is NULL
AND (mc.userid = ? OR mc.contactid = ?)
AND u.id != ?
AND u.deleted = 0
GROUP BY $userfields";
return $DB->get_records_sql($unreadcountssql, [$userid, $userid, $userid, self::MESSAGE_ACTION_READ,
$userid, $userid, $userid, $userid], $limitfrom, $limitnum);
public static function get_contacts_with_unread_message_count() {
throw new \coding_exception('\core_message\api::get_contacts_with_unread_message_count has been removed.');
}
/**
* Returns the an array of the users the given user is in a conversation
* with who are not a contact and the number of unread messages.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
* @return array
*/
public static function get_non_contacts_with_unread_message_count($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;
debugging('\core_message\api::get_non_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfieldsapi = \core_user\fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {user} u
INNER JOIN {messages} m
ON m.useridfrom = u.id
INNER JOIN {message_conversation_members} mcm
ON mcm.conversationid = m.conversationid
LEFT JOIN {message_user_actions} mua
ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
LEFT JOIN {message_contacts} mc
ON (mc.userid = ? AND mc.contactid = u.id)
LEFT JOIN {message_users_blocked} mub
ON (mub.userid = ? AND mub.blockeduserid = u.id)
WHERE mcm.userid = ?
AND mcm.userid != m.useridfrom
AND mua.id is NULL
AND mub.id is NULL
AND mc.id is NULL
AND u.deleted = 0
GROUP BY $userfields";
return $DB->get_records_sql($unreadcountssql, [$userid, self::MESSAGE_ACTION_READ, $userid, $userid, $userid],
$limitfrom, $limitnum);
public static function get_non_contacts_with_unread_message_count() {
throw new \coding_exception('\core_message\api::get_non_contacts_with_unread_message_count has been removed.');
}
/**

View File

@ -82,48 +82,6 @@ define('MESSAGE_GET_UNREAD', 0);
define('MESSAGE_GET_READ', 1);
define('MESSAGE_GET_READ_AND_UNREAD', 2);
/**
* Returns the count of unread messages for user. Either from a specific user or from all users.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param object $user1 the first user. Defaults to $USER
* @param object $user2 the second user. If null this function will count all of user 1's unread messages.
* @return int the count of $user1's unread messages
*/
function message_count_unread_messages($user1=null, $user2=null) {
global $USER, $DB;
debugging('message_count_unread_messages is deprecated and no longer used',
DEBUG_DEVELOPER);
if (empty($user1)) {
$user1 = $USER;
}
$sql = "SELECT COUNT(m.id)
FROM {messages} m
INNER JOIN {message_conversations} mc
ON mc.id = m.conversationid
INNER JOIN {message_conversation_members} mcm
ON mcm.conversationid = mc.id
LEFT JOIN {message_user_actions} mua
ON (mua.messageid = m.id AND mua.userid = ? AND (mua.action = ? OR mua.action = ?))
WHERE mua.id is NULL
AND mcm.userid = ?";
$params = [$user1->id, \core_message\api::MESSAGE_ACTION_DELETED, \core_message\api::MESSAGE_ACTION_READ, $user1->id];
if (!empty($user2)) {
$sql .= " AND m.useridfrom = ?";
$params[] = $user2->id;
} else {
$sql .= " AND m.useridfrom <> ?";
$params[] = $user1->id;
}
return $DB->count_records_sql($sql, $params);
}
/**
* Try to guess how to convert the message to html.
*

View File

@ -45,11 +45,13 @@ class api_test extends messagelib_test {
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->assertEquals(1, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id);
api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
// We've marked all of recipients conversation messages as read.
$this->assertEquals(0, api::count_unread_conversations($recipient));
}
public function test_mark_all_read_for_user_touser_with_fromuser() {
@ -70,32 +72,14 @@ class api_test extends messagelib_test {
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
$this->assertEquals(2, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id, $sender1->id);
$conversationid = api::get_conversation_between_users([$recipient->id, $sender1->id]);
api::mark_all_messages_as_read($recipient->id, $conversationid);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
}
public function test_mark_all_read_for_user_touser_with_type() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
api::mark_all_notifications_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
// We've marked only the conversation with sender1 messages as read.
$this->assertEquals(1, api::count_unread_conversations($recipient));
}
/**
@ -3806,229 +3790,6 @@ class api_test extends messagelib_test {
$this->assertCount(1, api::get_blocked_users($USER->id));
}
/**
* Test returning contacts with unread message count.
* MDL-69643
*/
public function test_get_contacts_with_unread_message_count() {
global $DB;
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// Add the users to each of their contacts.
api::add_contact($user1->id, $user2->id);
api::add_contact($user2->id, $user3->id);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$message4id = $this->send_fake_message($user1, $user2);
$this->send_fake_message($user3, $user2);
$message6id = $this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
// Send a message that should never be included as the user is not a contact.
$this->send_fake_message($user4, $user2);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
ksort($messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(4, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(5, $messageinfo2->messagecount);
// Mark some of the messages as read.
$m4 = $DB->get_record('messages', ['id' => $message4id]);
$m6 = $DB->get_record('messages', ['id' => $message6id]);
api::mark_message_as_read($user2->id, $m4);
api::mark_message_as_read($user2->id, $m6);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
ksort($messages);
// Confirm read messages are not included.
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(4, $messageinfo2->messagecount);
// Now, let's populate the database with messages from user2 to user 1.
$this->send_fake_message($user2, $user1);
$this->send_fake_message($user2, $user1);
$messageid = $this->send_fake_message($user2, $user1);
// Send a message that should never be included as the user is not a contact.
$this->send_fake_message($user4, $user1);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
// Mark the last message as read.
$m = $DB->get_record('messages', ['id' => $messageid]);
api::mark_message_as_read($user1->id, $m);
$messages = api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
// Confirm read messages are not included.
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(2, $messageinfo1->messagecount);
}
/**
* Test returning contacts with unread message count when there are no messages.
*/
public function test_get_contacts_with_unread_message_count_no_messages() {
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Add the users to each of their contacts.
api::add_contact($user2->id, $user1->id);
// Check we get the correct message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo->id);
$this->assertEquals(0, $messageinfo->messagecount);
}
/**
* Test returning non-contacts with unread message count.
* MDL-69643
*/
public function test_get_non_contacts_with_unread_message_count() {
global $DB;
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// Add a user to the contact list of the users we are testing this function with.
api::add_contact($user1->id, $user4->id);
api::add_contact($user2->id, $user4->id);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$message4id = $this->send_fake_message($user1, $user2);
$this->send_fake_message($user3, $user2);
$message6id = $this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
// Send a message that should never be included as the user is a contact.
$this->send_fake_message($user4, $user2);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check we get the correct message count.
ksort($messages);
$this->assertCount(2, $messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(4, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(5, $messageinfo2->messagecount);
// Mark some of the messages as read.
$m4 = $DB->get_record('messages', ['id' => $message4id]);
$m6 = $DB->get_record('messages', ['id' => $message6id]);
api::mark_message_as_read($user2->id, $m4);
api::mark_message_as_read($user2->id, $m6);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
ksort($messages);
$this->assertCount(2, $messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(4, $messageinfo2->messagecount);
// Now, let's populate the database with messages from user2 to user 1.
$this->send_fake_message($user2, $user1);
$this->send_fake_message($user2, $user1);
$messageid = $this->send_fake_message($user2, $user1);
// Send a message that should never be included as the user is a contact.
$this->send_fake_message($user4, $user1);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
// Mark the last message as read.
$m = $DB->get_record('messages', ['id' => $messageid]);
api::mark_message_as_read($user1->id, $m);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(2, $messageinfo1->messagecount);
}
/**
* Test marking a message as read.
*/

View File

@ -134,101 +134,6 @@ class messagelib_test extends \advanced_testcase {
message_get_contacts();
}
/**
* Test message_count_unread_messages.
* TODO: MDL-69643
*/
public function test_message_count_unread_messages() {
// Create users to send and receive message.
$userfrom1 = $this->getDataGenerator()->create_user();
$userfrom2 = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
// Send fake messages.
$this->send_fake_message($userfrom1, $userto);
$this->send_fake_message($userfrom2, $userto);
$this->assertEquals(2, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
$this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
* Test message_count_unread_messages with read messages.
*/
public function test_message_count_unread_messages_with_read_messages() {
global $DB;
// Create users to send and receive messages.
$userfrom1 = $this->getDataGenerator()->create_user();
$userfrom2 = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_unread_messages($userto));
// Send fake messages.
$messageid = $this->send_fake_message($userfrom1, $userto);
$this->send_fake_message($userfrom2, $userto);
// Mark message as read.
$message = $DB->get_record('messages', ['id' => $messageid]);
\core_message\api::mark_message_as_read($userto->id, $message);
// Should only count the messages that weren't read by the current user.
$this->assertEquals(1, message_count_unread_messages($userto));
$this->assertDebuggingCalledCount(2);
$this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
* Test message_count_unread_messages with deleted messages.
*/
public function test_message_count_unread_messages_with_deleted_messages() {
global $DB;
// Create users to send and receive messages.
$userfrom1 = $this->getDataGenerator()->create_user();
$userfrom2 = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
// Send fake messages.
$messageid = $this->send_fake_message($userfrom1, $userto);
$this->send_fake_message($userfrom2, $userto);
// Delete a message.
\core_message\api::delete_message($userto->id, $messageid);
// Should only count the messages that weren't deleted by the current user.
$this->assertEquals(1, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
$this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
* Test message_count_unread_messages with sent messages.
*/
public function test_message_count_unread_messages_with_sent_messages() {
$userfrom = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$this->send_fake_message($userfrom, $userto);
// Ensure an exception is thrown.
$this->assertEquals(0, message_count_unread_messages($userfrom));
$this->assertDebuggingCalled();
}
/**
* Test message_search_users.
*/

View File

@ -1,6 +1,13 @@
This files describes API changes in /message/ messaging system,
information provided here is intended especially for developers.
=== 4.2 ===
* The following methods, deprecated since 3.10, have been removed and can no longer be used:
- `message_count_unread_messages`
- `\core_message\api::get_non_contacts_with_unread_message_count`
- `\core_message\api::get_contacts_with_unread_message_count`
=== 4.0 ===
The following functions have been finally deprecated and can not be used anymore: