account.ban, account.unban, account.getBanned

This commit is contained in:
mrilyew 2024-12-11 18:07:02 +03:00
parent 7f86096b65
commit 4ac2958e90
4 changed files with 132 additions and 8 deletions

View file

@ -228,4 +228,72 @@ final class Account extends VKAPIRequestHandler
return (object) ['votes' => $this->getUser()->getCoins()]; return (object) ['votes' => $this->getUser()->getCoins()];
} }
function ban(int $owner_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
if($owner_id < 0)
return 1;
if($owner_id == $this->getUser()->getId())
$this->fail(15, "Access denied: cannot blacklist yourself");
$config_limit = OPENVK_ROOT_CONF['openvk']['preferences']['blacklists']['limit'] ?? 100;
$user_blocks = $this->getUser()->getBlacklistSize();
if(($user_blocks + 1) > $config_limit)
$this->fail(-7856, "Blacklist limit exceeded");
$entity = get_entity_by_id($owner_id);
if(!$entity || $entity->isDeleted())
return 0;
if($entity->isBlacklistedBy($this->getUser()))
return 1;
$this->getUser()->addToBlacklist($entity);
return 1;
}
function unban(int $owner_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
if($owner_id < 0)
return 1;
if($owner_id == $this->getUser()->getId())
return 1;
$entity = get_entity_by_id($owner_id);
if(!$entity || $entity->isDeleted())
return 0;
if(!$entity->isBlacklistedBy($this->getUser()))
return 1;
$this->getUser()->removeFromBlacklist($entity);
return 1;
}
function getBanned(int $offset = 0, int $count = 100, string $fields = ""): object
{
$this->requireUser();
$result = (object)[
'count' => $this->getUser()->getBlacklistSize(),
'items' => [],
];
$banned = $this->getUser()->getBlacklist($offset, $count);
foreach($banned as $ban) {
if(!$ban) continue;
$result->items[] = $ban->toVkApiStruct($this->getUser(), $fields);
}
return $result;
}
} }

View file

@ -510,9 +510,6 @@ class User extends RowModel
return $permStatus === User::PRIVACY_EVERYONE; return $permStatus === User::PRIVACY_EVERYONE;
else if($user->getId() === $this->getId()) else if($user->getId() === $this->getId())
return true; return true;
else if ($this->isBlacklistedBy($user)) {
return $user->isAdmin() && !OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["blacklists"]["applyToAdmins"];
}
if($permission != "messages.write" && !$this->canBeViewedBy($user)) if($permission != "messages.write" && !$this->canBeViewedBy($user))
return false; return false;
@ -1297,7 +1294,7 @@ class User extends RowModel
return $this->getRecord()->profile_type; return $this->getRecord()->profile_type;
} }
function canBeViewedBy(?User $user = NULL): bool function canBeViewedBy(?User $user = NULL, bool $blacklist_check = true): bool
{ {
if(!is_null($user)) { if(!is_null($user)) {
if($this->getId() == $user->getId()) { if($this->getId() == $user->getId()) {
@ -1308,6 +1305,10 @@ class User extends RowModel
return true; return true;
} }
if($blacklist_check && ($this->isBlacklistedBy($user) || $user->isBlacklistedBy($this))) {
return false;
}
if($this->getProfileType() == 0) { if($this->getProfileType() == 0) {
return true; return true;
} else { } else {
@ -1494,9 +1495,62 @@ class User extends RowModel
return DatabaseConnection::i()->getContext()->table("ignored_sources")->where("owner", $this->getId())->count(); return DatabaseConnection::i()->getContext()->table("ignored_sources")->where("owner", $this->getId())->count();
} }
function isBlacklistedBy($user): bool function isBlacklistedBy(?User $user = NULL): bool
{ {
return false; if(!$user)
return false;
$ctx = DatabaseConnection::i()->getContext();
$data = [
"author" => $user->getId(),
"target" => $this->getRealId(),
];
$sub = $ctx->table("blacklist_relations")->where($data);
return $sub->count() > 0;
}
function addToBlacklist(?User $user)
{
DatabaseConnection::i()->getContext()->table("blacklist_relations")->insert([
"author" => $this->getRealId(),
"target" => $user->getRealId(),
"created" => time(),
]);
return true;
}
function removeFromBlacklist(?User $user): bool
{
DatabaseConnection::i()->getContext()->table("blacklist_relations")->where([
"author" => $this->getRealId(),
"target" => $user->getRealId(),
])->delete();
return true;
}
function getBlacklist(int $offset = 0, int $limit = 10)
{
$sources = DatabaseConnection::i()->getContext()->table("blacklist_relations")->where("author", $this->getId())->limit($limit, $offset)->order('created ASC');
$output_array = [];
foreach($sources as $source) {
$entity_id = (int)$source->target ;
$entity = (new Users)->get($entity_id);
if(!$entity)
continue;
$output_array[] = $entity;
}
return $output_array;
}
function getBlacklistSize()
{
return DatabaseConnection::i()->getContext()->table("blacklist_relations")->where("author", $this->getId())->count();
} }
use Traits\TBackDrops; use Traits\TBackDrops;

View file

@ -5,3 +5,4 @@ CREATE TABLE `blacklist_relations` (
`created` BIGINT UNSIGNED NOT NULL, `created` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`index`) PRIMARY KEY (`index`)
) ENGINE = InnoDB; ) ENGINE = InnoDB;
ALTER TABLE `blacklist_relations` ADD INDEX(`author`, `target`);

View file

@ -38,8 +38,9 @@ openvk:
maxViolations: 50 maxViolations: 50
maxViolationsAge: 120 maxViolationsAge: 120
autoban: true autoban: true
blacklists: blacklists:
applyToAdmins: true limit: 100
applyToAdmins: true
registration: registration:
enable: true enable: true
disablingReason: "" disablingReason: ""