diff --git a/communication/provider/matrix/classes/matrix_user_manager.php b/communication/provider/matrix/classes/matrix_user_manager.php index 2dd4041f8b0..34a6b1fa85e 100644 --- a/communication/provider/matrix/classes/matrix_user_manager.php +++ b/communication/provider/matrix/classes/matrix_user_manager.php @@ -67,7 +67,12 @@ class matrix_user_manager { $username = self::MATRIX_USER_PREFIX . $username; } - $homeserver = self::get_formatted_matrix_home_server(); + $homeserver = get_config('communication_matrix', 'matrixhomeservername'); + + // Fall back to homeserver URL if name not set. + if (empty($homeserver)) { + $homeserver = self::get_formatted_matrix_home_server(); + } return "@{$username}:{$homeserver}"; } diff --git a/communication/provider/matrix/lang/en/communication_matrix.php b/communication/provider/matrix/lang/en/communication_matrix.php index c5e5eb4f96b..07f3b22dfcf 100644 --- a/communication/provider/matrix/lang/en/communication_matrix.php +++ b/communication/provider/matrix/lang/en/communication_matrix.php @@ -23,14 +23,16 @@ */ $string['cachedef_serverversions'] = 'Matrix server version information for running servers'; -$string['matrixuserid'] = 'Matrix user ID'; +$string['matrixhomeservername'] = 'Homeserver Name'; +$string['matrixhomeservername_desc'] = 'The part after @user: in your Matrix ID (e.g. example.com in @user:example.com)'; $string['matrixhomeserverurl'] = 'Homeserver URL'; -$string['matrixhomeserverurl_desc'] = 'The URL of the Synapse homeserver to connect to, for user and room creation.'; +$string['matrixhomeserverurl_desc'] = 'Server URL for connecting and creating accounts (e.g., https://matrix.example.com)'; $string['matrixaccesstoken'] = 'Access token'; $string['matrixaccesstoken_desc'] = 'Access token for the account which will perform actions on the homeserver.'; $string['matrixelementurl'] = 'Element web URL'; $string['matrixroomtopic'] = 'Room topic'; $string['matrixroomtopic_help'] = 'A short description of what this room is for.'; +$string['matrixuserid'] = 'Matrix user ID'; $string['matrix:moderator'] = 'Matrix moderator'; $string['pluginname'] = 'Matrix'; $string['privacy:metadata'] = 'The Matrix communication plugin does not store any personal data.'; diff --git a/communication/provider/matrix/settings.php b/communication/provider/matrix/settings.php index 5f23f3240f6..4a580064798 100644 --- a/communication/provider/matrix/settings.php +++ b/communication/provider/matrix/settings.php @@ -30,6 +30,11 @@ if ($hassiteconfig) { $desc = new lang_string('matrixhomeserverurl_desc', 'communication_matrix'); $settings->add(new admin_setting_configtext('communication_matrix/matrixhomeserverurl', $name, $desc, '')); + // Home server name. + $name = new lang_string('matrixhomeservername', 'communication_matrix'); + $desc = new lang_string('matrixhomeservername_desc', 'communication_matrix'); + $settings->add(new admin_setting_configtext('communication_matrix/matrixhomeservername', $name, $desc, '')); + // Access token. $name = new lang_string('matrixaccesstoken', 'communication_matrix'); $desc = new lang_string('matrixaccesstoken_desc', 'communication_matrix'); diff --git a/communication/provider/matrix/tests/matrix_user_manager_test.php b/communication/provider/matrix/tests/matrix_user_manager_test.php index a4b7ce77631..982dec3b4ad 100644 --- a/communication/provider/matrix/tests/matrix_user_manager_test.php +++ b/communication/provider/matrix/tests/matrix_user_manager_test.php @@ -86,12 +86,13 @@ final class matrix_user_manager_test extends \advanced_testcase { * @param string $expecteduserid The expected matrix user id */ public function test_get_formatted_matrix_userid( + ?string $servername, string $server, string $username, string $expecteduserid, ): void { $this->resetAfterTest(); - + set_config('matrixhomeservername', $servername, 'communication_matrix'); set_config('matrixhomeserverurl', $server, 'communication_matrix'); $this->assertEquals( $expecteduserid, @@ -106,27 +107,44 @@ final class matrix_user_manager_test extends \advanced_testcase { */ public static function get_formatted_matrix_userid_provider(): array { return [ + 'servername' => [ + 'example.org', + 'https://matrix.example.org', + 'user', + '@user:example.org', + ], + 'servername empty string' => [ + '', + 'https://matrix.example.org', + 'user', + '@user:matrix.example.org', + ], 'alphanumeric' => [ + null, 'https://matrix.example.org', 'alphabet1', '@alphabet1:matrix.example.org', ], 'chara' => [ + null, 'https://matrix.example.org', 'asdf#$%^&*()+{}|<>?!,asdf', '@asdf.................asdf:matrix.example.org', ], 'local server' => [ + null, 'https://synapse', 'colin.creavey', '@colin.creavey:synapse', ], 'server with port' => [ + null, 'https://matrix.example.org:8448', 'colin.creavey', '@colin.creavey:matrix.example.org', ], 'numeric username' => [ + null, 'https://matrix.example.org', '123456', '@' . matrix_user_manager::MATRIX_USER_PREFIX . '123456:matrix.example.org',