Merge branch 'MDL-65566-master-jake' of https://github.com/snake/moodle
This commit is contained in:
@@ -3135,59 +3135,6 @@ function xmldb_main_upgrade($oldversion) {
|
||||
}
|
||||
$legacyselfmessagesrs->close();
|
||||
|
||||
// STEP 3. For existing users without self-conversations, create and star it.
|
||||
|
||||
// Get all the users without a self-conversation.
|
||||
$sql = "SELECT u.id
|
||||
FROM {user} u
|
||||
WHERE u.deleted = 0 AND u.id NOT IN (SELECT mcm.userid
|
||||
FROM {message_conversation_members} mcm
|
||||
INNER JOIN {message_conversations} mc
|
||||
ON mc.id = mcm.conversationid AND mc.type = ?
|
||||
)";
|
||||
$useridsrs = $DB->get_recordset_sql($sql, [\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF]);
|
||||
// Create the self-conversation for all these users.
|
||||
foreach ($useridsrs as $user) {
|
||||
$conditions = [
|
||||
'type' => \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
'convhash' => \core_message\helper::get_conversation_hash([$user->id])
|
||||
];
|
||||
$selfconversation = $DB->get_record('message_conversations', $conditions);
|
||||
if (empty($selfconversation)) {
|
||||
// Create the self-conversation.
|
||||
$selfconversation = new \stdClass();
|
||||
$selfconversation->type = \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF;
|
||||
$selfconversation->convhash = \core_message\helper::get_conversation_hash([$user->id]);
|
||||
$selfconversation->enabled = 1;
|
||||
$selfconversation->timecreated = time();
|
||||
$selfconversation->timemodified = $selfconversation->timecreated;
|
||||
|
||||
$selfconversation->id = $DB->insert_record('message_conversations', $selfconversation);
|
||||
|
||||
// Add user to this self-conversation.
|
||||
$member = new \stdClass();
|
||||
$member->conversationid = $selfconversation->id;
|
||||
$member->userid = $user->id;
|
||||
$member->timecreated = time();
|
||||
|
||||
$member->id = $DB->insert_record('message_conversation_members', $member);
|
||||
|
||||
// Star the self-conversation.
|
||||
$favouriterecord = new \stdClass();
|
||||
$favouriterecord->component = 'core_message';
|
||||
$favouriterecord->itemtype = 'message_conversations';
|
||||
$favouriterecord->itemid = $selfconversation->id;
|
||||
$userctx = \context_user::instance($user->id);
|
||||
$favouriterecord->contextid = $userctx->id;
|
||||
$favouriterecord->userid = $user->id;
|
||||
$favouriterecord->timecreated = time();
|
||||
$favouriterecord->timemodified = $favouriterecord->timecreated;
|
||||
|
||||
$DB->insert_record('favourite', $favouriterecord);
|
||||
}
|
||||
}
|
||||
$useridsrs->close();
|
||||
|
||||
// Main savepoint reached.
|
||||
upgrade_main_savepoint(true, 2019041800.01);
|
||||
}
|
||||
|
||||
@@ -528,6 +528,8 @@ class api {
|
||||
throw new \moodle_exception("Invalid value ($type) for type param, please see api constants.");
|
||||
}
|
||||
|
||||
self::lazy_create_self_conversation($userid);
|
||||
|
||||
// We need to know which conversations are favourites, so we can either:
|
||||
// 1) Include the 'isfavourite' attribute on conversations (when $favourite = null and we're including all conversations)
|
||||
// 2) Restrict the results to ONLY those conversations which are favourites (when $favourite = true)
|
||||
@@ -1586,6 +1588,7 @@ class api {
|
||||
*/
|
||||
public static function get_conversation_counts(int $userid) : array {
|
||||
global $DB;
|
||||
self::lazy_create_self_conversation($userid);
|
||||
|
||||
// Some restrictions we need to be aware of:
|
||||
// - Individual conversations containing soft-deleted user must be counted.
|
||||
@@ -2451,6 +2454,7 @@ class api {
|
||||
*/
|
||||
public static function get_self_conversation(int $userid) {
|
||||
global $DB;
|
||||
self::lazy_create_self_conversation($userid);
|
||||
|
||||
$conditions = [
|
||||
'type' => self::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
@@ -2525,6 +2529,12 @@ class api {
|
||||
$conversation->convhash = null;
|
||||
if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL || $type == self::MESSAGE_CONVERSATION_TYPE_SELF) {
|
||||
$conversation->convhash = helper::get_conversation_hash($userids);
|
||||
|
||||
// Don't blindly create a conversation between 2 users if there is already one present - return that.
|
||||
// This stops us making duplicate self and individual conversations, which is invalid.
|
||||
if ($record = $DB->get_record('message_conversations', ['convhash' => $conversation->convhash])) {
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
$conversation->component = $component;
|
||||
$conversation->itemtype = $itemtype;
|
||||
@@ -3396,4 +3406,25 @@ class api {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a self conversation for a user, only if one doesn't already exist.
|
||||
*
|
||||
* @param int $userid the user to whom the conversation belongs.
|
||||
*/
|
||||
protected static function lazy_create_self_conversation(int $userid) : void {
|
||||
global $DB;
|
||||
// Check if the self-conversation for this user exists.
|
||||
// If not, create and star it for the user.
|
||||
// Don't use the API methods here, as they in turn may rely on
|
||||
// lazy creation and we'll end up with recursive loops of doom.
|
||||
$conditions = [
|
||||
'type' => self::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
'convhash' => helper::get_conversation_hash([$userid])
|
||||
];
|
||||
if (empty($DB->get_record('message_conversations', $conditions))) {
|
||||
$selfconversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_SELF, [$userid]);
|
||||
self::set_favourite_conversation($selfconversation->id, $userid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -827,8 +827,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
$this->assertEquals($user2->id, $member->id);
|
||||
|
||||
\core_message\api::delete_all_conversation_data($rsc3->id);
|
||||
$rsc3 = \core_message\api::get_self_conversation($user3->id);
|
||||
$this->assertFalse($rsc3);
|
||||
$selfconversation = \core_message\api::get_self_conversation($user3->id);
|
||||
$members = \core_message\api::get_conversation_members($user1->id, $selfconversation->id);
|
||||
$this->assertCount(1, $members);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2266,6 +2267,22 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that creation can't create the same conversation twice for 1:1 conversations.
|
||||
*/
|
||||
public function test_create_conversation_duplicate_conversations() {
|
||||
global $DB;
|
||||
$user1 = $this::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, [$user1->id]);
|
||||
\core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, [$user1->id]);
|
||||
|
||||
$convhash = \core_message\helper::get_conversation_hash([$user1->id]);
|
||||
$countconversations = $DB->count_records('message_conversations', ['convhash' => $convhash]);
|
||||
$this->assertEquals(1, $countconversations);
|
||||
$this->assertNotEmpty($conversation = \core_message\api::get_self_conversation($user1->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_conversations with a mixture of messages.
|
||||
*
|
||||
@@ -6717,6 +6734,17 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
$messageids[] = testhelper::send_fake_message_to_conversation($userfrom, $conversation->id);
|
||||
}
|
||||
|
||||
// Remove the self conversations created by the generator,
|
||||
// so we can choose to set that ourself and honour the original intention of the test.
|
||||
$userids = array_map(function($userindex) use ($users) {
|
||||
return $users[$userindex]->id;
|
||||
}, $config['users']);
|
||||
foreach ($userids as $userid) {
|
||||
if ($conversation->type == \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF) {
|
||||
\core_message\api::unset_favourite_conversation($conversation->id, $userid);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($config['favourites'] as $userfromindex) {
|
||||
$userfrom = $users[$userfromindex];
|
||||
$usercontext = \context_user::instance($userfrom->id);
|
||||
|
||||
@@ -6523,13 +6523,14 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Some random conversation.
|
||||
$otherconversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[
|
||||
$user1->id,
|
||||
$user2->id,
|
||||
$user3->id,
|
||||
]
|
||||
);
|
||||
|
||||
@@ -7039,6 +7040,17 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
$messageids[] = testhelper::send_fake_message_to_conversation($userfrom, $conversation->id);
|
||||
}
|
||||
|
||||
// Remove the self conversations created by the generator,
|
||||
// so we can choose to set that ourself and honour the original intention of the test.
|
||||
$userids = array_map(function($userindex) use ($users) {
|
||||
return $users[$userindex]->id;
|
||||
}, $config['users']);
|
||||
foreach ($userids as $userid) {
|
||||
if ($conversation->type == \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF) {
|
||||
\core_message\api::unset_favourite_conversation($conversation->id, $userid);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($config['favourites'] as $userfromindex) {
|
||||
$userfrom = $users[$userfromindex];
|
||||
$usercontext = \context_user::instance($userfrom->id);
|
||||
|
||||
@@ -126,10 +126,6 @@ function user_create_user($user, $updatepassword = true, $triggerevent = true) {
|
||||
\core\event\user_created::create_from_userid($newuserid)->trigger();
|
||||
}
|
||||
|
||||
// All new users must have a starred self-conversation.
|
||||
$selfconversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, [$newuserid]);
|
||||
\core_message\api::set_favourite_conversation($selfconversation->id, $newuserid);
|
||||
|
||||
// Purge the associated caches for the current user only.
|
||||
$presignupcache = \cache::make('core', 'presignup');
|
||||
$presignupcache->purge_current_user();
|
||||
|
||||
Reference in New Issue
Block a user