Browse Source

Don't reload the whole discussion on reconnect but simply append the new messages (using Chat_ajaxGetHistory)

pull/736/head
Timothée Jaussoin 7 years ago
parent
commit
f125082bfe
  1. 4
      app/assets/js/movim_websocket.js
  2. 18
      app/widgets/Chat/Chat.php
  3. 34
      app/widgets/Chat/chat.js

4
app/assets/js/movim_websocket.js

@ -42,9 +42,9 @@ var MovimWebsocket = {
init : function() {
if (SECURE_WEBSOCKET) {
var uri = 'wss:' + BASE_URI + '/ws/';
var uri = 'wss:' + BASE_URI + 'ws/';
} else {
var uri = 'ws:' + BASE_URI + '/ws/';
var uri = 'ws:' + BASE_URI + 'ws/';
}
if (this.connection

18
app/widgets/Chat/Chat.php

@ -406,13 +406,13 @@ class Chat extends \Movim\Widget\Base
*/
function ajaxHttpCorrect($to, $message)
{
$m = $this->user->messages()
$replace = $this->user->messages()
->where('jidto', $to)
->orderBy('published', 'desc')
->first();
if ($m) {
$this->ajaxHttpSendMessage($to, $message, false, false, $m);
if ($replace) {
$this->ajaxHttpSendMessage($to, $message, false, false, $replace);
}
}
@ -469,7 +469,7 @@ class Chat extends \Movim\Widget\Base
* @param string jid
* @param string time
*/
function ajaxGetHistory($jid, $date, $muc = false)
function ajaxGetHistory($jid, $date, $muc = false, $prepend = true)
{
if (!$this->validateJid($jid)) return;
@ -478,7 +478,7 @@ class Chat extends \Movim\Widget\Base
$query->where('jidfrom', $jid)
->orWhere('jidto', $jid);
})
->where('published', '<', date(SQL_DATE, strtotime($date)));
->where('published', $prepend ? '<' : '>', date(SQL_DATE, strtotime($date)));
$messages = $muc
@ -490,7 +490,11 @@ class Chat extends \Movim\Widget\Base
->get();
if ($messages->count() > 0) {
Notification::append(false, $this->__('message.history', $messages->count()));
if ($prepend) {
Notification::append(false, $this->__('message.history', $messages->count()));
} else {
$messages = $messages->reverse();
}
foreach($messages as $message) {
if (!$message->isOTR()) {
@ -498,7 +502,7 @@ class Chat extends \Movim\Widget\Base
}
}
$this->rpc('Chat.appendMessagesWrapper', $this->_wrapper, true);
$this->rpc('Chat.appendMessagesWrapper', $this->_wrapper, $prepend);
$this->_wrapper = [];
}
}

34
app/widgets/Chat/chat.js

@ -254,17 +254,6 @@ var Chat = {
MovimUtils.textareaAutoheight(textarea);
textarea.focus();
},
notify : function(title, body, image)
{
if (document_focus == false) {
movim_title_inc();
movim_desktop_notification(title, body, image);
}
},
empty : function()
{
Chat_ajaxGet();
},
setBubbles : function(left, right, date, separator) {
var div = document.createElement('div');
@ -278,8 +267,6 @@ var Chat = {
Chat.date = div.firstChild.cloneNode(true);
div.innerHTML = separator;
Chat.separator = div.firstChild.cloneNode(true);
Chat.setScrollBehaviour();
},
setScrollBehaviour : function() {
var discussion = document.querySelector('#chat_widget div.contained');
@ -289,7 +276,8 @@ var Chat = {
Chat_ajaxGetHistory(
Chat.getTextarea().dataset.jid,
Chat.currentDate,
Chat.getTextarea().dataset.muc);
Chat.getTextarea().dataset.muc,
true);
}
Chat.lastHeight = this.clientHeight;
@ -372,6 +360,8 @@ var Chat = {
discussion.querySelector('.placeholder').classList.add('show');
}
}
Chat.setScrollBehaviour();
},
appendMessage : function(idjidtime, data, prepend) {
if (data.body == null) return;
@ -697,14 +687,18 @@ var Chat = {
MovimWebsocket.attach(function() {
var jid = MovimUtils.urlParts().params[0];
var room = MovimUtils.urlParts().params[1];
var room = (MovimUtils.urlParts().params[1] === 'room');
if (jid) {
MovimTpl.showPanel();
if (room) {
Chat_ajaxGetRoom(jid, Boolean(document.getElementById(MovimUtils.cleanupId(jid) + '-conversation')));
if (Boolean(document.getElementById(MovimUtils.cleanupId(jid) + '-conversation'))) {
Chat_ajaxGetHistory(jid, Chat.currentDate, room, false);
} else {
Chat_ajaxGet(jid, Boolean(document.getElementById(MovimUtils.cleanupId(jid) + '-conversation')));
MovimTpl.showPanel();
if (room) {
Chat_ajaxGetRoom(jid);
} else {
Chat_ajaxGet(jid);
}
}
}
});

Loading…
Cancel
Save