Add VK longpool event stream logic (no route)

This commit is contained in:
Alma Armas 2020-08-01 14:29:22 +00:00
parent 9303dd6562
commit a251e3c3a7
2 changed files with 55 additions and 6 deletions

View file

@ -1,6 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\Web\Events;
use openvk\Web\Models\Entities\Message;
use openvk\Web\Models\Repositories\Messages;
class NewMessageEvent implements ILPEmitable
{
@ -18,4 +19,22 @@ class NewMessageEvent implements ILPEmitable
"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
];
}
}

View file

@ -54,10 +54,10 @@ final class MessengerPresenter extends OpenVKPresenter
function renderEvents(int $randNum): void
{
$this->assertUserLoggedIn();
$this->assertUserLoggedIn();
header("Content-Type: application/json");
$this->signaler->listen(function($event, $id) {
header("Content-Type: application/json");
$this->signaler->listen(function($event, $id) {
exit(json_encode([[
"UUID" => $id,
"event" => $event->getLongPoolSummary(),
@ -65,7 +65,37 @@ final class MessengerPresenter extends OpenVKPresenter
}, $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();
@ -75,7 +105,7 @@ final class MessengerPresenter extends OpenVKPresenter
$messages = [];
$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();
header("Content-Type: application/json");