Browse Source

Fix messages filtering when getting the history

Add a small cache to prevent presence resolving several time in a row when grabbing MUC messages from the DB
pull/612/merge
Timothée Jaussoin 8 years ago
parent
commit
259aeb206f
  1. 16
      app/Session.php
  2. 38
      app/widgets/Chat/Chat.php
  3. 5
      app/widgets/Chat/chat.js

16
app/Session.php

@ -55,24 +55,24 @@ class Session extends Model
public function getUploadService()
{
return Capability::where('node', 'like', '%' . $this->host . '%')
->where('features', 'like', '%urn:xmpp:http:upload%')
->first();
->where('features', 'like', '%urn:xmpp:http:upload%')
->first();
}
public function getChatroomsServices()
{
return Capability::where('node', 'like', '%' . $this->host . '%')
->where('node', 'not like', '%@%')
->where('category', 'conference')
->get();
->where('node', 'not like', '%@%')
->where('category', 'conference')
->get();
}
public function getCommentsService()
{
return Capability::where('node', 'comments.' . $this->host)
->where('category', 'pubsub')
->where('type', 'service')
->first();
->where('category', 'pubsub')
->where('type', 'service')
->first();
}
public function loadMemory()

38
app/widgets/Chat/Chat.php

@ -23,6 +23,7 @@ class Chat extends \Movim\Widget\Base
{
private $_pagination = 50;
private $_wrapper = [];
private $_mucPresences = [];
function load()
{
@ -457,7 +458,7 @@ class Chat extends \Movim\Widget\Base
* @param string jid
* @param string time
*/
function ajaxGetHistory($jid, $date)
function ajaxGetHistory($jid, $date, $muc = false)
{
if (!$this->validateJid($jid)) return;
@ -466,10 +467,16 @@ class Chat extends \Movim\Widget\Base
$query->where('jidfrom', $jid)
->orWhere('jidto', $jid);
})
->where('published', '<', date(SQL_DATE, strtotime($date)))
->orderBy('published', 'desc')
->take($this->_pagination)
->get();
->where('published', '<', date(SQL_DATE, strtotime($date)));
$messages = $muc
? $messages->where('type', 'groupchat')->whereNull('subject')
: $messages->whereIn('type', ['chat', 'headline', 'invitation']);
$messages = $messages->orderBy('published', 'desc')
->take($this->_pagination)
->get();
if ($messages->count() > 0) {
Notification::append(false, $this->__('message.history', $messages->count()));
@ -729,17 +736,24 @@ class Chat extends \Movim\Widget\Base
if ($message->type == 'groupchat') {
$message->color = stringToColor($message->session_id . $message->resource . $message->type);
$presence = $this->user->session->presences()
->where('jid', $message->jidfrom)
->where('resource', $message->resource)
->first();
// Cache the resolved presences for a while
$key = $message->jidfrom.$message->resource;
if (!isset($this->mucPresences[$key])) {
$this->mucPresences[$key] = $this->user->session->presences()
->where('jid', $message->jidfrom)
->where('resource', $message->resource)
->where('muc', true)
->first();
}
if ($presence) {
if ($url = $presence->conferencePicture) {
if ($this->mucPresences[$key] && $this->mucPresences[$key] !== true) {
if ($url = $this->mucPresences[$key]->conferencePicture) {
$message->icon_url = $url;
}
$message->mine = ($presence->mucjid == $this->user->id);
$message->mine = ($this->mucPresences[$key]->mucjid == $this->user->id);
} else {
$this->mucPresences[$key] = true;
}
$message->icon = firstLetterCapitalize($message->resource);

5
app/widgets/Chat/chat.js

@ -268,7 +268,10 @@ var Chat = {
discussion.onscroll = function() {
if (this.scrollTop < 1
&& discussion.querySelectorAll('ul li p').length >= Chat.pagination) {
Chat_ajaxGetHistory(Chat.getTextarea().dataset.jid, Chat.currentDate);
Chat_ajaxGetHistory(
Chat.getTextarea().dataset.jid,
Chat.currentDate,
Chat.getTextarea().dataset.muc);
}
Chat.lastHeight = this.clientHeight;

Loading…
Cancel
Save