Browse Source

Add a filter in PresenceBuffer to tell if a presence needs to be resolved from the DB the first time

pull/932/head
Timothée Jaussoin 6 years ago
parent
commit
323ad7f2fe
  1. 22
      app/Presence.php
  2. 18
      app/PresenceBuffer.php

22
app/Presence.php

@ -6,6 +6,8 @@ use Movim\Model;
use Movim\Picture; use Movim\Picture;
use Movim\Session; use Movim\Session;
use App\PresenceBuffer;
class Presence extends Model class Presence extends Model
{ {
protected $primaryKey = ['session_id', 'jid', 'resource']; protected $primaryKey = ['session_id', 'jid', 'resource'];
@ -86,12 +88,20 @@ class Presence extends Model
public static function findByStanza($stanza) public static function findByStanza($stanza)
{ {
$jid = explode('/', (string)$stanza->attributes()->from);
return self::firstOrNew([
'session_id' => SESSION_ID,
'jid' => $jid[0],
'resource' => isset($jid[1]) ? $jid[1] : ''
]);
$buffer = PresenceBuffer::getInstance();
$temporary = new self;
$temporary->set($stanza);
if ($buffer->saved($temporary)) {
$jid = explode('/', (string)$stanza->attributes()->from);
return self::firstOrNew([
'session_id' => SESSION_ID,
'jid' => $jid[0],
'resource' => isset($jid[1]) ? $jid[1] : ''
]);
}
return $temporary;
} }
public function set($stanza) public function set($stanza)

18
app/PresenceBuffer.php

@ -10,6 +10,9 @@ class PresenceBuffer
private $_models = null; private $_models = null;
private $_calls = null; private $_calls = null;
// Historically processed presences, to prevent useless DB lookup
private $_saved = [];
public static function getInstance() public static function getInstance()
{ {
if (!isset(self::$instance)) { if (!isset(self::$instance)) {
@ -25,12 +28,18 @@ class PresenceBuffer
$this->_models = collect(); $this->_models = collect();
$this->_calls = collect(); $this->_calls = collect();
$this->_saved = collect();
$loop->addPeriodicTimer(1, function () { $loop->addPeriodicTimer(1, function () {
$this->save(); $this->save();
}); });
} }
public function saved(Presence $presence)
{
return $this->_saved->contains($this->getPresenceKey($presence));
}
public function save() public function save()
{ {
if ($this->_models->isNotEmpty()) { if ($this->_models->isNotEmpty()) {
@ -54,11 +63,18 @@ class PresenceBuffer
{ {
// Only presences that can be inserted, not updated // Only presences that can be inserted, not updated
if ($presence->created_at == null) { if ($presence->created_at == null) {
$this->_models[$presence->muc ? $presence->jid.$presence->mucjid : $presence->jid.$presence->resource] = $presence->toArray();
$key = $this->getPresenceKey($presence);
$this->_saved->push($key);
$this->_models[$key] = $presence->toArray();
$this->_calls->push($call); $this->_calls->push($call);
} else { } else {
$presence->save(); $presence->save();
$call(); $call();
} }
} }
private function getPresenceKey(Presence $presence)
{
return $presence->muc ? $presence->jid.$presence->mucjid : $presence->jid.$presence->resource;
}
} }
Loading…
Cancel
Save