Skip to content

Commit

Permalink
MDL-81685 communication_matrix: Adds Matrix homeserver name setting
Browse files Browse the repository at this point in the history
Allows setting a homeserver name for instances where
usernames do not follow the pattern @user:homeserverurl
  • Loading branch information
mark-webster-catalyst committed Nov 14, 2024
1 parent cd5c991 commit 0c80ae9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>@user:</code> in usernames. e.g. <code>@user:example.com</code>.';
$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';
Expand Down
5 changes: 5 additions & 0 deletions communication/provider/matrix/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 21 additions & 1 deletion communication/provider/matrix/tests/matrix_user_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 0c80ae9

Please sign in to comment.