Skip to content

Commit

Permalink
Merge branch 'MDL-77356-master' of https://github.com/davewoloszyn/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 4, 2023
2 parents d1100f0 + 616c6a0 commit 3ab51da
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 16 deletions.
9 changes: 6 additions & 3 deletions communication/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ public function get_avatar_filerecord(string $filename): stdClass {
* @return bool
*/
public function set_avatar_from_datauri_or_filepath(?string $datauri): bool {
global $DB;

$currentfilename = $DB->get_field('communication', 'avatarfilename', ['id' => $this->communication->get_id()]);
$currentfilename = $this->communication->get_avatar_filename();
if (empty($datauri) && empty($currentfilename)) {
return false;
}
Expand Down Expand Up @@ -260,7 +259,11 @@ public function set_avatar_from_datauri_or_filepath(?string $datauri): bool {
$fs->create_file_from_string($this->get_avatar_filerecord($filename), file_get_contents($datauri));
}

$DB->set_field('communication', 'avatarfilename', $filename, ['id' => $this->communication->get_id()]);
$this->communication->set_avatar_filename($filename);

// Indicate that we need to sync the avatar when the update task is run.
$this->communication->set_avatar_synced_flag(false);

return true;
}

Expand Down
41 changes: 41 additions & 0 deletions communication/classes/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static function create_instance(
'roomname' => $roomname,
'avatarfilename' => null,
'active' => self::PROVIDER_ACTIVE,
'avatarsynced' => 0,
];
$record->id = $DB->insert_record('communication', $record);

Expand Down Expand Up @@ -586,6 +587,46 @@ public function get_avatar(): ?stored_file {
return $file ? $file : null;
}


/**
* Set the avatar file name.
*
* @param string|null $filename
*/
public function set_avatar_filename(?string $filename): void {
global $DB;
$DB->update_record('communication', ['id' => $this->instancedata->id, 'avatarfilename' => $filename]);
}

/**
* Get the avatar file name.
*
* @return string|null
*/
public function get_avatar_filename(): ?string {
return $this->instancedata->avatarfilename;
}

/**
* Check if the avatar has been synced with the provider.
*
* @return bool
*/
public function is_avatar_synced(): bool {
return (bool)$this->instancedata->avatarsynced;
}

/**
* Indicate if the avatar has been synced with the provider.
*
* @param boolean $synced True if avatar has been synced.
*/
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;
}

/**
* Get a room url.
*
Expand Down
29 changes: 17 additions & 12 deletions communication/provider/matrix/classes/communication_feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,26 @@ public function delete_chat_room(): bool {
* Update the room avatar when an instance image is added or updated.
*/
public function update_room_avatar(): void {
$instanceimage = $this->communication->get_avatar();
$contenturi = null;

// If avatar is set for the instance, update in matrix.
if (!empty($instanceimage)) {
// First upload the content.
$contenturi = $this->eventmanager->upload_matrix_content($instanceimage->get_content());
}
// Check if we have an avatar that needs to be synced.
if (!$this->communication->is_avatar_synced()) {

// Now update the room avatar.
$json = [
'url' => $contenturi,
];
$instanceimage = $this->communication->get_avatar();
$contenturi = null;

// If avatar is set for the instance, upload to Matrix. Otherwise, leave null for unsetting.
if (!empty($instanceimage)) {
$contenturi = $this->eventmanager->upload_matrix_content($instanceimage->get_content());
}

$response = $this->eventmanager->request(['url' => $contenturi], [], false)->put(
$this->eventmanager->get_update_avatar_endpoint());

$this->eventmanager->request($json, [], false)->put($this->eventmanager->get_update_avatar_endpoint());
// Indicate the avatar has been synced if it was successfully set with Matrix.
if ($response->getReasonPhrase() === 'OK') {
$this->communication->set_avatar_synced_flag(true);
}
}
}

public function get_chat_room_url(): ?string {
Expand Down
13 changes: 13 additions & 0 deletions communication/provider/matrix/tests/matrix_communication_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_create_course_with_matrix_provider(): void {
* @covers \core_communication\api::update_room
* @covers \core_communication\task\update_room_task::execute
* @covers \core_communication\task\update_room_task::queue
* @covers \core_communication\processor::is_avatar_synced
*/
public function test_update_course_with_matrix_provider(): void {
global $CFG;
Expand All @@ -101,6 +102,15 @@ public function test_update_course_with_matrix_provider(): void {
);
$communication->update_room($selectedcommunication, $communicationroomname, $avatarurl);

$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);

// Pending avatar update should indicate avatar is not in sync.
$this->assertFalse($communicationprocessor->is_avatar_synced());

// Run the task.
$this->runAdhocTasks('\core_communication\task\update_room_task');

Expand All @@ -110,6 +120,9 @@ public function test_update_course_with_matrix_provider(): void {
$course->id
);

// Check that the avatar is now synced with Matrix again.
$this->assertTrue($communicationprocessor->is_avatar_synced());

// Initialize the matrix room object.
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());

Expand Down
14 changes: 14 additions & 0 deletions communication/tests/processor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ public function test_get_room_user_provider(): void {
*
* @covers ::get_avatar
* @covers ::load_by_instance
* @covers ::get_avatar_filename
* @covers ::set_avatar_filename
* @covers ::set_avatar_synced_flag
*/
public function test_get_avatar(): void {
$this->resetAfterTest();
Expand Down Expand Up @@ -394,6 +397,17 @@ public function test_get_avatar(): void {
$this->assertEquals($avatar->get_itemid(), $communicationprocessor->get_id());
$this->assertEquals($avatar->get_filepath(), '/');
$this->assertEquals($avatar->get_filearea(), 'avatar');
$this->assertEquals($avatar->get_filename(), $communicationprocessor->get_avatar_filename());

// Change the avatar file name to something else and check it was set.
$communicationprocessor->set_avatar_filename('newname.svg');

$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertEquals($communicationprocessor->get_avatar_filename(), 'newname.svg');
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<FIELD NAME="roomname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Name of the communication room"/>
<FIELD NAME="avatarfilename" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Name of the avatar file name for the communication instance"/>
<FIELD NAME="active" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="The communication instance is active or not"/>
<FIELD NAME="avatarsynced" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Indicate if the avatar has been synced with the provider"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
15 changes: 15 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3333,5 +3333,20 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2023062700.01);
}

if ($oldversion < 2023062900.01) {

// Define field avatarsynced to be added to communication.
$table = new xmldb_table('communication');
$field = new xmldb_field('avatarsynced', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'active');

// Conditionally launch add field avatarsynced.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2023062900.01);
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2023062900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023062900.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.3dev (Build: 20230629)'; // Human-friendly version name
Expand Down

0 comments on commit 3ab51da

Please sign in to comment.