MDL-77917 communication_matrix: Support server API versions

This commit brings in support for multiple versions of the Matrix
specification.

A Matrix server is compromised of a number of individually versioned API
endpoints, for example:

    /_matrix/client/v3/createRoom
    /_matrix/client/v3/rooms/:roomid/joined_members
    /_matrix/media/v1/create

The combination of a large number of these individually versioned
endpoints forms a Matrix Specification version.

For example:

* the /_matrix/media/v1/create endpoint was created for version 1.7 of the
  specification, and does not exist in earlier versions.
* in the future a new behaviour or parameter may be created for the
  `createRoom` endpoint and a new endpoint created at:

    /_matrix/client/v4/createRoom

A single server can support multiple versions of the Matrix
specification. The server declares the versions of the specification
that it supports using a non-versioned endpoint at
`/_matrix/client/versions`.

As a Matrix client, Moodle should:
* query the server version endpoint
* determine the combination of mutually supported Matrix specification
  versions
* create a client instance of the highest-supported version of the
  specification.

For example, if Moodle (Matrix client) and a remote server have the
following support:

```
Moodle:      1.1  1.2  1.3  1.4  1.5  1.6  1.7
Server:  r0  1.1  1.2  1.3  1.4  1.5  1.6
```

The versions in common are 1.1 through 1.6, and version 1.6 would be
chosen.

To avoid duplication and allow for support of future features more
easily, the Moodle client is written as:
* a set of classes named `v1p1` through `v1p7` (currently) which extend
  the `matrix_client` abstract class; and
* a set if PHP traits which provide the implementation for individual
  versioned endpoints.

Each client version then imports any relevant traits which are present
in that version of the Matrix Specification. For example versions 1.1 to
1.6 do _not_ have the `/_matrix/media/v1/create` endpoint so they do not
import this trait. This trait was introduced in version 1.7, so the
trait is included from that version onwards.

In the future, if an endpoint is created which conflicts with an
existing endpoint, then it would be easy to create a new client version
which uses the existing common traits, and adds the new trait.

Each endpoint is written using a `command` class which extends the
Guzzle implementation of the PSR-7 Request interface. This command
class adds support for easy creation of:
* path parameters within the URI
* query parameters
* body parameters

This is done to avoid complex patterns of Request creation which are
repeated for every client endpoint.
This commit is contained in:
Andrew Nicols
2023-08-24 11:59:25 +08:00
parent cccc00954d
commit 914686bc5e
40 changed files with 3326 additions and 1901 deletions
+26 -18
View File
@@ -104,24 +104,29 @@ class processor {
}
/**
* Update communication instance.
* Update the communication instance with any changes.
*
* @param string $provider The communication provider
* @param string $roomname The room name
* @param null|string $provider The communication provider
* @param null|string $roomname The room name
*/
public function update_instance(
string $provider,
string $roomname,
?string $provider = null,
?string $roomname = null,
): void {
global $DB;
if ($provider === self::PROVIDER_NONE) {
$this->instancedata->active = self::PROVIDER_INACTIVE;
} else {
$this->instancedata->provider = $provider;
$this->instancedata->active = self::PROVIDER_ACTIVE;
if ($provider !== null) {
if ($provider === self::PROVIDER_NONE) {
$this->instancedata->active = self::PROVIDER_INACTIVE;
} else {
$this->instancedata->provider = $provider;
$this->instancedata->active = self::PROVIDER_ACTIVE;
}
}
if ($roomname !== null) {
$this->instancedata->roomname = $roomname;
}
$this->instancedata->roomname = $roomname;
$DB->update_record('communication', $this->instancedata);
}
@@ -470,7 +475,7 @@ class processor {
/**
* Get communication instance for form feature.
*
* @return bool
* @return form_provider
*/
public function get_form_provider(): form_provider {
$this->requires_form_features();
@@ -584,7 +589,7 @@ class processor {
$this->instancedata->avatarfilename,
);
return $file ? $file : null;
return $file ?: null;
}
@@ -595,7 +600,9 @@ class processor {
*/
public function set_avatar_filename(?string $filename): void {
global $DB;
$DB->update_record('communication', ['id' => $this->instancedata->id, 'avatarfilename' => $filename]);
$this->instancedata->avatarfilename = $filename;
$DB->set_field('communication', 'avatarfilename', $filename, ['id' => $this->instancedata->id]);
}
/**
@@ -613,7 +620,7 @@ class processor {
* @return bool
*/
public function is_avatar_synced(): bool {
return (bool)$this->instancedata->avatarsynced;
return (bool) $this->instancedata->avatarsynced;
}
/**
@@ -623,8 +630,9 @@ class processor {
*/
public function set_avatar_synced_flag(bool $synced): void {
global $DB;
$DB->update_record('communication', ['id' => $this->instancedata->id, 'avatarsynced' => (int)$synced]);
$this->instancedata->avatarsynced = (int)$synced;
$this->instancedata->avatarsynced = (int) $synced;
$DB->set_field('communication', 'avatarsynced', (int) $synced, ['id' => $this->instancedata->id]);
}
/**