diff --git a/ServiceAPI/Wall.php b/ServiceAPI/Wall.php index f94479ba..102c50ce 100644 --- a/ServiceAPI/Wall.php +++ b/ServiceAPI/Wall.php @@ -164,7 +164,7 @@ class Wall implements Handler } if(rand(0, 200) == 50) { - $arr["fact"] = $this->user->getUsersIgnoredCount(); + $arr["fact"] = $this->user->getIgnoresCount(); } $resolve($arr); diff --git a/VKAPI/Handlers/Newsfeed.php b/VKAPI/Handlers/Newsfeed.php index 02dfb645..fc876b5f 100644 --- a/VKAPI/Handlers/Newsfeed.php +++ b/VKAPI/Handlers/Newsfeed.php @@ -47,7 +47,7 @@ final class Newsfeed extends VKAPIRequestHandler return $response; } - function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0) + function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $return_banned = 0) { $this->requireUser(); @@ -57,7 +57,7 @@ final class Newsfeed extends VKAPIRequestHandler if($this->getUser()->getNsfwTolerance() === User::NSFW_INTOLERANT) $queryBase .= " AND `nsfw` = 0"; - if(($ignoredCount = $this->getUser()->getIgnoredSourcesCount()) > 0) { + if(($ignoredCount = $this->getUser()->getIgnoredSourcesCount()) > 0 && $return_banned == 0) { $sources = implode("', '", $this->getUser()->getIgnoredSources(1, $ignoredCount, true)); $queryBase .= " AND `posts`.`wall` NOT IN ('$sources')"; @@ -128,4 +128,82 @@ final class Newsfeed extends VKAPIRequestHandler return $retArr; } } + + function addBan(string $user_ids = "", string $group_ids = "") + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + if(empty($user_ids) && empty($group_ids)) + $this->fail(52, "Provide 'user_ids' or 'groups_ids'"); + + $arr = []; + + if(!empty($user_ids)) { + $arr = array_merge($arr, array_map(function($el) { + return (int)$el; + }, explode(",", $user_ids))); + } + + if(!empty($group_ids)) { + $arr = array_merge($arr, array_map(function($el) { + return abs((int)$el) * -1; + }, explode(",", $group_ids))); + } + + $arr = array_unique($arr); + if(sizeof($arr) > 10 || sizeof($arr) < 1) + $this->fail(20, "You can ignore only 10 users/groups at once"); + + $successes = 0; + foreach($arr as $ar) { + $entity = getEntity($ar); + + if(!$entity || $entity->isHideFromGlobalFeedEnabled() || $entity->isIgnoredBy($this->getUser())) continue; + + $entity->toggleIgnore($this->getUser()); + $successes += 1; + } + + return (int)($successes > 0); + } + + function deleteBan(string $user_ids = "", string $group_ids = "") + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + if(empty($user_ids) && empty($group_ids)) + $this->fail(52, "Provide 'user_ids' or 'groups_ids'"); + + $arr = []; + + if(!empty($user_ids)) { + $arr = array_merge($arr, array_map(function($el) { + return (int)$el; + }, explode(",", $user_ids))); + } + + if(!empty($group_ids)) { + $arr = array_merge($arr, array_map(function($el) { + return abs((int)$el) * -1; + }, explode(",", $group_ids))); + } + + $arr = array_unique($arr); + if(sizeof($arr) > 10 || sizeof($arr) < 1) + $this->fail(20, "You can unignore only 10 users/groups at once"); + + $successes = 0; + foreach($arr as $ar) { + $entity = getEntity($ar); + + if(!$entity || $entity->isHideFromGlobalFeedEnabled() || !$entity->isIgnoredBy($this->getUser())) continue; + + $entity->toggleIgnore($this->getUser()); + $successes += 1; + } + + return (int)($successes > 0); + } } diff --git a/Web/Models/Entities/Club.php b/Web/Models/Entities/Club.php index 69a3583d..cbbe622b 100644 --- a/Web/Models/Entities/Club.php +++ b/Web/Models/Entities/Club.php @@ -461,29 +461,8 @@ class Club extends RowModel return $res; } - function isIgnoredBy(User $user): bool - { - $ctx = DB::i()->getContext(); - $data = [ - "owner" => $user->getId(), - "ignored_source" => $this->getId() * -1, - ]; - - $sub = $ctx->table("ignored_sources")->where($data); - - if(!$sub->fetch()) { - return false; - } - - return true; - } - - function getRealId() - { - return $this->getId() * -1; - } - use Traits\TBackDrops; use Traits\TSubscribable; use Traits\TAudioStatuses; + use Traits\TIgnorable; } diff --git a/Web/Models/Entities/Traits/TIgnorable.php b/Web/Models/Entities/Traits/TIgnorable.php new file mode 100644 index 00000000..a3bbc1a1 --- /dev/null +++ b/Web/Models/Entities/Traits/TIgnorable.php @@ -0,0 +1,48 @@ +getContext(); + $data = [ + "owner" => $user->getId(), + "ignored_source" => $this->getRealId(), + ]; + + $sub = $ctx->table("ignored_sources")->where($data); + + if(!$sub->fetch()) { + return false; + } + + return true; + } + + function getIgnoresCount() + { + return sizeof(DatabaseConnection::i()->getContext()->table("ignored_sources")->where("ignored_source", $this->getRealId())); + } + + function toggleIgnore(User $user): bool + { + if($this->isIgnoredBy($user)) { + DatabaseConnection::i()->getContext()->table("ignored_sources")->where([ + "owner" => $user->getId(), + "ignored_source" => $this->getRealId(), + ])->delete(); + + return false; + } else { + DatabaseConnection::i()->getContext()->table("ignored_sources")->insert([ + "owner" => $user->getId(), + "ignored_source" => $this->getRealId(), + ]); + + return true; + } + } +} diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index d3a11b9f..697abdde 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -1368,28 +1368,6 @@ class User extends RowModel { return sizeof(DatabaseConnection::i()->getContext()->table("ignored_sources")->where("owner", $this->getId())); } - - function isIgnoredBy(User $user): bool - { - $ctx = DatabaseConnection::i()->getContext(); - $data = [ - "owner" => $user->getId(), - "ignored_source" => $this->getId(), - ]; - - $sub = $ctx->table("ignored_sources")->where($data); - - if(!$sub->fetch()) { - return false; - } - - return true; - } - - function getUsersIgnoredCount() - { - return sizeof(DatabaseConnection::i()->getContext()->table("ignored_sources")->where("ignored_source", $this->getId())); - } function getAudiosCollectionSize() { @@ -1430,7 +1408,13 @@ class User extends RowModel return $returnArr; } + function isHideFromGlobalFeedEnabled(): bool + { + return $this->isClosed(); + } + use Traits\TBackDrops; use Traits\TSubscribable; use Traits\TAudioStatuses; + use Traits\TIgnorable; } diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index b56170b3..04cf44ed 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -679,23 +679,20 @@ final class WallPresenter extends OpenVKPresenter if($ignoredSourceModel->getId() == $this->user->id) $this->flashFail("err", "Error", tr("cant_ignore_self"), null, true); + + if($ignoredSourceModel->isClosed()) + $this->flashFail("err", "Error", tr("no_sense"), null, true); } else { $ignoredSourceModel = (new Clubs)->get(abs($ignoredSource)); if(!$ignoredSourceModel) $this->flashFail("err", "Error", tr("invalid_club"), null, true); - if($ignoredSourceModel->isHideFromGlobalFeedEnabled()) { + if($ignoredSourceModel->isHideFromGlobalFeedEnabled()) $this->flashFail("err", "Error", tr("no_sense"), null, true); - } } - - if($ignoredSourceModel->isIgnoredBy($this->user->identity)) { - DatabaseConnection::i()->getContext()->table("ignored_sources")->where([ - "owner" => $this->user->id, - "ignored_source" => $ignoredSource - ])->delete(); - + + if(!$ignoredSourceModel->toggleIgnore($this->user->identity)) { $tr = ""; if($ignoredSource > 0) @@ -705,11 +702,6 @@ final class WallPresenter extends OpenVKPresenter $this->returnJson(["success" => true, "act" => "unignored", "text" => $tr]); } else { - DatabaseConnection::i()->getContext()->table("ignored_sources")->insert([ - "owner" => $this->user->id, - "ignored_source" => $ignoredSource - ]); - if($ignoredSource > 0) $tr = tr("unignore_user"); else diff --git a/Web/Presenters/templates/Wall/Feed.xml b/Web/Presenters/templates/Wall/Feed.xml index e8f4e335..be15387a 100644 --- a/Web/Presenters/templates/Wall/Feed.xml +++ b/Web/Presenters/templates/Wall/Feed.xml @@ -16,7 +16,7 @@ {_all_news} - {_ignored_sources} + {_ignored_sources}