Skip to content

Commit

Permalink
MDL-36941 core: added 'convhash' field to quickly get conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Mar 23, 2018
1 parent 0d51ae7 commit b2cd17e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 42 deletions.
4 changes: 4 additions & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,15 @@
<TABLE NAME="message_conversations" COMMENT="Stores all message conversations">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="convhash" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="convhash" UNIQUE="true" FIELDS="convhash"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_conversation_members" COMMENT="Stores all members in a conversations">
<FIELDS>
Expand Down
20 changes: 20 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2131,5 +2131,25 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018032200.01);
}

if ($oldversion < 2018032200.04) {
// Define table 'message_conversations' to be updated.
$table = new xmldb_table('message_conversations');
$field = new xmldb_field('convhash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, 0, 'id');

// Conditionally launch add field 'convhash'.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Conditionally launch add index.
$index = new xmldb_index('convhash', XMLDB_INDEX_UNIQUE, array('convhash'));
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2018032200.04);
}

return true;
}
8 changes: 4 additions & 4 deletions lib/messagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ function message_send($eventdata) {
} else {
$table = 'messages';

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);
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]);
}

$tabledata = new stdClass();
Expand Down
43 changes: 15 additions & 28 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public static function can_delete_conversation($userid) {
public static function delete_conversation($userid, $otheruserid) {
global $DB, $USER;

$conversationid = self::get_conversation_between_users($userid, $otheruserid);
$conversationid = self::get_conversation_between_users([$userid, $otheruserid]);

// If there is no conversation, there is nothing to do.
if (!$conversationid) {
Expand Down Expand Up @@ -1202,24 +1202,15 @@ public static function delete_message($userid, $messageid) {
/**
* Returns the conversation between two users.
*
* @param int $userid1 The userid of the first user
* @param int $userid2 The userid of the second user
* @param array $userids
* @return int|bool The id of the conversation, false if not found
*/
public static function get_conversation_between_users($userid1, $userid2) {
public static function get_conversation_between_users(array $userids) {
global $DB;

$sql = "SELECT DISTINCT mc.id
FROM {message_conversations} mc
INNER JOIN {message_conversation_members} mcm
ON mcm.conversationid = mc.id
INNER JOIN {message_conversation_members} mcm2
ON mcm2.conversationid = mc.id
WHERE mcm.userid = :userid1
AND mcm2.userid = :userid2
AND mcm.id != mcm2.id";
$hash = helper::get_conversation_hash($userids);

if ($conversation = $DB->get_record_sql($sql, ['userid1' => $userid1, 'userid2' => $userid2])) {
if ($conversation = $DB->get_record('message_conversations', ['convhash' => $hash])) {
return $conversation->id;
}

Expand All @@ -1229,29 +1220,25 @@ public static function get_conversation_between_users($userid1, $userid2) {
/**
* Creates a conversation between two users.
*
* @param int $userid1 The userid of the first user
* @param int $userid2 The userid of the second user
* @param array $userids
* @return int The id of the conversation
*/
public static function create_conversation_between_users($userid1, $userid2) {
public static function create_conversation_between_users(array $userids) {
global $DB;

$conversation = new \stdClass();
$conversation->convhash = helper::get_conversation_hash($userids);
$conversation->timecreated = time();
$conversation->id = $DB->insert_record('message_conversations', $conversation);

// Add members to this conversation.
$member = new \stdClass();
$member->conversationid = $conversation->id;
$member->userid = $userid1;
$member->timecreated = time();
$DB->insert_record('message_conversation_members', $member);

$member = new \stdClass();
$member->conversationid = $conversation->id;
$member->userid = $userid2;
$member->timecreated = time();
$DB->insert_record('message_conversation_members', $member);
foreach ($userids as $userid) {
$member = new \stdClass();
$member->conversationid = $conversation->id;
$member->userid = $userid;
$member->timecreated = time();
$DB->insert_record('message_conversation_members', $member);
}

return $conversation->id;
}
Expand Down
12 changes: 12 additions & 0 deletions message/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ public static function togglecontact_link_params($user, $iscontact = false) {
return $params;
}

/**
* Returns the conversation hash between users for easy look-ups in the DB.
*
* @param array $userids
* @return string
*/
public static function get_conversation_hash(array $userids) {
sort($userids);

return sha1(implode('-', $userids));
}

/**
* Returns the cache key for the time created value of the last message between two users.
*
Expand Down
6 changes: 3 additions & 3 deletions message/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ public function test_get_conversation_between_users_no_conversation() {
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();

$this->assertFalse(\core_message\api::get_conversation_between_users($user1->id, $user2->id));
$this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
}

/**
Expand All @@ -1931,9 +1931,9 @@ public function test_get_conversation_between_users_with_existing_conversation()
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();

$conversationid = \core_message\api::create_conversation_between_users($user1->id, $user2->id);
$conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);

$this->assertEquals($conversationid,
\core_message\api::get_conversation_between_users($user1->id, $user2->id));
\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
}
}
6 changes: 3 additions & 3 deletions message/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ protected function send_message($userfrom, $userto, $message = 'Hello world!', $
return $DB->insert_record('notifications', $record);
}

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);
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]);
}

// Ok, send the message.
Expand Down
6 changes: 3 additions & 3 deletions message/tests/messagelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ protected function send_fake_message($userfrom, $userto, $message = 'Hello world
return $DB->insert_record('notifications', $record);
}

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);
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]);
}

// Ok, send the message.
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2018032200.03; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2018032200.04; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit b2cd17e

Please sign in to comment.