openvk/VKAPI/Handlers/Notifications.php
Alexander Minkin 6ec54a379d
feat: add linting of code (#1220)
* feat(lint): add php-cs-fixer for linting

Removing previous CODE_STYLE as it was not enforced anyway and using PER-CS 2.0.

This is not the reformatting commit.

* style: format code according to PER-CS 2.0 with php-cs-fixer

* ci(actions): add lint action

Resolves #1132.
2025-01-31 18:20:13 +03:00

91 lines
2.6 KiB
PHP

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