Skip to content

Commit

Permalink
MDL-64773 core_message: added web services to mute/unmute conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Mar 13, 2019
1 parent 2687312 commit 086409f
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,24 @@
'type' => 'write',
'capabilities' => 'moodle/course:managegroups'
),
'core_message_mute_conversations' => array(
'classname' => 'core_message_external',
'methodname' => 'mute_conversations',
'classpath' => 'message/externallib.php',
'description' => 'Mutes a list of conversations',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_unmute_conversations' => array(
'classname' => 'core_message_external',
'methodname' => 'unmute_conversations',
'classpath' => 'message/externallib.php',
'description' => 'Unmutes a list of conversations',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_block_user' => array(
'classname' => 'core_message_external',
'methodname' => 'block_user',
Expand Down
119 changes: 119 additions & 0 deletions message/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,125 @@ public static function delete_contacts_returns() {
return null;
}

/**
* Mute conversations parameters description.
*
* @return external_function_parameters
*/
public static function mute_conversations_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is blocking'),
'conversationids' => new external_multiple_structure(
new external_value(PARAM_INT, 'id of the conversation', VALUE_REQUIRED)
),
]
);
}

/**
* Mutes conversations.
*
* @param int $userid The id of the user who is blocking
* @param array $conversationids The list of conversations being muted
* @return external_description
*/
public static function mute_conversations(int $userid, array $conversationids) {
global $CFG, $USER;

// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}

// Validate context.
$context = context_system::instance();
self::validate_context($context);

$params = ['userid' => $userid, 'conversationids' => $conversationids];
$params = self::validate_parameters(self::mute_conversations_parameters(), $params);

$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}

foreach ($params['conversationids'] as $conversationid) {
if (!\core_message\api::is_conversation_muted($params['userid'], $conversationid)) {
\core_message\api::mute_conversation($params['userid'], $conversationid);
}
}

return [];
}

/**
* Mute conversations return description.
*
* @return external_description
*/
public static function mute_conversations_returns() {
return new external_warnings();
}

/**
* Unmute conversations parameters description.
*
* @return external_function_parameters
*/
public static function unmute_conversations_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'The id of the user who is unblocking'),
'conversationids' => new external_multiple_structure(
new external_value(PARAM_INT, 'id of the conversation', VALUE_REQUIRED)
),
]
);
}

/**
* Unmute conversations.
*
* @param int $userid The id of the user who is unblocking
* @param array $conversationids The list of conversations being muted
*/
public static function unmute_conversations(int $userid, array $conversationids) {
global $CFG, $USER;

// Check if messaging is enabled.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}

// Validate context.
$context = context_system::instance();
self::validate_context($context);

$params = ['userid' => $userid, 'conversationids' => $conversationids];
$params = self::validate_parameters(self::unmute_conversations_parameters(), $params);

$capability = 'moodle/site:manageallmessaging';
if (($USER->id != $params['userid']) && !has_capability($capability, $context)) {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}

foreach ($params['conversationids'] as $conversationid) {
\core_message\api::unmute_conversation($params['userid'], $conversationid);
}

return [];
}

/**
* Unmute conversations return description.
*
* @return external_description
*/
public static function unmute_conversations_returns() {
return new external_warnings();
}

/**
* Block user parameters description.
*
Expand Down
162 changes: 162 additions & 0 deletions message/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,168 @@ public function test_decline_contact_request_no_permission() {
core_message_external::decline_contact_request($user1->id, $user2->id);
}

/**
* Test muting conversations.
*/
public function test_mute_conversations() {
global $DB;

$this->resetAfterTest(true);

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

$this->setUser($user1);

// Muting a conversation.
$return = core_message_external::mute_conversations($user1->id, [$conversation->id]);
$return = external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return);
$this->assertEquals(array(), $return);

// Get list of muted conversations.
$mca = $DB->get_record('message_conversation_actions', []);

$this->assertEquals($user1->id, $mca->userid);
$this->assertEquals($conversation->id, $mca->conversationid);
$this->assertEquals(\core_message\api::CONVERSATION_ACTION_MUTED, $mca->action);

// Muting a conversation that is already muted.
$return = core_message_external::mute_conversations($user1->id, [$conversation->id]);
$return = external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return);
$this->assertEquals(array(), $return);

$this->assertEquals(1, $DB->count_records('message_conversation_actions'));
}

/**
* Test muting a conversation with messaging disabled.
*/
public function test_mute_conversations_messaging_disabled() {
global $CFG;

$this->resetAfterTest();

// Create some skeleton data just so we can call the WS.
$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]);

$this->setUser($user1);

// Disable messaging.
$CFG->messaging = 0;

// Ensure an exception is thrown.
$this->expectException('moodle_exception');
core_message_external::mute_conversations($user1->id, [$conversation->id]);
}

/**
* Test muting a conversation with no permission.
*/
public function test_mute_conversations_no_permission() {
$this->resetAfterTest();

// Create some skeleton data just so we can call the WS.
$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_INDIVIDUAL,
[$user1->id, $user2->id]);

$this->setUser($user3);

// Ensure an exception is thrown.
$this->expectException('required_capability_exception');
core_message_external::mute_conversations($user1->id, [$conversation->id]);
}

/**
* Test unmuting conversations.
*/
public function test_unmute_conversations() {
global $DB;

$this->resetAfterTest(true);

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

$this->setUser($user1);

// Mute the conversation.
\core_message\api::mute_conversation($user1->id, $conversation->id);

// Unmuting a conversation.
$return = core_message_external::unmute_conversations($user1->id, [$conversation->id]);
$return = external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return);
$this->assertEquals(array(), $return);

$this->assertEquals(0, $DB->count_records('message_conversation_actions'));

// Unmuting a conversation which is already unmuted.
$return = core_message_external::unmute_conversations($user1->id, [$conversation->id]);
$return = external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return);
$this->assertEquals(array(), $return);

$this->assertEquals(0, $DB->count_records('message_conversation_actions'));
}

/**
* Test unmuting a conversation with messaging disabled.
*/
public function test_unmute_conversation_messaging_disabled() {
global $CFG;

$this->resetAfterTest();

// Create some skeleton data just so we can call the WS.
$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]);

$this->setUser($user1);

// Disable messaging.
$CFG->messaging = 0;

// Ensure an exception is thrown.
$this->expectException('moodle_exception');
core_message_external::unmute_conversations($user1->id, [$user2->id]);
}

/**
* Test unmuting a conversation with no permission.
*/
public function test_unmute_conversation_no_permission() {
$this->resetAfterTest();

// Create some skeleton data just so we can call the WS.
$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_INDIVIDUAL,
[$user1->id, $user2->id]);

$this->setUser($user3);

// Ensure an exception is thrown.
$this->expectException('required_capability_exception');
core_message_external::unmute_conversations($user1->id, [$conversation->id]);
}

/**
* Test blocking a user.
*/
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 = 2019030800.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2019030800.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 086409f

Please sign in to comment.