Fix being able to vote on polls past their end date (#13)

This commit is contained in:
David Joseph Guzsik 2020-07-21 17:51:12 +02:00 committed by byte[]
parent 75e07bfb13
commit 9a8b5a7ea7
5 changed files with 24 additions and 3 deletions

View file

@ -7,6 +7,7 @@ defmodule Philomena.PollVotes do
alias Ecto.Multi
alias Philomena.Repo
alias Philomena.Polls
alias Philomena.Polls.Poll
alias Philomena.PollVotes.PollVote
alias Philomena.PollOptions.PollOption
@ -53,6 +54,13 @@ defmodule Philomena.PollVotes do
{:ok, poll}
end)
|> Multi.run(:ended, fn _repo, _changes ->
# Bail if poll is no longer active
case Polls.active?(poll) do
false -> {:error, []}
_true -> {:ok, []}
end
end)
|> Multi.run(:existing_votes, fn _repo, _changes ->
# Don't proceed if any votes exist
case voted?(poll, user) do

View file

@ -101,4 +101,12 @@ defmodule Philomena.Polls do
def change_poll(%Poll{} = poll) do
Poll.changeset(poll, %{})
end
def active?(%{id: poll_id}) do
now = DateTime.utc_now()
Poll
|> where([p], p.id == ^poll_id and p.active_until > ^now)
|> Repo.exists?()
end
end

View file

@ -5,7 +5,9 @@ defmodule Philomena.Polls.Poll do
alias Philomena.Topics.Topic
alias Philomena.Users.User
alias Philomena.PollOptions.PollOption
alias Philomena.Polls.Poll
alias Philomena.Schema.Time
import Ecto.Query
schema "polls" do
belongs_to :topic, Topic

View file

@ -3,7 +3,7 @@ defmodule PhilomenaWeb.TopicController do
alias PhilomenaWeb.NotificationCountPlug
alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post, Polls.Poll, PollOptions.PollOption}
alias Philomena.{Forums, Topics, Posts}
alias Philomena.{Forums, Topics, Polls, Posts}
alias Philomena.PollVotes
alias PhilomenaWeb.TextileRenderer
alias Philomena.Repo
@ -76,6 +76,8 @@ defmodule PhilomenaWeb.TopicController do
voted = PollVotes.voted?(topic.poll, conn.assigns.current_user)
poll_active = Polls.active?(topic.poll)
changeset =
%Post{}
|> Posts.change_post()
@ -90,7 +92,8 @@ defmodule PhilomenaWeb.TopicController do
changeset: changeset,
topic_changeset: topic_changeset,
watching: watching,
voted: voted
voted: voted,
poll_active: poll_active
)
end

View file

@ -16,7 +16,7 @@
strong
= @poll.deletion_reason || "Unknown (likely deleted in error). Please contact a moderator."
- not @voted and not is_nil(@conn.assigns.current_user) ->
- @poll_active and not @voted and not is_nil(@conn.assigns.current_user) ->
.poll
.poll-area
= render PhilomenaWeb.Topic.PollView, "_vote_form.html", assigns