mirror of
https://github.com/moodle/moodle.git
synced 2025-04-20 07:56:06 +02:00
MDL-63213 core_message: add web services for set/unset favourites
This commit is contained in:
parent
b8ff2c4481
commit
5b367baef8
@ -1154,6 +1154,22 @@ $functions = array(
|
||||
'capabilities' => 'moodle/user:editownmessageprofile',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_set_favourite_conversations' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'set_favourite_conversations',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Mark a conversation or group of conversations as favourites/starred conversations.',
|
||||
'type' => 'write',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_unset_favourite_conversations' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'unset_favourite_conversations',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Unset a conversation or group of conversations as favourites/starred conversations.',
|
||||
'type' => 'write',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_notes_create_notes' => array(
|
||||
'classname' => 'core_notes_external',
|
||||
'methodname' => 'create_notes',
|
||||
|
@ -3137,4 +3137,122 @@ class core_message_external extends external_api {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters for the favourite_conversations() method.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function set_favourite_conversations_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_DEFAULT, 0),
|
||||
'conversations' => new external_multiple_structure(
|
||||
new external_value(PARAM_INT, 'id of the conversation', VALUE_DEFAULT, 0)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Favourite a conversation, or list of conversations for a user.
|
||||
*
|
||||
* @param int $userid the id of the user, or 0 for the current user.
|
||||
* @param array $conversationids the list of conversations ids to favourite.
|
||||
* @return array
|
||||
* @throws moodle_exception if messaging is disabled or if the user cannot perform the action.
|
||||
*/
|
||||
public static function set_favourite_conversations(int $userid, array $conversationids) {
|
||||
global $CFG, $USER;
|
||||
|
||||
// All the business logic checks that really shouldn't be in here.
|
||||
if (empty($CFG->messaging)) {
|
||||
throw new moodle_exception('disabled', 'message');
|
||||
}
|
||||
$params = [
|
||||
'userid' => $userid,
|
||||
'conversations' => $conversationids
|
||||
];
|
||||
$params = self::validate_parameters(self::set_favourite_conversations_parameters(), $params);
|
||||
$systemcontext = context_system::instance();
|
||||
self::validate_context($systemcontext);
|
||||
|
||||
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
|
||||
throw new moodle_exception('You do not have permission to perform this action.');
|
||||
}
|
||||
|
||||
foreach ($params['conversations'] as $conversationid) {
|
||||
\core_message\api::set_favourite_conversation($conversationid, $params['userid']);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a description of the returns for the create_user_favourite_conversations() method.
|
||||
*
|
||||
* @return external_description
|
||||
*/
|
||||
public static function set_favourite_conversations_returns() {
|
||||
return new external_warnings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters for unfavourite_conversations() method.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function unset_favourite_conversations_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_DEFAULT, 0),
|
||||
'conversations' => new external_multiple_structure(
|
||||
new external_value(PARAM_INT, 'id of the conversation', VALUE_DEFAULT, 0)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unfavourite a conversation, or list of conversations for a user.
|
||||
*
|
||||
* @param int $userid the id of the user, or 0 for the current user.
|
||||
* @param array $conversationids the list of conversations ids unset as favourites.
|
||||
* @return array
|
||||
* @throws moodle_exception if messaging is disabled or if the user cannot perform the action.
|
||||
*/
|
||||
public static function unset_favourite_conversations(int $userid, array $conversationids) {
|
||||
global $CFG, $USER;
|
||||
|
||||
// All the business logic checks that really shouldn't be in here.
|
||||
if (empty($CFG->messaging)) {
|
||||
throw new moodle_exception('disabled', 'message');
|
||||
}
|
||||
$params = [
|
||||
'userid' => $userid,
|
||||
'conversations' => $conversationids
|
||||
];
|
||||
$params = self::validate_parameters(self::unset_favourite_conversations_parameters(), $params);
|
||||
$systemcontext = context_system::instance();
|
||||
self::validate_context($systemcontext);
|
||||
|
||||
if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
|
||||
throw new moodle_exception('You do not have permission to perform this action.');
|
||||
}
|
||||
|
||||
foreach ($params['conversations'] as $conversationid) {
|
||||
\core_message\api::unset_favourite_conversation($conversationid, $params['userid']);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset favourite conversations return description.
|
||||
*
|
||||
* @return external_description
|
||||
*/
|
||||
public static function unset_favourite_conversations_returns() {
|
||||
return new external_warnings();
|
||||
}
|
||||
}
|
||||
|
@ -3752,4 +3752,199 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
protected static function sort_contacts($a, $b) {
|
||||
return $a['userid'] > $b['userid'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test verifying that conversations can be marked as favourite conversations.
|
||||
*/
|
||||
public function test_set_favourite_conversations_basic() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setUser($user1);
|
||||
|
||||
// Now, create some conversations.
|
||||
$time = time();
|
||||
$this->send_message($user1, $user2, 'Yo!', 0, $time);
|
||||
$this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
|
||||
$this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
|
||||
|
||||
$this->send_message($user1, $user3, 'Booyah');
|
||||
$this->send_message($user3, $user1, 'Whaaat?');
|
||||
$this->send_message($user1, $user3, 'Nothing.');
|
||||
|
||||
$this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
|
||||
$this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
|
||||
|
||||
// Favourite 2 conversations as user 1.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
$conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
|
||||
$result = core_message_external::set_favourite_conversations($user1->id, [$conversation1, $conversation2]);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::set_favourite_conversations_returns(), $result);
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can't favourite a conversation on behalf of another user.
|
||||
*/
|
||||
public function test_set_favourite_conversations_another_users_conversation() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setUser($user3);
|
||||
|
||||
// Now, create some conversations.
|
||||
$time = time();
|
||||
$this->send_message($user1, $user2, 'Yo!', 0, $time);
|
||||
$this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
|
||||
$this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
|
||||
|
||||
$this->send_message($user1, $user3, 'Booyah');
|
||||
$this->send_message($user3, $user1, 'Whaaat?');
|
||||
$this->send_message($user1, $user3, 'Nothing.');
|
||||
|
||||
// Try to favourite conversation 1 for user 2, as user3.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$result = core_message_external::set_favourite_conversations($user2->id, [$conversation1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can't mark a conversation as their own favourite if it's a conversation they're not a member of.
|
||||
*/
|
||||
public function test_set_favourite_conversations_non_member() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setUser($user3);
|
||||
|
||||
// Now, create some conversations.
|
||||
$time = time();
|
||||
$this->send_message($user1, $user2, 'Yo!', 0, $time);
|
||||
$this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
|
||||
$this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
|
||||
|
||||
$this->send_message($user1, $user3, 'Booyah');
|
||||
$this->send_message($user3, $user1, 'Whaaat?');
|
||||
$this->send_message($user1, $user3, 'Nothing.');
|
||||
|
||||
// Try to favourite conversation 1 as user 3.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
$conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$result = core_message_external::set_favourite_conversations($user3->id, [$conversation1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can't favourite a non-existent conversation.
|
||||
*/
|
||||
public function test_set_favourite_conversations_non_existent_conversation() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$this->setUser($user1);
|
||||
|
||||
// Try to favourite a non-existent conversation.
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$result = core_message_external::set_favourite_conversations($user1->id, [0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can unset a favourite conversation, or list of favourite conversations.
|
||||
*/
|
||||
public function test_unset_favourite_conversations_basic() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setUser($user1);
|
||||
|
||||
// Now, create some conversations.
|
||||
$time = time();
|
||||
$this->send_message($user1, $user2, 'Yo!', 0, $time);
|
||||
$this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
|
||||
$this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
|
||||
|
||||
$this->send_message($user1, $user3, 'Booyah');
|
||||
$this->send_message($user3, $user1, 'Whaaat?');
|
||||
$this->send_message($user1, $user3, 'Nothing.');
|
||||
|
||||
$this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
|
||||
$this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
|
||||
|
||||
// Favourite 2 conversations as user 1.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
$conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
|
||||
\core_message\api::set_favourite_conversation($conversation1, $user1->id);
|
||||
\core_message\api::set_favourite_conversation($conversation2, $user1->id);
|
||||
$this->assertCount(2, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
|
||||
|
||||
// Now, using the web service, unset the favourite conversations.
|
||||
$result = core_message_external::unset_favourite_conversations($user1->id, [$conversation1, $conversation2]);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::unset_favourite_conversations_returns(), $result);
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can't unfavourite a conversation for another user.
|
||||
*/
|
||||
public function test_unset_favourite_conversations_another_users_conversation() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setUser($user3);
|
||||
|
||||
// Now, create some conversations.
|
||||
$time = time();
|
||||
$this->send_message($user1, $user2, 'Yo!', 0, $time);
|
||||
$this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
|
||||
$this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
|
||||
|
||||
$this->send_message($user1, $user3, 'Booyah');
|
||||
$this->send_message($user3, $user1, 'Whaaat?');
|
||||
$this->send_message($user1, $user3, 'Nothing.');
|
||||
|
||||
// Favourite conversation 1 for user1. The current user ($USER) isn't checked for this action.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
\core_message\api::set_favourite_conversation($conversation1, $user1->id);
|
||||
$this->assertCount(1, \core_message\api::get_conversations($user1->id, 0, 20, null, true));
|
||||
|
||||
// Try to unfavourite conversation 1 for user 2, as user3.
|
||||
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$result = core_message_external::unset_favourite_conversations($user2->id, [$conversation1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that a user can't unfavourite a non-existent conversation.
|
||||
*/
|
||||
public function test_unset_favourite_conversations_non_existent_conversation() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$this->setUser($user1);
|
||||
|
||||
// Try to unfavourite a non-existent conversation.
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$result = core_message_external::unset_favourite_conversations($user1->id, [0]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user