From 92e799312c7ab0cc39c926e4fea4aa716a45bc2f Mon Sep 17 00:00:00 2001 From: Thomas Hackl <hackl@data-quest.de> Date: Mon, 20 Sep 2021 11:17:52 +0200 Subject: [PATCH] post room message on news creation or update --- MatrixPlugin.php | 29 +++++++++++++++++++++++++---- controllers/matrix_chat.php | 5 ----- models/MatrixRoom.php | 23 ----------------------- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/MatrixPlugin.php b/MatrixPlugin.php index 0e4b205..7f0728e 100644 --- a/MatrixPlugin.php +++ b/MatrixPlugin.php @@ -32,6 +32,8 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin NotificationCenter::addObserver($this, 'uninvite', 'CourseMemberDidDelete'); NotificationCenter::addObserver($this, 'unregister', 'UserDidDelete'); NotificationCenter::addObserver($this, 'deleteRoom', 'CourseDidDelete'); + NotificationCenter::addObserver($this, 'postNews', 'StudipNewsDidCreate'); + NotificationCenter::addObserver($this, 'postNews', 'StudipNewsDidUpdate'); $this->addScript('assets/javascripts/matrixchat.js'); $this->addStylesheet('assets/stylesheets/matrixchat.scss'); @@ -130,7 +132,7 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin * @param string $event * @param CourseMember $membership */ - private function invite($event, $membership) + public function invite($event, $membership) { if ($matrix = MatrixAccount::findByUser_id($membership->user_id) && $room = MatrixRoom::findByRange_id($membership->seminar_id)) { @@ -148,7 +150,7 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin * @param string $event * @param CourseMember $membership */ - private function uninvite($event, $membership) + public function uninvite($event, $membership) { if ($matrix = MatrixAccount::findByUser_id($membership->user_id) && $room = MatrixRoom::findByRange_id($membership->seminar_id)) { @@ -166,7 +168,7 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin * @param string $event * @param User $user */ - private function unregister($event, $user) + public function unregister($event, $user) { if ($account = MatrixAccount::findByUser_id($user->id)) { if (MatrixClient::get()->deactivateAccount($account->getLinkedAccount())) { @@ -181,7 +183,7 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin * @param string $event * @param Course $course */ - private function deleteRoom($event, $course) + public function deleteRoom($event, $course) { if ($room = MatrixRoom::findByRange_id($course->id)) { MatrixClient::get()->deleteRoom( @@ -191,4 +193,23 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin } } + /** + * Auto-post course news to associated Matrix room if configured. + * + * @param string $event + * @param StudipNews $news + */ + public function postNews($event, $news) + { + foreach ($news->news_ranges as $range) { + if ($range->course && MatrixRoom::hasRoom($range->course->id)) { + MatrixClient::get()->postMessage( + MatrixAccount::requireSystemAccount(), + MatrixRoom::find($range->course->id)->getLinkedRoom(), + $news->topic + ); + } + } + } + } \ No newline at end of file diff --git a/controllers/matrix_chat.php b/controllers/matrix_chat.php index ffc66df..112ff01 100644 --- a/controllers/matrix_chat.php +++ b/controllers/matrix_chat.php @@ -51,11 +51,6 @@ class MatrixChatController extends AuthenticatedController $this->hasToCreate = false; $room->requireMembership($this->account->getLinkedAccount()); - - $room->sendMessage( - 'Avengers... assemble.', - '<!-- HTML -->Hulk <strong>SMASH</strong>!' - ); } } diff --git a/models/MatrixRoom.php b/models/MatrixRoom.php index 7caf4fe..6d5489c 100644 --- a/models/MatrixRoom.php +++ b/models/MatrixRoom.php @@ -158,27 +158,4 @@ class MatrixRoom extends SimpleORMap } } - /** - * Sends a message into the current room, sender is the Stud.IP system account. - * - * @param string $title - * @param string $body - */ - public function sendMessage($title, $body) - { - $content = [ - 'msgtype' => 'm.text', - 'format' => 'm.format.custom_html', - 'body' => strip_tags($title . ":\n" . $body), - 'formatted_body' => htmlReady('<strong>' . $title . '</strong><br>' . $body), - ]; - MatrixClient::get()->sendRoomEvent( - MatrixAccount::requireSystemAccount(), - $this->getLinkedRoom(), - 'm.room.message', - '', - $content - ); - } - } \ No newline at end of file -- GitLab