Thanasoft-H2F/gestion/lib/Service/TalkService.php
2025-03-11 14:29:34 +03:00

104 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* Calendar App
*
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Gestion\Service;
use DateTime;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Db\TalkDb;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
class TalkService {
/** @var TalkDb */
private $talkDb;
/** @var LoggerInterface */
private $logger;
public function __construct(
TalkDb $talkDb,
LoggerInterface $logger) {
$this->logger = $logger;
$this->talkDb = $talkDb;
}
private function getUserDevisTalkRoomNames($idNextCloud){
$roomNamesCreatedByUser = '["'.$idNextCloud.'","'.BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD.'"]';
$roomNamesCreatedByAdmin = '["'.BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD.'","'.$idNextCloud.'"]';
return [
"createdByUser" => $roomNamesCreatedByUser,
"createdByAdmin" => $roomNamesCreatedByAdmin
];
}
private function generateTalkRandomToken(){
$length = 8;
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$charactersLength = strlen($characters);
$randomToken = '';
for ($i = 0; $i < $length; $i++) {
$randomToken .= $characters[rand(0, $charactersLength - 1)];
}
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;
}
$roomNames = $this->getUserDevisTalkRoomNames($idNextcloud);
$room = $this->talkDb->getDevisTalkRoomByNames($roomNames);
if($room == null){
$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;
}
}