Skip to content

Commit

Permalink
Merge branch 'master' into n1rwana-geo
Browse files Browse the repository at this point in the history
  • Loading branch information
veselcraft authored Dec 13, 2024
2 parents bc78cc1 + bec9079 commit 8db4bc7
Show file tree
Hide file tree
Showing 26 changed files with 512 additions and 24 deletions.
68 changes: 68 additions & 0 deletions VKAPI/Handlers/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,72 @@ function sendVotes(int $receiver, int $value, string $message = ""): object

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;
}
}
53 changes: 53 additions & 0 deletions VKAPI/Handlers/Reports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\Report;
use openvk\Web\Models\Repositories\Reports as ReportsRepo;

final class Reports extends VKAPIRequestHandler
{
function add(int $owner_id = 0, string $comment = "", int $reason = 0, string $type = "", string $report_source = ""): int
{
$this->requireUser();
$this->willExecuteWriteAction();

$allowed_types = ["post", "photo", "video", "group", "comment", "note", "app", "user", "audio"];
if($type == "" || !in_array($type, $allowed_types)) {
$this->fail(100, "One of the parameters specified was missing or invalid: type should be ".implode(", ", $allowed_types));
}

if($owner_id <= 0) {
$this->fail(100, "One of the parameters specified was missing or invalid: Bad input");
}

if(mb_strlen($comment) === 0) {
$this->fail(100, "One of the parameters specified was missing or invalid: Comment can't be empty");
}

if($type == "user" && $owner_id == $this->getUser()->getId()) {
return 1;
}

if($this->getUser()->isBannedInSupport()) {
return 0;
}

if(sizeof(iterator_to_array((new ReportsRepo)->getDuplicates($type, $owner_id, NULL, $this->getUser()->getId()))) > 0) {
return 1;
}

try {
$report = new Report;
$report->setUser_id($this->getUser()->getId());
$report->setTarget_id($owner_id);
$report->setType($type);
$report->setReason($comment);
$report->setCreated(time());

$report->save();
} catch(\Throwable $e) {
$this->fail(-1, "Unknown error failed");
}

return 1;
}
}
14 changes: 14 additions & 0 deletions VKAPI/Handlers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $
case 'nickname':
$response[$i]->nickname = $usr->getPseudo();
break;
case 'blacklisted_by_me':
if(!$authuser) {
continue;
}

$response[$i]->blacklisted_by_me = (int)$usr->isBlacklistedBy($this->getUser());
break;
case 'blacklisted':
if(!$authuser) {
continue;
}

$response[$i]->blacklisted = (int)$this->getUser()->isBlacklistedBy($usr);
break;
}
}

Expand Down
103 changes: 98 additions & 5 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Photo, Message, Correspondence, Gift, Audio};
use openvk\Web\Models\Repositories\{Applications, Bans, Comments, Notes, Posts, Users, Clubs, Albums, Gifts, Notifications, Videos, Photos};
use openvk\Web\Models\Repositories\{Applications, Bans, Comments, Notes, Posts, Users, Clubs, Albums, Gifts, Notifications, Videos, Photos};
use openvk\Web\Models\Exceptions\InvalidUserNameException;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
Expand Down Expand Up @@ -511,7 +511,7 @@ function getPrivacyPermission(string $permission, ?User $user = NULL): bool
else if($user->getId() === $this->getId())
return true;

if($permission != "messages.write" && !$this->canBeViewedBy($user))
if(/*$permission != "messages.write" && */!$this->canBeViewedBy($user, true))
return false;

switch($permStatus) {
Expand Down Expand Up @@ -1228,11 +1228,16 @@ function isActivated(): bool
return (bool) $this->getRecord()->activated;
}

function isAdmin(): bool
{
return $this->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL);
}

function isDead(): bool
{
return $this->onlineStatus() == 2;
}

function getUnbanTime(): ?string
{
$ban = (new Bans)->get((int) $this->getRecord()->block_reason);
Expand Down Expand Up @@ -1289,17 +1294,21 @@ function getProfileType(): int
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($this->getId() == $user->getId()) {
return true;
}

if($user->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL)) {
if($user->isAdmin() && !(OPENVK_ROOT_CONF['openvk']['preferences']['blacklists']['applyToAdmins'] ?? true)) {
return true;
}

if($blacklist_check && ($this->isBlacklistedBy($user) || $user->isBlacklistedBy($this))) {
return false;
}

if($this->getProfileType() == 0) {
return true;
} else {
Expand Down Expand Up @@ -1409,6 +1418,20 @@ function toVkApiStruct(?User $user = NULL, string $fields = ''): object
case 'real_id':
$res->real_id = $this->getRealId();
break;
case "blacklisted_by_me":
if(!$user) {
continue;
}

$res->blacklisted_by_me = (int)$this->isBlacklistedBy($user);
break;
case "blacklisted":
if(!$user) {
continue;
}

$res->blacklisted = (int)$user->isBlacklistedBy($this);
break;
}
}

Expand Down Expand Up @@ -1486,6 +1509,76 @@ function getIgnoredSourcesCount()
return DatabaseConnection::i()->getContext()->table("ignored_sources")->where("owner", $this->getId())->count();
}

function isBlacklistedBy(?User $user = NULL): bool
{
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(),
]);

DatabaseConnection::i()->getContext()->table("subscriptions")->where([
"follower" => $user->getId(),
"model" => static::class,
"target" => $this->getId(),
])->delete();

DatabaseConnection::i()->getContext()->table("subscriptions")->where([
"follower" => $this->getId(),
"model" => static::class,
"target" => $user->getId(),
])->delete();

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\TSubscribable;
use Traits\TAudioStatuses;
Expand Down
2 changes: 1 addition & 1 deletion Web/Models/shell/processVideo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Move-Item $file $temp

# video stub logic was implicitly deprecated, so we start processing at once
ffmpeg -i $temp -ss 00:00:01.000 -vframes 1 "$dir$hashT/$hash.gif"
ffmpeg -i $temp -c:v libx264 -q:v 7 -c:a libmp3lame -q:a 4 -tune zerolatency -vf "scale=480:-1,setsar=1" -y $temp2
ffmpeg -i $temp -c:v libx264 -q:v 7 -c:a libmp3lame -q:a 4 -tune zerolatency -vf "scale=iw*min(1\,if(gt(iw\,ih)\,640/iw\,(640*sar)/ih)):(floor((ow/dar)/2))*2" -y $temp2

Move-Item $temp2 "$dir$hashT/$hash.mp4"
Remove-Item $temp
Expand Down
2 changes: 1 addition & 1 deletion Web/Models/shell/processVideo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tmpfile="$RANDOM-$(date +%s%N)"
cp $2 "/tmp/vid_$tmpfile.bin"

nice ffmpeg -i "/tmp/vid_$tmpfile.bin" -ss 00:00:01.000 -vframes 1 $3${4:0:2}/$4.gif
nice -n 20 ffmpeg -i "/tmp/vid_$tmpfile.bin" -c:v libx264 -q:v 7 -c:a libmp3lame -q:a 4 -tune zerolatency -vf "scale=480:-1,setsar=1" -y "/tmp/ffmOi$tmpfile.mp4"
nice -n 20 ffmpeg -i "/tmp/vid_$tmpfile.bin" -c:v libx264 -q:v 7 -c:a libmp3lame -q:a 4 -tune zerolatency -vf "scale=iw*min(1\,if(gt(iw\,ih)\,640/iw\,(640*sar)/ih)):(floor((ow/dar)/2))*2" -y "/tmp/ffmOi$tmpfile.mp4"

rm -rf $3${4:0:2}/$4.mp4
mv "/tmp/ffmOi$tmpfile.mp4" $3${4:0:2}/$4.mp4
Expand Down
2 changes: 1 addition & 1 deletion Web/Presenters/GroupPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function renderFollowers(int $id): void
$this->template->paginatorConf = (object) [
"count" => $this->template->count,
"page" => $this->queryParam("p") ?? 1,
"amount" => NULL,
"amount" => 10,
"perPage" => OPENVK_DEFAULT_PER_PAGE,
];
}
Expand Down
3 changes: 3 additions & 0 deletions Web/Presenters/PhotosPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function renderAlbumList(int $owner): void
if(!$user) $this->notFound();
if (!$user->getPrivacyPermission('photos.read', $this->user->identity ?? NULL))
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));

$this->template->albums = $this->albums->getUserAlbums($user, (int)($this->queryParam("p") ?? 1));
$this->template->count = $this->albums->getUserAlbumsCount($user);
$this->template->owner = $user;
Expand Down Expand Up @@ -161,8 +162,10 @@ function renderPhoto(int $ownerId, int $photoId): void
{
$photo = $this->photos->getByOwnerAndVID($ownerId, $photoId);
if(!$photo || $photo->isDeleted()) $this->notFound();

if(!$photo->canBeViewedBy($this->user->identity))
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));

if(!is_null($this->queryParam("from"))) {
if(preg_match("%^album([0-9]++)$%", $this->queryParam("from"), $matches) === 1) {
$album = $this->albums->get((int) $matches[1]);
Expand Down
3 changes: 3 additions & 0 deletions Web/Presenters/ReportPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function renderCreate(int $id): void

if(!$id)
exit(json_encode([ "error" => tr("error_segmentation") ]));

if ($this->queryParam("type") === "user" && $id === $this->user->id)
exit(json_encode([ "error" => "You can't report yourself" ]));

if(in_array($this->queryParam("type"), ["post", "photo", "video", "group", "comment", "note", "app", "user", "audio"])) {
if (count(iterator_to_array($this->reports->getDuplicates($this->queryParam("type"), $id, NULL, $this->user->id))) <= 0) {
Expand Down
Loading

0 comments on commit 8db4bc7

Please sign in to comment.