mirror of
https://github.com/openvk/openvk
synced 2024-11-13 10:39:24 +03:00
Add VK longpool event stream logic (no route)
This commit is contained in:
parent
9303dd6562
commit
a251e3c3a7
2 changed files with 55 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
namespace openvk\Web\Events;
|
namespace openvk\Web\Events;
|
||||||
use openvk\Web\Models\Entities\Message;
|
use openvk\Web\Models\Entities\Message;
|
||||||
|
use openvk\Web\Models\Repositories\Messages;
|
||||||
|
|
||||||
class NewMessageEvent implements ILPEmitable
|
class NewMessageEvent implements ILPEmitable
|
||||||
{
|
{
|
||||||
|
@ -18,4 +19,22 @@ class NewMessageEvent implements ILPEmitable
|
||||||
"message" => $this->payload,
|
"message" => $this->payload,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
function getVKAPISummary(int $userId): array
|
||||||
|
{
|
||||||
|
$msg = (new Messages)->get($this->payload["uuid"]);
|
||||||
|
$peer = $msg->getSender()->getId();
|
||||||
|
if($peer === $userId)
|
||||||
|
$peer = $msg->getRecipient()->getId();
|
||||||
|
|
||||||
|
return [
|
||||||
|
4, # event type
|
||||||
|
256, # checked for spam flag
|
||||||
|
$peer, # TODO calculate peer correctly
|
||||||
|
$msg->getSendTime()->timestamp(), # creation time in unix
|
||||||
|
$msg->getText(), # text (formatted)
|
||||||
|
[], # empty attachments
|
||||||
|
$msg->getId() << 2, # id as random_id
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -54,10 +54,10 @@ final class MessengerPresenter extends OpenVKPresenter
|
||||||
|
|
||||||
function renderEvents(int $randNum): void
|
function renderEvents(int $randNum): void
|
||||||
{
|
{
|
||||||
$this->assertUserLoggedIn();
|
$this->assertUserLoggedIn();
|
||||||
|
|
||||||
header("Content-Type: application/json");
|
header("Content-Type: application/json");
|
||||||
$this->signaler->listen(function($event, $id) {
|
$this->signaler->listen(function($event, $id) {
|
||||||
exit(json_encode([[
|
exit(json_encode([[
|
||||||
"UUID" => $id,
|
"UUID" => $id,
|
||||||
"event" => $event->getLongPoolSummary(),
|
"event" => $event->getLongPoolSummary(),
|
||||||
|
@ -65,7 +65,37 @@ final class MessengerPresenter extends OpenVKPresenter
|
||||||
}, $this->user->id);
|
}, $this->user->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderApiGetMessages(int $sel, int $offset): void
|
function renderVKEvents(int $id): void
|
||||||
|
{
|
||||||
|
header("Content-Type: application/json");
|
||||||
|
|
||||||
|
if($this->queryParam("act") !== "a_check")
|
||||||
|
exit(header("HTTP/1.1 400 Bad Request"));
|
||||||
|
else if(!$this->queryParam("key"))
|
||||||
|
exit(header("HTTP/1.1 403 Forbidden"));
|
||||||
|
|
||||||
|
$key = $this->queryParam("key");
|
||||||
|
$payload = hex2bin(substr($key, 0, 16));
|
||||||
|
$signature = hex2bin(substr($key, 16));
|
||||||
|
if(($signature ^ ( ~CHANDLER_ROOT_CONF["security"]["secret"] | ((string) $id))) !== $payload) {
|
||||||
|
exit(json_encode([
|
||||||
|
"failed" => 3,
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
$legacy = $this->queryParam("version") < 3;
|
||||||
|
|
||||||
|
$this->signaler->listen(function($event, $eId) use ($id) {
|
||||||
|
exit(json_encode([
|
||||||
|
"ts" => time(),
|
||||||
|
"updates" => [
|
||||||
|
$event->getVKAPISummary($id),
|
||||||
|
],
|
||||||
|
]));
|
||||||
|
}, $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderApiGetMessages(int $sel, int $lastMsg): void
|
||||||
{
|
{
|
||||||
$this->assertUserLoggedIn();
|
$this->assertUserLoggedIn();
|
||||||
|
|
||||||
|
@ -75,7 +105,7 @@ final class MessengerPresenter extends OpenVKPresenter
|
||||||
|
|
||||||
$messages = [];
|
$messages = [];
|
||||||
$correspondence = new Correspondence($this->user->identity, $correspondent);
|
$correspondence = new Correspondence($this->user->identity, $correspondent);
|
||||||
foreach($correspondence->getMessages($offset === 0 ? null : $offset) as $message)
|
foreach($correspondence->getMessages(1, $lastMsg === 0 ? null : $lastMsg) as $message)
|
||||||
$messages[] = $message->simplify();
|
$messages[] = $message->simplify();
|
||||||
|
|
||||||
header("Content-Type: application/json");
|
header("Content-Type: application/json");
|
||||||
|
|
Loading…
Reference in a new issue