MDL-63691 core_message: added api::create_conversation
authorMark Nelson <markn@moodle.com>
Thu, 18 Oct 2018 06:23:19 +0000 (14:23 +0800)
committerMark Nelson <markn@moodle.com>
Mon, 22 Oct 2018 02:21:28 +0000 (10:21 +0800)
lib/messagelib.php
message/classes/api.php
message/classes/task/migrate_message_data.php
message/tests/api_test.php
message/tests/externallib_test.php
message/tests/messagelib_test.php
message/tests/privacy_provider_test.php

index 16ccefe..b808981 100644 (file)
@@ -163,8 +163,14 @@ function message_send(\core\message\message $eventdata) {
 
         if (!$conversationid = \core_message\api::get_conversation_between_users([$eventdata->userfrom->id,
                 $eventdata->userto->id])) {
-            $conversationid = \core_message\api::create_conversation_between_users([$eventdata->userfrom->id,
-                $eventdata->userto->id]);
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [
+                    $eventdata->userfrom->id,
+                    $eventdata->userto->id
+                ]
+            );
+            $conversationid = $conversation->id;
         }
 
         $tabledata = new stdClass();
index 317d40d..df6b606 100644 (file)
@@ -61,6 +61,16 @@ class api {
      */
     const MESSAGE_PRIVACY_SITE = 2;
 
+    /**
+     * An individual conversation.
+     */
+    const MESSAGE_CONVERSATION_TYPE_INDIVIDUAL = 1;
+
+    /**
+     * A group conversation.
+     */
+    const MESSAGE_CONVERSATION_TYPE_GROUP = 2;
+
     /**
      * Handles searching for messages in the message area.
      *
@@ -1348,7 +1358,11 @@ class api {
 
         $hash = helper::get_conversation_hash($userids);
 
-        if ($conversation = $DB->get_record('message_conversations', ['convhash' => $hash])) {
+        $params = [
+            'type' => self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            'convhash' => $hash
+        ];
+        if ($conversation = $DB->get_record('message_conversations', $params)) {
             return $conversation->id;
         }
 
@@ -1366,23 +1380,55 @@ class api {
         debugging('\core_message\api::create_conversation_between_users is deprecated, please use ' .
             '\core_message\api::create_conversation instead.', DEBUG_DEVELOPER);
 
+        // This method was always used for individual conversations.
+        $conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
+
+        return $conversation->id;
+    }
+
+    /**
+     * Creates a conversation with selected users and messages.
+     *
+     * @param int $type The type of conversation
+     * @param int[] $userids The array of users to add to the conversation
+     * @param string $name The name of the conversation
+     * @return \stdClass
+     */
+    public static function create_conversation(int $type, array $userids, string $name = null) {
         global $DB;
 
+        // Sanity check.
+        if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
+            if (count($userids) > 2) {
+                throw new \moodle_exception('An individual conversation can not have more than two users.');
+            }
+        }
+
         $conversation = new \stdClass();
-        $conversation->convhash = helper::get_conversation_hash($userids);
+        $conversation->type = $type;
+        $conversation->name = $name;
+        $conversation->convhash = null;
+        if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
+            $conversation->convhash = helper::get_conversation_hash($userids);
+        }
         $conversation->timecreated = time();
         $conversation->id = $DB->insert_record('message_conversations', $conversation);
 
-        // Add members to this conversation.
+        // Add users to this conversation.
+        $arrmembers = [];
         foreach ($userids as $userid) {
             $member = new \stdClass();
             $member->conversationid = $conversation->id;
             $member->userid = $userid;
             $member->timecreated = time();
-            $DB->insert_record('message_conversation_members', $member);
+            $member->id = $DB->insert_record('message_conversation_members', $member);
+
+            $arrmembers[] = $member;
         }
 
-        return $conversation->id;
+        $conversation->members = $arrmembers;
+
+        return $conversation;
     }
 
     /**
index 34d481f..53e2182 100644 (file)
@@ -123,7 +123,14 @@ class migrate_message_data extends \core\task\adhoc_task {
         global $DB;
 
         if (!$conversationid = \core_message\api::get_conversation_between_users([$userid, $otheruserid])) {
-            $conversationid = \core_message\api::create_conversation_between_users([$userid, $otheruserid]);
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [
+                    $userid,
+                    $otheruserid
+                ]
+            );
+            $conversationid = $conversation->id;
         }
 
         // First, get the rows from the 'message' table.
index d67f363..4b04fc4 100644 (file)
@@ -2127,6 +2127,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $user2 = self::getDataGenerator()->create_user();
 
         $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
+        $this->assertDebuggingCalled();
 
         $this->assertEquals($conversationid,
             \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
@@ -2398,7 +2399,14 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $user1 = self::getDataGenerator()->create_user();
         $user2 = self::getDataGenerator()->create_user();
 
-        $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
+        $conversation = \core_message\api::create_conversation(
+            \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [
+                $user1->id,
+                $user2->id
+            ]
+        );
+        $conversationid = $conversation->id;
 
         $this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $conversationid));
     }
@@ -2411,7 +2419,14 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $user2 = self::getDataGenerator()->create_user();
         $user3 = self::getDataGenerator()->create_user();
 
-        $conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
+        $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_user_in_conversation($user3->id, $conversationid));
     }
@@ -2445,6 +2460,84 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertTrue(\core_message\api::can_create_group_conversation($teacher->id, $coursecontext));
     }
 
+    /**
+     * Test creating an individual conversation.
+     */
+    public function test_create_conversation_individual() {
+        $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
+            ],
+            'A conversation name'
+        );
+
+        $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $conversation->type);
+        $this->assertEquals('A conversation name', $conversation->name);
+        $this->assertEquals(\core_message\helper::get_conversation_hash([$user1->id, $user2->id]), $conversation->convhash);
+
+        $this->assertCount(2, $conversation->members);
+
+        $member1 = array_shift($conversation->members);
+        $member2 = array_shift($conversation->members);
+
+        $this->assertEquals($user1->id, $member1->userid);
+        $this->assertEquals($conversation->id, $member1->conversationid);
+
+        $this->assertEquals($user2->id, $member2->userid);
+        $this->assertEquals($conversation->id, $member2->conversationid);
+    }
+
+    /**
+     * Test creating a group conversation.
+     */
+    public function test_create_conversation_group() {
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+        $user3 = self::getDataGenerator()->create_user();
+
+        $conversation = \core_message\api::create_conversation(
+            \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
+            [
+                $user1->id,
+                $user2->id,
+                $user3->id
+            ],
+            'A conversation name'
+        );
+
+        $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $conversation->type);
+        $this->assertEquals('A conversation name', $conversation->name);
+        $this->assertNull($conversation->convhash);
+
+        $this->assertCount(3, $conversation->members);
+
+        $member1 = array_shift($conversation->members);
+        $member2 = array_shift($conversation->members);
+        $member3 = array_shift($conversation->members);
+
+        $this->assertEquals($user1->id, $member1->userid);
+        $this->assertEquals($conversation->id, $member1->conversationid);
+
+        $this->assertEquals($user2->id, $member2->userid);
+        $this->assertEquals($conversation->id, $member2->conversationid);
+
+        $this->assertEquals($user3->id, $member3->userid);
+        $this->assertEquals($conversation->id, $member3->conversationid);
+    }
+
+    /**
+     * Test creating an individual conversation with too many members.
+     */
+    public function test_create_conversation_individual_too_many_members() {
+        $this->expectException('moodle_exception');
+        \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, [1, 2, 3]);
+    }
+
     /**
      * Comparison function for sorting contacts.
      *
index aa7f61f..2a929d7 100644 (file)
@@ -74,8 +74,14 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         }
 
         if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
-            $conversationid = \core_message\api::create_conversation_between_users([$userfrom->id,
-                $userto->id]);
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [
+                    $userfrom->id,
+                    $userto->id
+                ]
+            );
+            $conversationid = $conversation->id;
         }
 
         // Ok, send the message.
index 0aaac07..dbc7a0b 100644 (file)
@@ -86,8 +86,14 @@ class core_message_messagelib_testcase extends advanced_testcase {
         }
 
         if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
-            $conversationid = \core_message\api::create_conversation_between_users([$userfrom->id,
-                $userto->id]);
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [
+                    $userfrom->id,
+                    $userto->id
+                ]
+            );
+            $conversationid = $conversation->id;
         }
 
         // Ok, send the message.
index 27e2232..2e15a7b 100644 (file)
@@ -663,8 +663,14 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         }
 
         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
-            $conversationid = \core_message\api::create_conversation_between_users([$useridfrom,
-                $useridto]);
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [
+                    $useridfrom,
+                    $useridto
+                ]
+            );
+            $conversationid = $conversation->id;
         }
 
         // Ok, send the message.