2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-11-14 22:44:39 +03:00
|
|
|
namespace openvk\VKAPI\Handlers;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2023-11-14 22:44:39 +03:00
|
|
|
use openvk\Web\Models\Entities\Club;
|
|
|
|
use openvk\Web\Models\Repositories\{Notifications as Notifs, Clubs, Users};
|
|
|
|
|
|
|
|
final class Notifications extends VKAPIRequestHandler
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
public function get(
|
|
|
|
int $count = 10,
|
|
|
|
string $from = "",
|
|
|
|
int $offset = 0,
|
|
|
|
string $start_from = "",
|
|
|
|
string $filters = "",
|
|
|
|
int $start_time = 0,
|
|
|
|
int $end_time = 0,
|
|
|
|
int $archived = 0
|
|
|
|
) {
|
2023-11-14 22:44:39 +03:00
|
|
|
$this->requireUser();
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
$res = (object) [
|
2023-11-14 22:44:39 +03:00
|
|
|
"items" => [],
|
|
|
|
"profiles" => [],
|
|
|
|
"groups" => [],
|
2025-01-31 18:20:13 +03:00
|
|
|
"last_viewed" => $this->getUser()->getNotificationOffset(),
|
2023-11-14 22:44:39 +03:00
|
|
|
];
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($count > 100) {
|
2023-11-14 22:44:39 +03:00
|
|
|
$this->fail(125, "Count is too big");
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2023-11-14 22:44:39 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!eventdb()) {
|
2023-11-14 22:44:39 +03:00
|
|
|
$this->fail(1289, "EventDB is disabled on this instance");
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2023-11-14 22:44:39 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
$notifs = array_slice(iterator_to_array((new Notifs())->getNotificationsByUser($this->getUser(), $this->getUser()->getNotificationOffset(), (bool) $archived, 1, $offset + $count)), $offset);
|
2023-11-14 22:44:39 +03:00
|
|
|
$tmpProfiles = [];
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($notifs as $notif) {
|
2023-11-14 22:44:39 +03:00
|
|
|
$sxModel = $notif->getModel(1);
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!method_exists($sxModel, "getAvatarUrl")) {
|
2023-11-14 22:44:39 +03:00
|
|
|
$sxModel = $notif->getModel(0);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2023-11-14 22:44:39 +03:00
|
|
|
|
|
|
|
$tmpProfiles[] = $sxModel instanceof Club ? $sxModel->getId() * -1 : $sxModel->getId();
|
|
|
|
$res->items[] = $notif->toVkApiStruct();
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach (array_unique($tmpProfiles) as $id) {
|
|
|
|
if ($id > 0) {
|
|
|
|
$sxModel = (new Users())->get($id);
|
|
|
|
$result = (object) [
|
2023-11-14 22:44:39 +03:00
|
|
|
"uid" => $sxModel->getId(),
|
|
|
|
"first_name" => $sxModel->getFirstName(),
|
|
|
|
"last_name" => $sxModel->getLastName(),
|
|
|
|
"photo" => $sxModel->getAvatarUrl(),
|
|
|
|
"photo_medium_rec" => $sxModel->getAvatarUrl("tiny"),
|
2025-01-31 18:20:13 +03:00
|
|
|
"screen_name" => $sxModel->getShortCode(),
|
2023-11-14 22:44:39 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$res->profiles[] = $result;
|
|
|
|
} else {
|
2025-01-31 18:20:13 +03:00
|
|
|
$sxModel = (new Clubs())->get(abs($id));
|
2023-11-14 22:44:39 +03:00
|
|
|
$result = $sxModel->toVkApiStruct($this->getUser());
|
|
|
|
|
|
|
|
$res->groups[] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function markAsViewed()
|
2023-11-14 22:44:39 +03:00
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
$this->willExecuteWriteAction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getUser()->updateNotificationOffset();
|
|
|
|
$this->getUser()->save();
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (\Throwable $e) {
|
2023-11-14 22:44:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|