10 changed files with 268 additions and 70 deletions
-
51app/Message.php
-
57app/MessageBuffer.php
-
34app/Presence.php
-
60app/PresenceBuffer.php
-
2app/helpers/StringHelper.php
-
3lib/moxl/src/Moxl/Xec/Action/MAM/Get.php
-
55lib/moxl/src/Moxl/Xec/Action/Presence/Muc.php
-
13lib/moxl/src/Moxl/Xec/Payload/MAMResult.php
-
1lib/moxl/src/Moxl/Xec/Payload/Message.php
-
62lib/moxl/src/Moxl/Xec/Payload/Presence.php
@ -0,0 +1,57 @@ |
|||
<?php |
|||
|
|||
namespace App; |
|||
|
|||
use App\Message; |
|||
|
|||
class MessageBuffer |
|||
{ |
|||
protected static $instance; |
|||
private $_models = null; |
|||
private $_calls = null; |
|||
|
|||
public static function getInstance() |
|||
{ |
|||
if (!isset(self::$instance)) { |
|||
self::$instance = new self(); |
|||
} |
|||
|
|||
return self::$instance; |
|||
} |
|||
|
|||
public function __construct() |
|||
{ |
|||
$this->_models = collect(); |
|||
$this->_calls = collect(); |
|||
} |
|||
|
|||
public function save() |
|||
{ |
|||
if ($this->_models->isNotEmpty()) { |
|||
try { |
|||
Message::insert($this->_models->toArray()); |
|||
} catch (\Exception $e) { |
|||
\Utils::error($e->getMessage()); |
|||
} |
|||
$this->_models = collect(); |
|||
} |
|||
|
|||
if ($this->_calls->isNotEmpty()) { |
|||
$this->_calls->each(function ($call) { |
|||
$call(); |
|||
}); |
|||
$this->_calls = collect(); |
|||
} |
|||
} |
|||
|
|||
public function append(Message $message, $call) |
|||
{ |
|||
if ($message->created_at == null && $message->updated_at == null) { |
|||
$this->_models[$message->jidfrom.$message->id] = $message->toRawArray(); |
|||
$this->_calls->push($call); |
|||
} else { |
|||
$message->save(); |
|||
$call(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
<?php |
|||
|
|||
namespace App; |
|||
|
|||
use App\Presence; |
|||
|
|||
class PresenceBuffer |
|||
{ |
|||
protected static $instance; |
|||
private $_models = null; |
|||
private $_calls = null; |
|||
|
|||
public static function getInstance() |
|||
{ |
|||
if (!isset(self::$instance)) { |
|||
self::$instance = new self(); |
|||
} |
|||
|
|||
return self::$instance; |
|||
} |
|||
|
|||
public function __construct() |
|||
{ |
|||
global $loop; |
|||
|
|||
$this->_models = collect(); |
|||
$this->_calls = collect(); |
|||
|
|||
$loop->addPeriodicTimer(1, function () { |
|||
$this->save(); |
|||
}); |
|||
} |
|||
|
|||
public function save() |
|||
{ |
|||
if ($this->_models->isNotEmpty()) { |
|||
Presence::insert($this->_models->toArray()); |
|||
$this->_models = collect(); |
|||
} |
|||
|
|||
if ($this->_calls->isNotEmpty()) { |
|||
$this->_calls->each(function ($call) { |
|||
$call(); |
|||
}); |
|||
$this->_calls = collect(); |
|||
} |
|||
} |
|||
|
|||
public function append(Presence $presence, $call) |
|||
{ |
|||
// Only presences that can be inserted, not updated
|
|||
if ($presence->created_at == null) { |
|||
$this->_models[$presence->muc ? $presence->mucjid : $presence->jid.$presence->resource] = $presence->toArray(); |
|||
$this->_calls->push($call); |
|||
} else { |
|||
$presence->save(); |
|||
$call(); |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue