Add ability to fetch history of events

This commit is contained in:
Jill Stingray 2020-08-01 14:12:51 +00:00
parent f3106105b4
commit 3093be04a6

View file

@ -81,6 +81,43 @@ class SignalManager
exit("[]"); exit("[]");
} }
/**
* Gets creation time of user's last event.
* If there is no events returns 1.
*
* @api
* @param int $for User ID
* @return int
*/
function tipFor(int $for): int
{
$statement = $this->connection->query("SELECT since FROM pool WHERE `for` = $for ORDER BY since DESC");
$result = $statement->fetch(\PDO::FETCH_LAZY);
if(!$result) return 1;
return $result->since;
}
/**
* Gets history of long pool events.
* If there is no events returns empty array.
*
* @api
* @param int $for User ID
* @param int|null $tip last sync time
* @return array
*/
function getHistoryFor(int $for, ?int $tip = NULL, int $limit = 1000): array
{
$res = [];
$tip = $tip ?? $this->tipFor($for);
$query = $this->connection->query("SELECT * FROM pool WHERE `for` = $for AND `since` > $tip ORDER BY since DESC LIMIT $limit");
foreach($query as $event)
$res[] = unserialize(hex2bin($event["event"]));
return $res;
}
/** /**
* Triggers event for user and sends signal to DB and listeners. * Triggers event for user and sends signal to DB and listeners.
* *
@ -99,4 +136,4 @@ class SignalManager
} }
use TSimpleSingleton; use TSimpleSingleton;
} }