Skip to content

Commit

Permalink
MDL-63692 core_message: Add favourite conversations to Privacy API
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Nov 19, 2018
1 parent 6b036d0 commit 16913e6
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 11 deletions.
31 changes: 26 additions & 5 deletions favourites/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ public static function get_favourites_info_for_user(int $userid, \context $conte
* @param \context $context The context to which deletion is scoped.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype) {
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype,
int $itemid = 0) {
global $DB;

$params = [
Expand All @@ -184,6 +186,11 @@ public static function delete_favourites_for_all_users(\context $context, string
];

$select = "component = :component AND itemtype =:itemtype AND contextid = :contextid";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}
$DB->delete_records_select('favourite', $select, $params);
}

Expand All @@ -193,10 +200,11 @@ public static function delete_favourites_for_all_users(\context $context, string
* @param \core_privacy\local\request\approved_userlist $userlist The approved contexts and user information
* to delete information for.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_userlist(\core_privacy\local\request\approved_userlist $userlist,
string $itemtype) {
string $itemtype, int $itemid = 0) {
global $DB;

$userids = $userlist->get_userids();
Expand All @@ -217,19 +225,26 @@ public static function delete_favourites_for_userlist(\core_privacy\local\reques
$params += $userparams;
$select = "component = :component AND itemtype = :itemtype AND contextid = :contextid AND userid $usersql";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}

$DB->delete_records_select('favourite', $select, $params);
}

/**
* Delete all favourites for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @param string $component
* @param string $itemtype
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \coding_exception
* @throws \dml_exception
*/
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype) {
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype,
int $itemid = 0) {
global $DB;

$userid = $contextlist->get_user()->id;
Expand All @@ -244,6 +259,12 @@ public static function delete_favourites_for_user(approved_contextlist $contextl
$params += $inparams;

$select = "userid = :userid AND component = :component AND itemtype =:itemtype AND contextid $insql";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}

$DB->delete_records_select('favourite', $select, $params);
}
}
1 change: 1 addition & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
$string['outputnotavailable'] = 'Not available';
$string['outputnotconfigured'] = 'Not configured';
$string['permitted'] = 'Permitted';
$string['privacy:metadata:core_favourites'] = 'The conversations starred by the user';
$string['privacy:metadata:messages'] = 'Messages';
$string['privacy:metadata:messages:conversationid'] = 'The ID of the conversation';
$string['privacy:metadata:messages:fullmessage'] = 'The full message';
Expand Down
78 changes: 78 additions & 0 deletions message/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public static function get_metadata(collection $items) : collection {
$items->add_user_preference('core_message_messageprovider_settings',
'privacy:metadata:preference:core_message_settings');

// Add favourite conversations.
$items->link_subsystem('core_favourites', 'privacy:metadata:core_favourites');

return $items;
}

Expand Down Expand Up @@ -224,6 +227,9 @@ public static function get_contexts_for_userid(int $userid) : contextlist {
$contextlist->add_user_context($userid);
}

// Add favourite conversations.
\core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $userid, 'core_message', 'message_conversations');

return $contextlist;
}

Expand Down Expand Up @@ -273,6 +279,16 @@ public static function get_users_in_context(userlist $userlist) {
if ($hasdata) {
$userlist->add_user($userid);
}

// Add favourite conversations.
$component = $userlist->get_component();
if ($component != 'core_message') {
$userlist->set_component('core_message');
}
\core_favourites\privacy\provider::add_userids_for_context($userlist, 'message_conversations');
if ($component != 'core_message') {
$userlist->set_component($component);
}
}

/**
Expand Down Expand Up @@ -407,6 +423,10 @@ public static function add_contexts_for_conversations(contextlist $contextlist,
}

$contextlist->add_from_sql($sql, $params);

// Add favourite conversations. We don't need to filter by itemid because for now they are in the system context.
\core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $userid, 'core_message', 'message_conversations');

}

/**
Expand Down Expand Up @@ -435,6 +455,16 @@ public static function add_conversations_in_context(userlist $userlist, string $
}

$userlist->add_from_sql('userid', $sql, $params);

// Add favourite conversations.
$component = $userlist->get_component();
if ($component != 'core_message') {
$userlist->set_component('core_message');
}
\core_favourites\privacy\provider::add_userids_for_context($userlist, 'message_conversations');
if ($component != 'core_message') {
$userlist->set_component($component);
}
}

/**
Expand Down Expand Up @@ -519,6 +549,15 @@ public static function delete_conversations_for_all_users(\context $context, str
$messageids = $DB->get_records_list('messages', 'conversationid', $conversationids);
$messageids = array_keys($messageids);

// Delete these favourite conversations to all the users.
foreach ($conversationids as $conversationid) {
\core_favourites\privacy\provider::delete_favourites_for_all_users(
$context,
'core_message',
'message_conversations',
$conversationid);
}

// Delete messages and user_actions.
$DB->delete_records_list('message_user_actions', 'messageid', $messageids);
$DB->delete_records_list('messages', 'id', $messageids);
Expand Down Expand Up @@ -629,6 +668,13 @@ public static function delete_conversations_for_users(approved_userlist $userlis
$sql = "conversationid $conversationidsql AND userid $useridsql";
// Reuse the $params var because it contains the useridparams and the conversationids.
$DB->delete_records_select('message_conversation_members', $sql, $params);

// Delete the favourite conversations.
$userlist = new \core_privacy\local\request\approved_userlist($context, 'core_message', $userids);
\core_favourites\privacy\provider::delete_favourites_for_userlist(
$userlist,
'message_conversations'
);
}
}

Expand Down Expand Up @@ -712,6 +758,24 @@ protected static function delete_user_data_conversations(int $userid, array $con
$sql = "conversationid $conversationidsql AND userid = :userid";
// Reuse the $params var because it contains the userid and the conversationids.
$DB->delete_records_select('message_conversation_members', $sql, $params);

// Delete the favourite conversations.
if (empty($contextids) && empty($component) && empty($itemtype) && empty($itemid)) {
// Favourites for individual conversations are stored into the user context.
$favouritectxids = [\context_user::instance($userid)->id];
} else {
$favouritectxids = $contextids;
}
$contextlist = new \core_privacy\local\request\approved_contextlist(
\core_user::get_user($userid),
'core_message',
$favouritectxids
);
\core_favourites\privacy\provider::delete_favourites_for_user(
$contextlist,
'core_message',
'message_conversations'
);
}
}

Expand Down Expand Up @@ -874,6 +938,9 @@ protected static function export_user_data_conversation_messages(int $userid, \s
$subcontext,
[get_string('messages', 'core_message'), $otherusertext]
);

// Get the context for the favourite conversation.
$conversationctx = \context_user::instance($userid);
} else {
// Conversations with context are stored in 'Messages | <Conversation item type> | <Conversation name>'.
if (get_string_manager()->string_exists($conversation->itemtype, $conversation->component)) {
Expand All @@ -888,10 +955,21 @@ protected static function export_user_data_conversation_messages(int $userid, \s
$subcontext,
[get_string('messages', 'core_message'), $itemtypestring, $conversationname]
);

// Get the context for the favourite conversation.
$conversationctx = \context::instance_by_id($conversation->contextid);
}

// Export the conversation messages.
writer::with_context($context)->export_data($subcontext, (object) $messagedata);

// Get user's favourites information for the particular conversation.
$conversationfavourite = \core_favourites\privacy\provider::get_favourites_info_for_user($userid, $conversationctx,
'core_message', 'message_conversations', $conversation->id);
if ($conversationfavourite) {
// If the conversation has been favorited by the user, include it in the export.
writer::with_context($context)->export_related_data($subcontext, 'starred', (object) $conversationfavourite);
}
}
}

Expand Down
Loading

0 comments on commit 16913e6

Please sign in to comment.