2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Presenters;
|
|
|
|
use Chandler\Signaling\SignalManager;
|
|
|
|
use openvk\Web\Events\NewMessageEvent;
|
|
|
|
use openvk\Web\Models\Repositories\{Users, Clubs, Messages};
|
|
|
|
use openvk\Web\Models\Entities\{Message, Correspondence};
|
|
|
|
|
|
|
|
final class MessengerPresenter extends OpenVKPresenter
|
|
|
|
{
|
|
|
|
private $messages;
|
|
|
|
private $signaler;
|
2022-09-17 00:19:46 +03:00
|
|
|
protected $presenterName = "messenger";
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
function __construct(Messages $messages)
|
|
|
|
{
|
|
|
|
$this->messages = $messages;
|
|
|
|
$this->signaler = SignalManager::i();
|
2022-09-17 00:19:46 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCorrespondent(int $id): object
|
|
|
|
{
|
|
|
|
if($id > 0)
|
|
|
|
return (new Users)->get($id);
|
|
|
|
else if($id < 0)
|
|
|
|
return (new Clubs)->get(abs($id));
|
|
|
|
else if($id === 0)
|
|
|
|
return $this->user->identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderIndex(): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
2022-09-17 00:19:46 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
if(isset($_GET["sel"]))
|
|
|
|
$this->pass("openvk!Messenger->app", $_GET["sel"]);
|
|
|
|
|
2022-05-31 17:39:52 +03:00
|
|
|
$page = (int) ($_GET["p"] ?? 1);
|
2020-06-07 19:04:43 +03:00
|
|
|
$correspondences = iterator_to_array($this->messages->getCorrespondencies($this->user->identity, $page));
|
2022-05-31 17:39:52 +03:00
|
|
|
|
|
|
|
// #КакаоПрокакалось
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
$this->template->corresps = $correspondences;
|
2022-05-31 17:39:52 +03:00
|
|
|
$this->template->paginatorConf = (object) [
|
|
|
|
"count" => $this->messages->getCorrespondenciesCount($this->user->identity),
|
|
|
|
"page" => (int) ($_GET["p"] ?? 1),
|
|
|
|
"amount" => sizeof($this->template->corresps),
|
|
|
|
"perPage" => OPENVK_DEFAULT_PER_PAGE,
|
|
|
|
];
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderApp(int $sel): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
|
|
|
|
|
|
|
$correspondent = $this->getCorrespondent($sel);
|
|
|
|
if(!$correspondent)
|
|
|
|
$this->notFound();
|
|
|
|
|
|
|
|
$this->template->selId = $sel;
|
|
|
|
$this->template->correspondent = $correspondent;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEvents(int $randNum): void
|
|
|
|
{
|
2020-08-01 17:29:22 +03:00
|
|
|
$this->assertUserLoggedIn();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2020-08-01 17:29:22 +03:00
|
|
|
header("Content-Type: application/json");
|
|
|
|
$this->signaler->listen(function($event, $id) {
|
2020-06-07 19:04:43 +03:00
|
|
|
exit(json_encode([[
|
|
|
|
"UUID" => $id,
|
|
|
|
"event" => $event->getLongPoolSummary(),
|
|
|
|
]]));
|
|
|
|
}, $this->user->id);
|
|
|
|
}
|
|
|
|
|
2020-08-01 17:29:22 +03:00
|
|
|
function renderVKEvents(int $id): void
|
|
|
|
{
|
2020-09-29 23:00:59 +03:00
|
|
|
header("Access-Control-Allow-Origin: *");
|
2020-08-01 17:29:22 +03:00
|
|
|
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;
|
2022-09-19 17:13:37 +03:00
|
|
|
|
|
|
|
$time = intval($this->queryParam("wait"));
|
|
|
|
|
|
|
|
if($time > 60)
|
|
|
|
$time = 60;
|
|
|
|
elseif($time == 0)
|
|
|
|
$time = 25; // default
|
2020-08-01 17:29:22 +03:00
|
|
|
|
|
|
|
$this->signaler->listen(function($event, $eId) use ($id) {
|
|
|
|
exit(json_encode([
|
|
|
|
"ts" => time(),
|
|
|
|
"updates" => [
|
|
|
|
$event->getVKAPISummary($id),
|
|
|
|
],
|
|
|
|
]));
|
2022-09-19 17:13:37 +03:00
|
|
|
}, $id, $time);
|
2020-08-01 17:29:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderApiGetMessages(int $sel, int $lastMsg): void
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
|
|
|
|
|
|
|
$correspondent = $this->getCorrespondent($sel);
|
|
|
|
if(!$correspondent)
|
|
|
|
$this->notFound();
|
|
|
|
|
|
|
|
$messages = [];
|
|
|
|
$correspondence = new Correspondence($this->user->identity, $correspondent);
|
2022-05-08 13:06:26 +03:00
|
|
|
foreach($correspondence->getMessages(1, $lastMsg === 0 ? NULL : $lastMsg) as $message)
|
2020-06-07 19:04:43 +03:00
|
|
|
$messages[] = $message->simplify();
|
|
|
|
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
exit(json_encode($messages));
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderApiWriteMessage(int $sel): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
if(empty($this->postParam("content"))) {
|
|
|
|
header("HTTP/1.1 400 Bad Request");
|
|
|
|
exit("<b>Argument error</b>: param 'content' expected to be string, undefined given.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$sel = $this->getCorrespondent($sel);
|
2021-12-28 12:40:14 +03:00
|
|
|
if($sel->getId() !== $this->user->id && !$sel->getPrivacyPermission('messages.write', $this->user->identity))
|
2020-06-07 19:04:43 +03:00
|
|
|
exit(header("HTTP/1.1 403 Forbidden"));
|
|
|
|
|
|
|
|
$cor = new Correspondence($this->user->identity, $sel);
|
|
|
|
$msg = new Message;
|
|
|
|
$msg->setContent($this->postParam("content"));
|
|
|
|
$cor->sendMessage($msg);
|
|
|
|
|
|
|
|
header("HTTP/1.1 202 Accepted");
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
exit(json_encode($msg->simplify()));
|
|
|
|
}
|
|
|
|
}
|