MDL-64773 core_message: added API to mute/unmute conversations
authorMark Nelson <markn@moodle.com>
Wed, 6 Feb 2019 09:09:16 +0000 (17:09 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 13 Mar 2019 00:55:13 +0000 (08:55 +0800)
message/classes/api.php
message/tests/api_test.php

index b15a97e..3885da3 100644 (file)
@@ -48,6 +48,11 @@ class api {
      */
     const MESSAGE_ACTION_DELETED = 2;
 
+    /**
+     * The action for reading a message.
+     */
+    const CONVERSATION_ACTION_MUTED = 1;
+
     /**
      * The privacy setting for being messaged by anyone within courses user is member of.
      */
@@ -3133,4 +3138,59 @@ class api {
 
         return $counts;
     }
+
+    /**
+     * Handles muting a conversation.
+     *
+     * @param int $userid The id of the user
+     * @param int $conversationid The id of the conversation
+     */
+    public static function mute_conversation(int $userid, int $conversationid) : void {
+        global $DB;
+
+        $mutedconversation = new \stdClass();
+        $mutedconversation->userid = $userid;
+        $mutedconversation->conversationid = $conversationid;
+        $mutedconversation->action = self::CONVERSATION_ACTION_MUTED;
+        $mutedconversation->timecreated = time();
+
+        $DB->insert_record('message_conversation_actions', $mutedconversation);
+    }
+
+    /**
+     * Handles unmuting a conversation.
+     *
+     * @param int $userid The id of the user
+     * @param int $conversationid The id of the conversation
+     */
+    public static function unmute_conversation(int $userid, int $conversationid) : void {
+        global $DB;
+
+        $DB->delete_records('message_conversation_actions',
+            [
+                'userid' => $userid,
+                'conversationid' => $conversationid,
+                'action' => self::CONVERSATION_ACTION_MUTED
+            ]
+        );
+    }
+
+    /**
+     * Checks whether a conversation is muted or not.
+     *
+     * @param int $userid The id of the user
+     * @param int $conversationid The id of the conversation
+     * @return bool Whether or not the conversation is muted or not
+     */
+    public static function is_conversation_muted(int $userid, int $conversationid) : bool {
+        global $DB;
+
+        return $DB->record_exists('message_conversation_actions',
+            [
+                'userid' => $userid,
+                'conversationid' => $conversationid,
+                'action' => self::CONVERSATION_ACTION_MUTED
+            ]
+        );
+    }
 }
index f3f534b..ce8ea8d 100644 (file)
@@ -4912,6 +4912,84 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertEquals(0, $DB->count_records('message_users_blocked'));
     }
 
+    /**
+     * Test muting a conversation.
+     */
+    public function test_mute_conversation() {
+        global $DB;
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $conversation = \core_message\api::create_conversation(
+            \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [
+                $user1->id,
+                $user2->id
+            ]
+        );
+        $conversationid = $conversation->id;
+
+        \core_message\api::mute_conversation($user1->id, $conversationid);
+
+        $mutedconversation = $DB->get_records('message_conversation_actions');
+
+        $this->assertCount(1, $mutedconversation);
+
+        $mutedconversation = reset($mutedconversation);
+
+        $this->assertEquals($user1->id, $mutedconversation->userid);
+        $this->assertEquals($conversationid, $mutedconversation->conversationid);
+        $this->assertEquals(\core_message\api::CONVERSATION_ACTION_MUTED, $mutedconversation->action);
+    }
+
+    /**
+     * Test unmuting a conversation.
+     */
+    public function test_unmute_conversation() {
+        global $DB;
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $conversation = \core_message\api::create_conversation(
+            \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [
+                $user1->id,
+                $user2->id
+            ]
+        );
+        $conversationid = $conversation->id;
+
+        \core_message\api::mute_conversation($user1->id, $conversationid);
+        \core_message\api::unmute_conversation($user1->id, $conversationid);
+
+        $this->assertEquals(0, $DB->count_records('message_conversation_actions'));
+    }
+
+    /**
+     * Test if a conversation is muted.
+     */
+    public function test_is_conversation_muted() {
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $conversation = \core_message\api::create_conversation(
+            \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [
+                $user1->id,
+                $user2->id
+            ]
+        );
+        $conversationid = $conversation->id;
+
+        $this->assertFalse(\core_message\api::is_conversation_muted($user1->id, $conversationid));
+
+        \core_message\api::mute_conversation($user1->id, $conversationid);
+
+        $this->assertTrue(\core_message\api::is_conversation_muted($user1->id, $conversationid));
+    }
+
     /**
      * Test is contact check.
      */