Skip to content

Commit

Permalink
Remove the saved state system from the PresenceBuffer to a simpler tr…
Browse files Browse the repository at this point in the history
…ansaction based deletion system
  • Loading branch information
edhelas committed Oct 15, 2020
1 parent 58e982e commit 7a37b34
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
13 changes: 2 additions & 11 deletions app/Presence.php
Expand Up @@ -2,6 +2,8 @@

namespace App;

use Illuminate\Database\Capsule\Manager as DB;

use Movim\Model;
use Movim\Picture;
use Movim\Session;
Expand Down Expand Up @@ -104,19 +106,8 @@ public function getRefreshableAttribute()

public static function findByStanza($stanza)
{
$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;
}

Expand Down
45 changes: 24 additions & 21 deletions app/PresenceBuffer.php
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Illuminate\Database\Capsule\Manager as DB;
use App\Presence;

class PresenceBuffer
Expand All @@ -10,9 +11,6 @@ class PresenceBuffer
private $_models = null;
private $_calls = null;

// Historically processed presences, to prevent useless DB lookup
private $_saved = [];

public static function getInstance()
{
if (!isset(self::$instance)) {
Expand All @@ -34,17 +32,34 @@ public function __construct()
});
}

public function saved(Presence $presence)
{
return array_key_exists($this->getPresenceKey($presence), $this->_saved);
}

public function save()
{
if ($this->_models->isNotEmpty()) {
if ($this->_models->count() > 0) {
try {
DB::beginTransaction();

// We delete all the presences that might already be there
$table = DB::table('presences');
$first = $this->_models->shift();
$table = $table->where([
['session_id', $first['session_id']],
['jid', $first['jid']],
['resource', $first['resource']],
]);
$this->_models->each(function ($presence) use ($table) {
$table->orWhere([
['session_id', $presence['session_id']],
['jid', $presence['jid']],
['resource', $presence['resource']],
]);
});
$table->delete();

// And we save it
Presence::insert($this->_models->toArray());
DB::commit();
} catch (\Exception $e) {
DB::rollback();
\Utils::error($e->getMessage());
}
$this->_models = collect();
Expand All @@ -60,21 +75,9 @@ public function save()

public function append(Presence $presence, $call)
{
// Only presences that can be inserted, not updated
if ($presence->created_at == null) {
$key = $this->getPresenceKey($presence);
$this->_saved[$key] = 1;
$this->_models[$key] = $presence->toArray();
$this->_calls->push($call);
} else {
$presence->save();
$call();
}
}

public function remove(Presence $presence)
{
unset($this->_saved[$this->getPresenceKey($presence)]);
}

private function getPresenceKey(Presence $presence)
Expand Down
5 changes: 0 additions & 5 deletions app/widgets/Rooms/Rooms.php
Expand Up @@ -266,11 +266,6 @@ public function ajaxExit($room)

// We clear the presences from the buffer cache and then the DB
$pb = PresenceBuffer::getInstance();
$this->user->session->conferences()
->where('conference', $room)
->first()->presences->each(function ($presence) use ($pb) {
$pb->remove($presence);
});
$this->user->session->conferences()
->where('conference', $room)
->first()->presences()->delete();
Expand Down
2 changes: 0 additions & 2 deletions lib/moxl/src/Xec/Payload/Presence.php
Expand Up @@ -49,7 +49,6 @@ public function handle($stanza, $parent = false)
}

if ($cCount > 1) {
PresenceBuffer::getInstance()->remove($presence);
$presence->delete();
break;
}
Expand All @@ -72,7 +71,6 @@ public function handle($stanza, $parent = false)

if ($presence->value == 5 && !empty($presence->resource)) {
$presence->delete();
PresenceBuffer::getInstance()->remove($presence);
}
}

Expand Down

0 comments on commit 7a37b34

Please sign in to comment.