ninegate/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php

148 lines
5.4 KiB
PHP
Raw Normal View History

2019-07-16 12:10:32 +02:00
<?php
namespace Cadoles\WebsocketBundle\Topic;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Cadoles\WebsocketBundle\Entity\Message;
class WebsocketTopic implements TopicInterface
{
protected $em;
protected $container;
protected $clientManipulator;
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $em, ContainerInterface $container) {
$this->clientManipulator = $clientManipulator;
$this->em = $em;
$this->container = $container;
}
/**
* This will receive any Subscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
2019-07-17 11:50:37 +02:00
{
2019-07-16 12:10:32 +02:00
//this will broadcast the message to ALL subscribers of this topic.
2019-09-19 13:46:25 +02:00
$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]);
2019-07-17 11:50:37 +02:00
2019-07-16 12:10:32 +02:00
}
/**
* This will receive any UnSubscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
//this will broadcast the message to ALL subscribers of this topic.
2019-09-19 13:46:25 +02:00
$topic->broadcast(['log' => $connection->resourceId . " has left " . $topic->getId()]);
2019-07-16 12:10:32 +02:00
}
/**
* This will receive any Publish requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @param $event
* @param array $exclude
* @param array $eligible
* @return mixed|void
*/
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
{
$groupid=$request->getAttributes()->get('group');
2019-07-23 15:32:35 +02:00
if(!is_array($event)) $event=json_decode($event, true);
2019-07-16 12:10:32 +02:00
$group=$this->em->getRepository("CadolesCoreBundle:Group")->find($groupid);
if(!$group) {
2019-07-17 11:50:37 +02:00
$topic->broadcast(['log' => 'group NOT FIND']);
2019-07-16 12:10:32 +02:00
}
2019-07-23 15:32:35 +02:00
$usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=>$event["mykey"]]);
2019-07-17 11:50:37 +02:00
if(!$usergroup) {
2019-07-23 15:32:35 +02:00
$topic->broadcast(['log' => $event]);
$topic->broadcast(['log' => 'user '.$event["mykey"].' NOT IN GROUP '.$groupid]);
2019-07-17 11:50:37 +02:00
}
else {
$user=$usergroup->getUser();
2019-07-16 12:10:32 +02:00
}
2019-07-17 11:50:37 +02:00
if($group&&$usergroup&&$user) {
2019-07-17 17:06:05 +02:00
if($event["type"]=="add") {
2019-07-24 13:41:35 +02:00
if(array_key_exists("mail",$event)) {
$mail = $this->container->get('cadoles.core.service.mail');
if($mail) {
$mail_params=array(
"subject" => $event["subject"],
"body_html"=>$event["message"],
"body_text"=>strip_tags($event["message"])
);
// Transformer la liste des destinataires en tableau : [0] tjr vide on l'unset
$to=explode(";",$event["to"]);
unset($to[0]);
$mail->sendEmail("template", $mail_params, $to, $user->getEmail(), $user->getLastname()." ".$user->getFirstname());
$event["message"].="<br><i>Notification envoyée par mail</i>";
}
}
2019-07-17 17:06:05 +02:00
$message=new Message();
$message->setTopic($event["message"]);
$message->setUser($user);
$message->setGroup($group);
$this->em->persist($message);
$this->em->flush();
$return["id"]=$message->getId();
$return["lastname"]=$user->getLastname()." ".$user->getFirstname();
$return["avatar"]=$user->getAvatar();
$return["submitdate"]=$message->getSubmitdate();
$return["message"]=$event["message"];
$return["userid"]=$user->getId();
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast(['msg' => $return]);
}
if($event["type"]=="del") {
$message=$this->em->getRepository("CadolesWebsocketBundle:Message")->find($event["id"]);
2019-07-19 16:51:11 +02:00
if($message&&($usergroup->getFgmanager()||$message->getUser()==$user||$user->getRole()=="ROLE_ADMIN"||$user->getRole()=="ROLE_MODO" )) {
$id=$message->getId();
$this->em->remove($message);
$this->em->flush();
$topic->broadcast(['del' => $id]);
2019-07-17 17:06:05 +02:00
}
else $topic->broadcast(['log' => "Suppression interdite"]);
}
2019-07-16 12:10:32 +02:00
}
}
/**
* Like RPC is will use to prefix the channel
* @return string
*/
public function getName()
{
return 'websocket.topic';
}
}