MDL-65313 core_message: remove favourites when deleting a conversation
authorJake Dallimore <jake@moodle.com>
Wed, 10 Apr 2019 00:29:43 +0000 (08:29 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 6 May 2019 06:01:34 +0000 (14:01 +0800)
When deleting a conversation for all users (e.g. when deleting a group
which has a conversation), we need to remove any favourites.

message/classes/api.php
message/tests/api_test.php

index 2b3bcee..f760142 100644 (file)
@@ -3309,6 +3309,9 @@ class api {
     public static function delete_all_conversation_data(int $conversationid) {
         global $DB;
 
+        $conv = $DB->get_record('message_conversations', ['id' => $conversationid], 'id, contextid');
+        $convcontext = !empty($conv->contextid) ? \context::instance_by_id($conv->contextid) : null;
+
         $DB->delete_records('message_conversations', ['id' => $conversationid]);
         $DB->delete_records('message_conversation_members', ['conversationid' => $conversationid]);
         $DB->delete_records('message_conversation_actions', ['conversationid' => $conversationid]);
@@ -3323,5 +3326,9 @@ class api {
             // Delete the messages now.
             $DB->delete_records('messages', ['conversationid' => $conversationid]);
         }
+
+        // Delete all favourite records for all users relating to this conversation.
+        $service = \core_favourites\service_factory::get_service_for_component('core_message');
+        $service->delete_favourites_by_type('message_conversations', $convcontext);
     }
 }
index a94c6ca..dd1dedd 100644 (file)
@@ -6851,6 +6851,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $g2m3 = \core_message\tests\helper::send_fake_message_to_conversation($user1, $groupconversation2->id);
         $g2m4 = \core_message\tests\helper::send_fake_message_to_conversation($user2, $groupconversation2->id);
 
+        // Favourite the conversation for several of the users.
+        \core_message\api::set_favourite_conversation($groupconversation1->id, $user1->id);
+        \core_message\api::set_favourite_conversation($groupconversation1->id, $user2->id);
+
         // Delete a few messages.
         \core_message\api::delete_message($user1->id, $g1m1);
         \core_message\api::delete_message($user1->id, $g1m2);
@@ -6895,6 +6899,14 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m2]));
         $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m3]));
         $this->assertEquals(1, $DB->count_records('messages', ['id' => $g2m4]));
+
+        // Confirm favourites were deleted for both users.
+        $user1service = \core_favourites\service_factory::get_service_for_user_context(context_user::instance($user1->id));
+        $this->assertFalse($user1service->favourite_exists('core_message', 'message_conversations', $groupconversation1->id,
+            $coursecontext1));
+        $user2service = \core_favourites\service_factory::get_service_for_user_context(context_user::instance($user1->id));
+        $this->assertFalse($user2service->favourite_exists('core_message', 'message_conversations', $groupconversation1->id,
+            $coursecontext1));
     }
 
     /**