From 030fd9a1196decc5814602f1e3121398af61ded2 Mon Sep 17 00:00:00 2001 From: afornerot Date: Fri, 19 Jul 2019 16:51:11 +0200 Subject: [PATCH] commit --- .../src/Cadoles/CoreBundle/Entity/Group.php | 2 +- .../src/Cadoles/CoreBundle/Entity/User.php | 41 ++++++- .../CoreBundle/Resources/public/css/style.css | 4 + .../CoreBundle/Resources/views/base.html.twig | 2 +- .../Controller/PageController.php | 50 +++++++- .../Controller/PagewidgetController.php | 2 +- .../src/Cadoles/PortalBundle/Entity/Page.php | 12 +- .../Repository/PageRepository.php | 23 ++++ .../Resources/views/Page/pages.html.twig | 108 ++++++++++++++++-- .../Controller/ChatController.php | 1 + .../WebsocketBundle/Entity/Message.php | 45 ++++++++ .../WebsocketBundle/RPC/RPCService.php | 1 + .../Resources/config/services.yml | 6 + .../Resources/config/topic.yml | 5 + .../Resources/views/Chat/client.html.twig | 21 ++-- .../Topic/WebsocketCounter.php | 100 ++++++++++++++++ .../WebsocketBundle/Topic/WebsocketTopic.php | 9 +- 17 files changed, 401 insertions(+), 31 deletions(-) create mode 100644 src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketCounter.php diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/Group.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/Group.php index 0a3463dd..4dcfbb5f 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/Group.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/Group.php @@ -153,7 +153,7 @@ class Group * @var ArrayCollection $message * @var Message * - * @ORM\OneToMany(targetEntity="Cadoles\WebsocketBundle\Entity\Message", mappedBy="user", cascade={"persist"}, orphanRemoval=true) + * @ORM\OneToMany(targetEntity="Cadoles\WebsocketBundle\Entity\Message", mappedBy="group", cascade={"persist"}, orphanRemoval=true) */ protected $messages; diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/User.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/User.php index 77c8882c..fd550b54 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/User.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Entity/User.php @@ -260,7 +260,12 @@ class User implements UserInterface, \Serializable * @ORM\OneToMany(targetEntity="Cadoles\WebsocketBundle\Entity\Message", mappedBy="user", cascade={"persist"}, orphanRemoval=true) */ private $messages; - + + /** + * @ORM\ManyToMany(targetEntity="Cadoles\WebsocketBundle\Entity\Message", mappedBy="readers") + */ + protected $messagereaders; + //== CODE A NE PAS REGENERER /** @@ -1382,4 +1387,38 @@ class User implements UserInterface, \Serializable { return $this->messages; } + + /** + * Add messagereader + * + * @param \Cadoles\WebsocketBundle\Entity\Message $messagereader + * + * @return User + */ + public function addMessagereader(\Cadoles\WebsocketBundle\Entity\Message $messagereader) + { + $this->messagereaders[] = $messagereader; + + return $this; + } + + /** + * Remove messagereader + * + * @param \Cadoles\WebsocketBundle\Entity\Message $messagereader + */ + public function removeMessagereader(\Cadoles\WebsocketBundle\Entity\Message $messagereader) + { + $this->messagereaders->removeElement($messagereader); + } + + /** + * Get messagereaders + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getMessagereaders() + { + return $this->messagereaders; + } } diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/public/css/style.css b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/public/css/style.css index d55535d7..3cc6cd67 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/public/css/style.css +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/public/css/style.css @@ -100,6 +100,10 @@ body { font-size: 35px; } +.navbar-top-links li a .badge { + margin-left: 10px; +} + @media (max-width: 767px) { .navbar-default .navbar-header #title { display: none; diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/base.html.twig b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/base.html.twig index 282d47bc..def1f202 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/base.html.twig +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/base.html.twig @@ -152,7 +152,7 @@ {% block localexternalscript %} {% endblock %} - + + + {% endif %} {% endblock %} {% block localjavascript %} @@ -235,7 +267,55 @@ responsive: true, iDisplayLength: 10, order: [[ 1, "asc" ]], - }); + }); + + // Init socket de counter + var _WS_URI = "wss://{{ gos_web_socket_server_host }}:{{ gos_web_socket_server_port }}"; + var _WS_URI = "wss://{{ gos_web_socket_server_host }}:5555"; + + var webSocket = WS.connect(_WS_URI); + var globalsession; + + webSocket.on("socket/connect", function (session) { + globalsession=session; + session.call("websocket/rpc/update_connection_data", {"userid": "{{app.user.id}}" }).then( + function (result) + { + //console.log("RPC Valid!", result); + }, + function (error, desc) + { + //console.log("RPC Error", error, desc); + } + ); + + // The callback function in "subscribe" is called everytime an event is published in that channel. + session.subscribe("websocket/counter", function (uri, payload) { + if(payload.log) { + console.log("Received message", payload.log); + } + if(payload.alert) { + alert(payload.alert); + } + if(payload.from!={{app.user.id}}) { + menu=$("a[data-group='"+payload.group+"']"); + if(menu.length) { + if(payload.add) { + console.log("Ajout counter pour group "+payload.group+" by "+payload.from); + if(menu.children(".badge").length) { + menu.children(".badge").html(+(menu.children(".badge").html())+1); + } + else menu.append("1"); + // Détruire le badge associé car normalement de fait on a lu les notif + if(usage=="group") { + $("#badge-"+groupid).remove() + } + // Cacher les actions possibles sur la page $("#menuupdate").hide(); @@ -377,4 +462,9 @@ $(location).attr('href', url); }; + // Permet de déclencher l'évenement de modification des counter + function counter(event) { + $('#refreshcounter').data("event",event); + $('#refreshcounter').click(); + }; {% endblock %} diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php index b3d98772..88089007 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php @@ -74,6 +74,7 @@ class ChatController extends Controller 'border' => $border, 'colorbodyback' => $colorbodyback, 'colorbodyfont' => $colorbodyfont, + 'fgmanager' => ($usergroup->getFgmanager()||$user->getRole()=="ROLE_ADMIN"||$user->getRole()=="ROLE_MODO"), 'form' => $form->createView() ]); } diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Entity/Message.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Entity/Message.php index 94c467c5..3de5d076 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Entity/Message.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Entity/Message.php @@ -40,6 +40,17 @@ class Message */ private $group; + /** + * @ORM\ManyToMany(targetEntity="Cadoles\CoreBundle\Entity\User", inversedBy="messagereaders", cascade={"persist"}) + * @ORM\JoinTable(name="messageuser", + * joinColumns={@ORM\JoinColumn(name="message", referencedColumnName="id")}, + * inverseJoinColumns={@ORM\JoinColumn(name="user", referencedColumnName="id")} + * ) + */ + protected $readers; + + + /** * Constructor */ @@ -155,4 +166,38 @@ class Message { return $this->group; } + + /** + * Add reader + * + * @param \Cadoles\CoreBundle\Entity\User $reader + * + * @return Message + */ + public function addReader(\Cadoles\CoreBundle\Entity\User $reader) + { + $this->readers[] = $reader; + + return $this; + } + + /** + * Remove reader + * + * @param \Cadoles\CoreBundle\Entity\User $reader + */ + public function removeReader(\Cadoles\CoreBundle\Entity\User $reader) + { + $this->readers->removeElement($reader); + } + + /** + * Get readers + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getReaders() + { + return $this->readers; + } } diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/RPC/RPCService.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/RPC/RPCService.php index 2af3d867..fe14c55b 100755 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/RPC/RPCService.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/RPC/RPCService.php @@ -21,6 +21,7 @@ class RPCService implements RpcInterface { */ public function updateConnectionData(ConnectionInterface $connection, WampRequest $request, $params) { $connection->userkey = $params['userkey']; + $connection->userid = $params['userid']; return array("result" => array_sum($params)); } diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/services.yml b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/services.yml index b08c02ba..afd6cf98 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/services.yml +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/services.yml @@ -5,6 +5,12 @@ services: tags: - { name: gos_web_socket.topic } + cadoles.websocket.counter: + class: Cadoles\WebsocketBundle\Topic\WebsocketCounter + arguments: ['@gos_web_socket.websocket.client_manipulator','@doctrine.orm.entity_manager','@service_container'] + tags: + - { name: gos_web_socket.topic } + cadoles.websocket.rpc: class: Cadoles\WebsocketBundle\RPC\RPCService tags: diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/topic.yml b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/topic.yml index 5a2b9dee..571f1e34 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/topic.yml +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/topic.yml @@ -6,6 +6,11 @@ websocket_topic: group: pattern: '[-+]?\d+' +websocket_counter: + channel: websocket/counter + handler: + callback: 'websocket.counter' + websocket_rpc: channel: websocket/rpc/{method} handler: diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig index b8e9c08c..c3731080 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig @@ -26,6 +26,7 @@ .msgavatar { float:left; height:40px; + text-align: center; } .msgdiv { float:left; @@ -52,7 +53,9 @@

- + {% if fgmanager or message.user == app.user %} + + {% endif %}
@@ -87,11 +90,11 @@ session.call("websocket/rpc/update_connection_data", {"userkey": "{{userkey}}" }).then( function (result) { - console.log("RPC Valid!", result); + //console.log("RPC Valid!", result); }, function (error, desc) { - console.log("RPC Error", error, desc); + //console.log("RPC Error", error, desc); } ); @@ -101,7 +104,7 @@ html ="
"; html+="
"; html+="
"; - if(payload.msg.userid=={{app.user.id}}) { + if(payload.msg.userid=={{app.user.id}} || '{{ fgmanager }}'=='1') { html+=""; } html+="
"; @@ -111,10 +114,8 @@ html+="
"; html+="
"; $(".mychat").prepend(html); - - - console.log("Received message by "+payload.msg.userid+" = ",payload.msg); + //console.log("Received message by "+payload.msg.userid+" = ",payload.msg); } if(payload.log) { @@ -136,14 +137,18 @@ event={type: "add", message: data}; session.publish("websocket/channel/{{groupid}}", event); CKEDITOR.instances["chat_message"].setData(''); + + event={type: "add", group:{{groupid}}}; + parent.parent.counter(event); } }); $(document).on('click', '.delmessage', function(){ - console.log("suppression"); id=$(this).data("id"); event={type: "del", id: id}; session.publish("websocket/channel/{{groupid}}", event); + + }); }) {% endif %} diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketCounter.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketCounter.php new file mode 100644 index 00000000..5cb07ff4 --- /dev/null +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketCounter.php @@ -0,0 +1,100 @@ +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) + { + $userid=$connection->userid; + //this will broadcast the message to ALL subscribers of this topic. + //$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()." ".$userid]); + + } + + /** + * 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. + //$topic->broadcast(['msg' => $connection->resourceId . " has left " . $topic->getId()]); + } + + + /** + * 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) + { + $userid=$connection->userid; + + $user=$this->em->getRepository("CadolesCoreBundle:User")->find($userid); + if(!$user) + return; + + $group=$this->em->getRepository("CadolesCoreBundle:Group")->find($event["group"]); + if(!$group) + return; + + $usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(["user"=>$user,"group"=>$group]); + if(!$usergroup) + return; + + $topic->broadcast([$event["type"] => 1, "group" => $event["group"] , "from" =>$userid]); + + + } + + /** + * Like RPC is will use to prefix the channel + * @return string + */ + public function getName() + { + return 'websocket.counter'; + } +} \ No newline at end of file diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php index ed9f9ffb..f7756de7 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php @@ -37,7 +37,7 @@ class WebsocketTopic implements TopicInterface public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request) { //this will broadcast the message to ALL subscribers of this topic. - $topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]); + //$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]); } @@ -107,8 +107,11 @@ class WebsocketTopic implements TopicInterface if($event["type"]=="del") { $message=$this->em->getRepository("CadolesWebsocketBundle:Message")->find($event["id"]); - if($message&&($usergroup->getFgmanager()||$message->getUser()==$user)) { - $topic->broadcast(['del' => $message->getId()]); + 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]); } else $topic->broadcast(['log' => "Suppression interdite"]); }