diff --git a/message/classes/api.php b/message/classes/api.php index daab81d1cdd..cb2e35ad4d6 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -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; diff --git a/message/externallib.php b/message/externallib.php index b98ddd54d22..9a60aeef750 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -1118,8 +1118,8 @@ class core_message_external extends external_api { return new external_single_structure( array( 'id' => new external_value(PARAM_INT, 'The conversation id'), - 'name' => new external_value(PARAM_NOTAGS, 'The conversation name, if set', VALUE_DEFAULT, null), - 'subname' => new external_value(PARAM_NOTAGS, 'A subtitle for the conversation name, if set', VALUE_DEFAULT, null), + 'name' => new external_value(PARAM_TEXT, 'The conversation name, if set', VALUE_DEFAULT, null), + 'subname' => new external_value(PARAM_TEXT, 'A subtitle for the conversation name, if set', VALUE_DEFAULT, null), 'imageurl' => new external_value(PARAM_URL, 'A link to the conversation picture, if set', VALUE_DEFAULT, null), 'type' => new external_value(PARAM_INT, 'The type of the conversation (1=individual,2=group)'), 'membercount' => new external_value(PARAM_INT, 'Total number of conversation members'),