This commit is contained in:
afornerot 2019-07-17 17:06:05 +02:00
parent 58aa6c3d86
commit bf1c7299b1
2 changed files with 60 additions and 23 deletions

View File

@ -49,8 +49,11 @@
<div class="mychat"> <div class="mychat">
{% for message in messages %} {% for message in messages %}
<div class='message row'> <div id='message-{{message.id}}' class='message row'>
<div class='msgavatar'><img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/{{message.user.avatar}}' class='avatar'></div> <div class='msgavatar'>
<img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/{{message.user.avatar}}' class='avatar'><br>
<i class='delmessage fa fa-trash fa-fw' data-id='{{message.id}}' style='cursor: pointer;'></i>
</div>
<div class='msgdiv'> <div class='msgdiv'>
<div class='msgtitle'> <div class='msgtitle'>
{{message.user.lastname}} {{message.user.firstname}}<br> {{message.user.lastname}} {{message.user.firstname}}<br>
@ -76,7 +79,6 @@
{% if app.user %} {% if app.user %}
var _WS_URI = "wss://{{ gos_web_socket_server_host }}:{{ gos_web_socket_server_port }}"; 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 _WS_URI = "wss://{{ gos_web_socket_server_host }}:5555";
console.log(_WS_URI);
var webSocket = WS.connect(_WS_URI); var webSocket = WS.connect(_WS_URI);
@ -96,8 +98,13 @@
//the callback function in "subscribe" is called everytime an event is published in that channel. //the callback function in "subscribe" is called everytime an event is published in that channel.
session.subscribe("websocket/channel/{{groupid}}", function (uri, payload) { session.subscribe("websocket/channel/{{groupid}}", function (uri, payload) {
if(payload.msg) { if(payload.msg) {
html ="<div class='message row'>"; html ="<div id='message-"+payload.msg.id+"' class='message row'>";
html+="<div class='msgavatar'><img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/"+payload.msg.avatar+"' class='avatar'></div>"; html+="<div class='msgavatar'>";
html+="<img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/"+payload.msg.avatar+"' class='avatar'><br>";
if(payload.msg.userid=={{app.user.id}}) {
html+="<i class='delmessage fa fa-trash fa-fw' data-id='"+payload.msg.id+"' style='cursor: pointer;'></i>";
}
html+="</div>";
html+="<div class='msgdiv'>" html+="<div class='msgdiv'>"
html+="<div class='msgtitle'>"+payload.msg.lastname+"<br><small>"+new Date(payload.msg.submitdate.date).toLocaleDateString("fr-FR", dateoptions)+"</small></div>"; html+="<div class='msgtitle'>"+payload.msg.lastname+"<br><small>"+new Date(payload.msg.submitdate.date).toLocaleDateString("fr-FR", dateoptions)+"</small></div>";
html+="<div class='msgtopic'>"+payload.msg.message+"</div>"; html+="<div class='msgtopic'>"+payload.msg.message+"</div>";
@ -105,21 +112,41 @@
html+="</div>"; html+="</div>";
$(".mychat").prepend(html); $(".mychat").prepend(html);
console.log("Received message", payload.msg);
console.log("Received message by "+payload.msg.userid+" = ",payload.msg);
} }
if(payload.log) { if(payload.log) {
console.log("Received message", payload.log); console.log("Received message", payload.log);
} }
if(payload.del) {
$("#message-"+payload.del).remove();
}
if(payload.alert) {
alert(payload.alert);
}
}); });
$( "#sendbtn" ).click(function() { $( "#sendbtn" ).click(function() {
var data = CKEDITOR.instances["chat_message"].getData(); var data = CKEDITOR.instances["chat_message"].getData();
if(data) session.publish("websocket/channel/{{groupid}}", data); if(data) {
event={type: "add", message: data};
session.publish("websocket/channel/{{groupid}}", event);
CKEDITOR.instances["chat_message"].setData(''); CKEDITOR.instances["chat_message"].setData('');
}
});
$(document).on('click', '.delmessage', function(){
console.log("suppression");
id=$(this).data("id");
event={type: "del", id: id};
session.publish("websocket/channel/{{groupid}}", event);
}); });
}) })
{% endif %} {% endif %}
}); });
{% endblock %} {% endblock %}

View File

@ -79,29 +79,39 @@ class WebsocketTopic implements TopicInterface
$usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=> $userkey]); $usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=> $userkey]);
if(!$usergroup) { if(!$usergroup) {
$topic->broadcast(['log' => 'user NOT IN GROUP = '.$userkey]); $topic->broadcast(['log' => 'user NOT IN GROUP']);
} }
else { else {
$user=$usergroup->getUser(); $user=$usergroup->getUser();
} }
if($group&&$usergroup&&$user) { if($group&&$usergroup&&$user) {
if($event["type"]=="add") {
$message=new Message(); $message=new Message();
$message->setTopic($event); $message->setTopic($event["message"]);
$message->setUser($user); $message->setUser($user);
$message->setGroup($group); $message->setGroup($group);
$this->em->persist($message); $this->em->persist($message);
$this->em->flush(); $this->em->flush();
$return["id"]=$message->getId();
$return["lastname"]=$user->getLastname()." ".$user->getFirstname(); $return["lastname"]=$user->getLastname()." ".$user->getFirstname();
$return["avatar"]=$user->getAvatar(); $return["avatar"]=$user->getAvatar();
$return["submitdate"]=$message->getSubmitdate(); $return["submitdate"]=$message->getSubmitdate();
$return["message"]=$event; $return["message"]=$event["message"];
$return["userid"]=$user->getId();
//this will broadcast the message to ALL subscribers of this topic. //this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast([ $topic->broadcast(['msg' => $return]);
'msg' => $return, }
]);
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()]);
}
else $topic->broadcast(['log' => "Suppression interdite"]);
}
} }
} }