From 8c965ce08be5d1bbd62467f14ecd40dc4c2e36f7 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Tue, 21 Jan 2025 17:13:52 +0300 Subject: [PATCH] talk notifications --- gestion/lib/Db/TalkDb.php | 44 ++++++++++++++++++++++++++--- gestion/lib/Service/TalkService.php | 18 +++++++++++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/gestion/lib/Db/TalkDb.php b/gestion/lib/Db/TalkDb.php index f7651eb..530f037 100644 --- a/gestion/lib/Db/TalkDb.php +++ b/gestion/lib/Db/TalkDb.php @@ -16,12 +16,14 @@ class TalkDb { private const DEVIS_TALK_ROOM_INITIAL_MESSAGE = '{"message":"conversation_created","parameters":[]}'; private const DEVIS_TALK_ROOM_DEFAULT_MESSAGE_TYPE = "chat"; private const DEVIS_TALK_ROOM_DEFAULT_PARTICIPANT_TYPE = 1; - private $commentsTablePrefix; + private const DEVIS_TALK_NOTIFICATIONS_APP_NAME ="spreed"; + + private $defaultTablePrefix; private IDbConnection $pdo; public function __construct(IDbConnection $db) { $this->talkTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX ."talk_"; - $this->commentsTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX; + $this->defaultTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX; $this->pdo = $db; } @@ -104,7 +106,7 @@ class TalkDb { private function createDevisTalkRoomMessage($actorId,$message,$verb,$roomId,$reference = null){ $datetime = new DateTime(); $datetime = $datetime->format(('Y-m-d H:i:s')); - $sql = "INSERT INTO `".$this->commentsTablePrefix."comments` ( + $sql = "INSERT INTO `".$this->defaultTablePrefix."comments` ( `actor_type`, `actor_id`, `message`, @@ -128,7 +130,7 @@ class TalkDb { private function getDevisTalkRoomMessageByRoomAndMessage($roomId,$message){ $sql = "SELECT * - FROM ".$this->commentsTablePrefix."comments as comment + FROM ".$this->defaultTablePrefix."comments as comment WHERE comment.object_id = ? AND comment.message = ? ORDER BY comment.id DESC @@ -226,5 +228,39 @@ class TalkDb { [$lastMessageId,$roomId]); } + public function sendAttendeeNotifications($idNextCloud,$roomToken,$notificationsSubjectsParameters,$notificationsMessageParameters){ + $datetimeNow = new DateTime(); + $timestamp = $datetimeNow->getTimestamp(); + $sql = "INSERT INTO `".$this->defaultTablePrefix."notifications` ( + `app`, + `user`, + `timestamp`, + `object_type`, + `object_id`, + `subject`, + `subject_parameters`, + `message`, + `message_parameters`, + `link`, + `icon`, + `actions` + ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);"; + + $this->execSQLNoData($sql, array( + self::DEVIS_TALK_NOTIFICATIONS_APP_NAME, + $idNextCloud, + $timestamp, + 'chat', + $roomToken, + 'chat', + $notificationsSubjectsParameters, + 'comment', + $notificationsMessageParameters, + '', + '', + '[]' + )); + } + } \ No newline at end of file diff --git a/gestion/lib/Service/TalkService.php b/gestion/lib/Service/TalkService.php index 5911c19..837f7c5 100644 --- a/gestion/lib/Service/TalkService.php +++ b/gestion/lib/Service/TalkService.php @@ -68,6 +68,14 @@ class TalkService { return $randomToken; } + private function getNotificationsSubjectsParameters(){ + return '{"userType":"users","userId":"'.BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD.'"}'; + } + + private function getNotificationsMessageParameters($commentId){ + return '{"commentId":"'.$commentId.'"}'; + } + public function sendDevisTalkNotifications(string $message,string $idNextcloud){ if($idNextcloud === BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD || $idNextcloud === BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD){ return true; @@ -75,12 +83,20 @@ class TalkService { $roomNames = $this->getUserDevisTalkRoomNames($idNextcloud); $room = $this->talkDb->getDevisTalkRoomByNames($roomNames); if($room == null){ - $room = $this->talkDb->createDevisTalkRoomAndReturnDevisTalkRoom($idNextcloud,$this->generateTalkRandomToken()); + $roomToken = $this->generateTalkRandomToken(); + $room = $this->talkDb->createDevisTalkRoomAndReturnDevisTalkRoom($idNextcloud,$roomToken); $initialMessage = $this->talkDb->setDevisTalkRoomInitialMessageAndReturnMessage($room['id'],$idNextcloud); } + else{ + $roomToken = $room['token']; + } $devisMessage = $this->talkDb->createDevisTalkRoomMessageAndReturnMessage($room['id'],$message); $this->talkDb->updateRoomLastMessage($room['id'],$devisMessage['id']); $this->talkDb->setAttendeeLastReadMessage($room['id'],$devisMessage['id'],BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD); + //send notifications + $notificationsSubjectsParameters = $this->getNotificationsSubjectsParameters(); + $notificationsMessageParameters = $this->getNotificationsMessageParameters($devisMessage['id']); + $this->talkDb->sendAttendeeNotifications($idNextcloud,$roomToken,$notificationsSubjectsParameters,$notificationsMessageParameters); return true; }