MDL-63547 core_message: web service can delete multiple conversations
This commit is contained in:
+3
-3
@@ -924,11 +924,11 @@ $functions = array(
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_delete_conversation_by_id' => array(
|
||||
'core_message_delete_conversations_by_id' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'delete_conversation_by_id',
|
||||
'methodname' => 'delete_conversations_by_id',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Deletes a conversation.',
|
||||
'description' => 'Deletes a list of conversations.',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'moodle/site:deleteownmessage',
|
||||
'ajax' => true,
|
||||
|
||||
+16
-11
@@ -2639,11 +2639,14 @@ class core_message_external extends external_api {
|
||||
* @return external_function_parameters
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function delete_conversation_by_id_parameters() {
|
||||
public static function delete_conversations_by_id_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'),
|
||||
'conversationid' => new external_value(PARAM_INT, 'The id of the conversation'),
|
||||
'conversationids' => new external_multiple_structure(
|
||||
new external_value(PARAM_INT, 'The id of the conversation'),
|
||||
'List of conversation IDs'
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -2652,12 +2655,12 @@ class core_message_external extends external_api {
|
||||
* Deletes a conversation.
|
||||
*
|
||||
* @param int $userid The user id of who we want to delete the conversation for
|
||||
* @param int $conversationid The id of the conversation
|
||||
* @param int[] $conversationids The ids of the conversations
|
||||
* @return array
|
||||
* @throws moodle_exception
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function delete_conversation_by_id($userid, $conversationid) {
|
||||
public static function delete_conversations_by_id($userid, array $conversationids) {
|
||||
global $CFG;
|
||||
|
||||
// Check if private messaging between users is allowed.
|
||||
@@ -2668,9 +2671,9 @@ class core_message_external extends external_api {
|
||||
// Validate params.
|
||||
$params = [
|
||||
'userid' => $userid,
|
||||
'conversationid' => $conversationid,
|
||||
'conversationids' => $conversationids,
|
||||
];
|
||||
$params = self::validate_parameters(self::delete_conversation_by_id_parameters(), $params);
|
||||
$params = self::validate_parameters(self::delete_conversations_by_id_parameters(), $params);
|
||||
|
||||
// Validate context.
|
||||
$context = context_system::instance();
|
||||
@@ -2679,10 +2682,12 @@ class core_message_external extends external_api {
|
||||
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
|
||||
core_user::require_active_user($user);
|
||||
|
||||
if (\core_message\api::can_delete_conversation($user->id, $conversationid)) {
|
||||
\core_message\api::delete_conversation_by_id($user->id, $conversationid);
|
||||
} else {
|
||||
throw new moodle_exception("You do not have permission to delete the conversation '$conversationid'");
|
||||
foreach ($conversationids as $conversationid) {
|
||||
if (\core_message\api::can_delete_conversation($user->id, $conversationid)) {
|
||||
\core_message\api::delete_conversation_by_id($user->id, $conversationid);
|
||||
} else {
|
||||
throw new moodle_exception("You do not have permission to delete the conversation '$conversationid'");
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
@@ -2694,7 +2699,7 @@ class core_message_external extends external_api {
|
||||
* @return external_description
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function delete_conversation_by_id_returns() {
|
||||
public static function delete_conversations_by_id_returns() {
|
||||
return new external_warnings();
|
||||
}
|
||||
|
||||
|
||||
@@ -3484,9 +3484,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting conversation.
|
||||
* Test deleting conversations.
|
||||
*/
|
||||
public function test_delete_conversation_by_id() {
|
||||
public function test_delete_conversations_by_id() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
@@ -3508,7 +3508,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
$conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
|
||||
// Delete the conversation.
|
||||
core_message_external::delete_conversation_by_id($user1->id, $conversationid);
|
||||
core_message_external::delete_conversations_by_id($user1->id, [$conversationid]);
|
||||
|
||||
$muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
|
||||
$this->assertCount(4, $muas);
|
||||
@@ -3538,9 +3538,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting conversation as other user.
|
||||
* Test deleting conversations as other user.
|
||||
*/
|
||||
public function test_delete_conversation_by_id_as_other_user() {
|
||||
public function test_delete_conversations_by_id_as_other_user() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
@@ -3561,7 +3561,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
$conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
|
||||
// Delete the conversation.
|
||||
core_message_external::delete_conversation_by_id($user1->id, $conversationid);
|
||||
core_message_external::delete_conversations_by_id($user1->id, [$conversationid]);
|
||||
|
||||
$muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
|
||||
$this->assertCount(4, $muas);
|
||||
@@ -3591,9 +3591,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting conversation as other user without proper capability.
|
||||
* Test deleting conversations as other user without proper capability.
|
||||
*/
|
||||
public function test_delete_conversation_by_id_as_other_user_without_cap() {
|
||||
public function test_delete_conversations_by_id_as_other_user_without_cap() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create some users.
|
||||
@@ -3615,13 +3615,13 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Ensure an exception is thrown.
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::delete_conversation_by_id($user1->id, $conversationid);
|
||||
core_message_external::delete_conversations_by_id($user1->id, [$conversationid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting conversation with messaging disabled.
|
||||
* Test deleting conversations with messaging disabled.
|
||||
*/
|
||||
public function test_delete_conversation_by_id_messaging_disabled() {
|
||||
public function test_delete_conversations_by_id_messaging_disabled() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
@@ -3647,7 +3647,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Ensure an exception is thrown.
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::delete_conversation_by_id($user1->id, $conversationid);
|
||||
core_message_external::delete_conversations_by_id($user1->id, [$conversationid]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ information provided here is intended especially for developers.
|
||||
- core_message_external::block_contacts(), please use core_message_external::block_user() instead.
|
||||
- core_message_external::unblock_contacts(), please use core_message_external::unblock_user() instead.
|
||||
- core_message_external::create_contacts(), please use core_message_external::create_contact_request() instead.
|
||||
- core_message_external::delete_conversation(), please use core_message_external::delete_conversation_by_id() instead.
|
||||
- core_message_external::delete_conversation(), please use core_message_external::delete_conversations_by_id() instead.
|
||||
|
||||
=== 3.5 ===
|
||||
|
||||
|
||||
Reference in New Issue
Block a user