<commit_before><?php

final class PhabricatorSlowvoteVoteController
  extends PhabricatorSlowvoteController {

  public function handleRequest(AphrontRequest $request) {
    $viewer = $request->getViewer();
    $id = $request->getURIData('id');

    if (!$request->isFormPost()) {
      return id(new Aphront404Response());
    }

    $poll = id(new PhabricatorSlowvoteQuery())
      ->setViewer($viewer)
      ->withIDs(array($id))
      ->needOptions(true)
      ->needViewerChoices(true)
      ->executeOne();
    if (!$poll) {
      return new Aphront404Response();
    }

    if ($poll->getIsClosed()) {
      return new Aphront400Response();
    }

    $options = $poll->getOptions();
    $options = mpull($options, null, 'getID');

    $old_votes = $poll->getViewerChoices($viewer);
    $old_votes = mpull($old_votes, null, 'getOptionID');

    $votes = $request->getArr('vote');
    $votes = array_fuse($votes);

    $method = $poll->getMethod();
    $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY);

    if ($is_plurality && count($votes) > 1) {
      throw new Exception(
        pht('In this poll, you may only vote for one option.'));
    }

    foreach ($votes as $vote) {
      if (!isset($options[$vote])) {
        throw new Exception(
          pht(
            'Option ("%s") is not a valid poll option. You may only '.
            'vote for valid options.',
            $vote));
      }
    }

    foreach ($old_votes as $old_vote) {
      if (!idx($votes, $old_vote->getOptionID(), false)) {
        $old_vote->delete();
      }
    }

    foreach ($votes as $vote) {
      if (idx($old_votes, $vote, false)) {
        continue;
      }

      id(new PhabricatorSlowvoteChoice())
        ->setAuthorPHID($viewer->getPHID())
        ->setPollID($poll->getID())
        ->setOptionID($vote)
        ->save();
    }

    return id(new AphrontRedirectResponse())
      ->setURI($poll->getURI());
  }

}
<commit_msg>Fix two very, very minor correctness issues in Slowvote<commit_after><?php

final class PhabricatorSlowvoteVoteController
  extends PhabricatorSlowvoteController {

  public function handleRequest(AphrontRequest $request) {
    $viewer = $request->getViewer();
    $id = $request->getURIData('id');

    if (!$request->isFormPost()) {
      return id(new Aphront404Response());
    }

    $poll = id(new PhabricatorSlowvoteQuery())
      ->setViewer($viewer)
      ->withIDs(array($id))
      ->needOptions(true)
      ->needViewerChoices(true)
      ->executeOne();
    if (!$poll) {
      return new Aphront404Response();
    }

    if ($poll->getIsClosed()) {
      return new Aphront400Response();
    }

    $options = $poll->getOptions();
    $options = mpull($options, null, 'getID');

    $old_votes = $poll->getViewerChoices($viewer);
    $old_votes = mpull($old_votes, null, 'getOptionID');

    $votes = $request->getArr('vote');
    $votes = array_fuse($votes);

    $method = $poll->getMethod();
    $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY);

    if (!$votes) {
      if ($is_plurality) {
        $message = pht('You must vote for something.');
      } else {
        $message = pht('You must vote for at least one option.');
      }

      return $this->newDialog()
        ->setTitle(pht('Stand For Something'))
        ->appendParagraph($message)
        ->addCancelButton($poll->getURI());
    }

    if ($is_plurality && count($votes) > 1) {
      throw new Exception(
        pht('In this poll, you may only vote for one option.'));
    }

    foreach ($votes as $vote) {
      if (!isset($options[$vote])) {
        throw new Exception(
          pht(
            'Option ("%s") is not a valid poll option. You may only '.
            'vote for valid options.',
            $vote));
      }
    }

    $poll->openTransaction();
      $poll->beginReadLocking();

      $poll->reload();

      $old_votes = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
        'pollID = %d AND authorPHID = %s',
        $poll->getID(),
        $viewer->getPHID());
      $old_votes = mpull($old_votes, null, 'getOptionID');

      foreach ($old_votes as $old_vote) {
        if (idx($votes, $old_vote->getOptionID())) {
          continue;
        }

        $old_vote->delete();
      }

      foreach ($votes as $vote) {
        if (idx($old_votes, $vote)) {
          continue;
        }

        id(new PhabricatorSlowvoteChoice())
          ->setAuthorPHID($viewer->getPHID())
          ->setPollID($poll->getID())
          ->setOptionID($vote)
          ->save();
      }

      $poll->endReadLocking();
    $poll->saveTransaction();

    return id(new AphrontRedirectResponse())
      ->setURI($poll->getURI());
  }

}

{'fraction_non_alphanumeric': 0.1596015260703688, 'fraction_numerical': 0.00423908435777872, 'mean_word_length': 2.5588235294117645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535545', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1517, 'n_words': 311}