Fix aprosav

This commit is contained in:
lalka2016 2023-07-17 17:53:09 +03:00
parent aef77db697
commit 8495f493d0
4 changed files with 38 additions and 16 deletions

View file

@ -26,7 +26,12 @@ class Polls implements Handler
{ {
$poll = $this->polls->get($pollId); $poll = $this->polls->get($pollId);
if(!$poll) { if(!$poll) {
$reject("Poll not found"); $reject(1, "Poll not found");
return;
}
if(!$poll->canBeViewedBy($this->user)) {
$reject(12, "Access to poll denied");
return; return;
} }
@ -34,16 +39,16 @@ class Polls implements Handler
$options = explode(",", $options); $options = explode(",", $options);
$poll->vote($this->user, $options); $poll->vote($this->user, $options);
} catch(AlreadyVotedException $ex) { } catch(AlreadyVotedException $ex) {
$reject("Poll state changed: user has already voted."); $reject(10, "Poll state changed: user has already voted.");
return; return;
} catch(PollLockedException $ex) { } catch(PollLockedException $ex) {
$reject("Poll state changed: poll has ended."); $reject(25, "Poll state changed: poll has ended.");
return; return;
} catch(InvalidOptionException $ex) { } catch(InvalidOptionException $ex) {
$reject("Foreign options passed."); $reject(34, "Foreign options passed.");
return; return;
} catch(UnexpectedValueException $ex) { } catch(UnexpectedValueException $ex) {
$reject("Too much options passed."); $reject(42, "Too much options passed.");
return; return;
} }
@ -54,14 +59,19 @@ class Polls implements Handler
{ {
$poll = $this->polls->get($pollId); $poll = $this->polls->get($pollId);
if(!$poll) { if(!$poll) {
$reject("Poll not found"); $reject(28, "Poll not found");
return;
}
if(!$poll->canBeViewedBy($this->user)) {
$reject(12, "Access to poll denied");
return; return;
} }
try { try {
$poll->revokeVote($this->user); $poll->revokeVote($this->user);
} catch(PollLockedException $ex) { } catch(PollLockedException $ex) {
$reject("Votes can't be revoked from this poll."); $reject(19, "Votes can't be revoked from this poll.");
return; return;
} }

View file

@ -34,7 +34,8 @@ final class Polls extends VKAPIRequestHandler
$userVote = array(); $userVote = array();
foreach($poll->getUserVote($this->getUser()) as $vote) foreach($poll->getUserVote($this->getUser()) as $vote)
$userVote[] = $vote[0]; $userVote[] = $vote[0];
$ownerr = $poll->getAttachedPost()->getOwner() instanceof User ? $poll->getAttachedPost()->getOwner()->getId() : $poll->getAttachedPost()->getOwner()->getId() * -1;
$response = [ $response = [
"multiple" => $poll->isMultipleChoice(), "multiple" => $poll->isMultipleChoice(),
"end_date" => $poll->endsAt() == NULL ? 0 : $poll->endsAt()->timestamp(), "end_date" => $poll->endsAt() == NULL ? 0 : $poll->endsAt()->timestamp(),
@ -44,16 +45,16 @@ final class Polls extends VKAPIRequestHandler
"can_vote" => $poll->canVote($this->getUser()), "can_vote" => $poll->canVote($this->getUser()),
"can_report" => false, "can_report" => false,
"can_share" => true, "can_share" => true,
"created" => 0, "created" => $poll->getAttachedPost()->getPublicationTime()->timestamp(),
"id" => $poll->getId(), "id" => $poll->getId(),
"owner_id" => $poll->getOwner()->getId(), "owner_id" => $ownerr,
"question" => $poll->getTitle(), "question" => $poll->getTitle(),
"votes" => $poll->getVoterCount(), "votes" => $poll->getVoterCount(),
"disable_unvote" => $poll->isRevotable(), "disable_unvote" => $poll->isRevotable(),
"anonymous" => $poll->isAnonymous(), "anonymous" => $poll->isAnonymous(),
"answer_ids" => $userVote, "answer_ids" => $userVote,
"answers" => $answers, "answers" => $answers,
"author_id" => $poll->getOwner()->getId(), "author_id" => $ownerr,
]; ];
if ($extended) { if ($extended) {

View file

@ -879,6 +879,7 @@ final class Wall extends VKAPIRequestHandler
foreach($attachment->getUserVote($user) as $vote) foreach($attachment->getUserVote($user) as $vote)
$userVote[] = $vote[0]; $userVote[] = $vote[0];
$ownerr = $attachment->getAttachedPost()->getOwner() instanceof User ? $attachment->getAttachedPost()->getOwner()->getId() : $attachment->getAttachedPost()->getOwner()->getId() * -1;
return [ return [
"type" => "poll", "type" => "poll",
"poll" => [ "poll" => [
@ -890,16 +891,16 @@ final class Wall extends VKAPIRequestHandler
"can_vote" => $attachment->canVote($user), "can_vote" => $attachment->canVote($user),
"can_report" => false, "can_report" => false,
"can_share" => true, "can_share" => true,
"created" => 0, "created" => $attachment->getAttachedPost()->getPublicationTime()->timestamp(),
"id" => $attachment->getId(), "id" => $attachment->getId(),
"owner_id" => $attachment->getOwner()->getId(), "owner_id" => $ownerr,
"question" => $attachment->getTitle(), "question" => $attachment->getTitle(),
"votes" => $attachment->getVoterCount(), "votes" => $attachment->getVoterCount(),
"disable_unvote" => $attachment->isRevotable(), "disable_unvote" => $attachment->isRevotable(),
"anonymous" => $attachment->isAnonymous(), "anonymous" => $attachment->isAnonymous(),
"answer_ids" => $userVote, "answer_ids" => $userVote,
"answers" => $answers, "answers" => $answers,
"author_id" => $attachment->getOwner()->getId(), "author_id" => $ownerr,
] ]
]; ];
} }

View file

@ -4,7 +4,7 @@ use openvk\Web\Models\Exceptions\TooMuchOptionsException;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use \UnexpectedValueException; use \UnexpectedValueException;
use Nette\InvalidStateException; use Nette\InvalidStateException;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\{Users, Posts};
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Exceptions\PollLockedException; use openvk\Web\Models\Exceptions\PollLockedException;
use openvk\Web\Models\Exceptions\AlreadyVotedException; use openvk\Web\Models\Exceptions\AlreadyVotedException;
@ -293,8 +293,18 @@ class Poll extends Attachable
} }
} }
function getAttachedPost()
{
$post = DatabaseConnection::i()->getContext()->table("attachments")
->where(
["attachable_type" => static::class,
"attachable_id" => $this->getId()])->fetch();
return (new Posts)->get($post->target_id);
}
function canBeViewedBy(?User $user = NULL): bool function canBeViewedBy(?User $user = NULL): bool
{ {
return $this->getOwner()->canBeViewedBy($user); return $this->getAttachedPost()->canBeViewedBy($user);
} }
} }