MDL-64426 core_message: fix filter support in api::get_conversations()

Conversation name and subname should support filters, so the data should
be passed through format_string, and advertised in the external params
as PARAM_TEXT.
This commit is contained in:
Jake Dallimore
2019-01-08 15:51:32 +08:00
parent afba820e1d
commit 0c638fda5c
2 changed files with 17 additions and 4 deletions
+15 -2
View File
@@ -553,7 +553,7 @@ class api {
$sql = "SELECT m.id as messageid, mc.id as id, mc.name as conversationname, mc.type as conversationtype, m.useridfrom,
m.smallmessage, m.fullmessage, m.fullmessageformat, m.fullmessagehtml, m.timecreated, mc.component,
mc.itemtype, mc.itemid
mc.itemtype, mc.itemid, mc.contextid
FROM {message_conversations} mc
INNER JOIN {message_conversation_members} mcm
ON (mcm.conversationid = mc.id AND mcm.userid = :userid3)
@@ -748,6 +748,10 @@ class api {
$unreadcounts = $DB->get_records_sql($unreadcountssql, [$userid, self::MESSAGE_ACTION_READ, self::MESSAGE_ACTION_DELETED,
$userid, $userid]);
// Because we'll be calling format_string on each conversation name and passing contexts, we preload them here.
// This warms the cache and saves potentially hitting the DB once for each context fetch below.
\context_helper::preload_contexts_by_id(array_column($conversations, 'contextid'));
// Now, create the final return structure.
$arrconversations = [];
foreach ($conversations as $conversation) {
@@ -766,7 +770,16 @@ class api {
$conv = new \stdClass();
$conv->id = $conversation->id;
$conv->name = $conversation->conversationname;
// Name should be formatted and depends on the context the conversation resides in.
// If not set, the context is always context_user.
if (is_null($conversation->contextid)) {
$convcontext = \context_user::instance_by_id($userid);
} else {
$convcontext = \context::instance_by_id($conversation->contextid);
}
$conv->name = format_string($conversation->conversationname, true, ['context' => $convcontext]);
$conv->subname = $convextrafields[$conv->id]['subname'] ?? null;
$conv->imageurl = $convextrafields[$conv->id]['imageurl'] ?? null;
$conv->type = $conversation->conversationtype;