diff --git a/communication/provider/matrix/classes/matrix_user_manager.php b/communication/provider/matrix/classes/matrix_user_manager.php index 2dd4041f8b0c6..34a6b1fa85eac 100644 --- a/communication/provider/matrix/classes/matrix_user_manager.php +++ b/communication/provider/matrix/classes/matrix_user_manager.php @@ -67,7 +67,12 @@ public static function get_formatted_matrix_userid( $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 c5e5eb4f96be4..c101fd16b8f52 100644 --- a/communication/provider/matrix/lang/en/communication_matrix.php +++ b/communication/provider/matrix/lang/en/communication_matrix.php @@ -24,6 +24,8 @@ $string['cachedef_serverversions'] = 'Matrix server version information for running servers'; $string['matrixuserid'] = 'Matrix user ID'; +$string['matrixhomeservername'] = 'Homeserver Name'; +$string['matrixhomeservername_desc'] = 'The name of your Synapse homeserver if different from the URL. The part that follows the @user: in usernames. e.g. @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['matrixaccesstoken'] = 'Access token'; diff --git a/communication/provider/matrix/settings.php b/communication/provider/matrix/settings.php index 5f23f3240f67b..4a5800647980b 100644 --- a/communication/provider/matrix/settings.php +++ b/communication/provider/matrix/settings.php @@ -30,6 +30,11 @@ $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 559e435e4174b..b84b97a3ac9f7 100644 --- a/communication/provider/matrix/tests/matrix_user_manager_test.php +++ b/communication/provider/matrix/tests/matrix_user_manager_test.php @@ -86,12 +86,15 @@ public function test_get_formatted_matrix_userid_unset(): void { * @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(); - + if ($servername !== null) { + set_config('matrixhomeservername', $servername, 'communication_matrix'); + } set_config('matrixhomeserverurl', $server, 'communication_matrix'); $this->assertEquals( $expecteduserid, @@ -106,27 +109,44 @@ public function test_get_formatted_matrix_userid( */ 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',