From 9a8b5a7ea7611c0457c1787ab860ac8be2aaf8d4 Mon Sep 17 00:00:00 2001 From: David Joseph Guzsik Date: Tue, 21 Jul 2020 17:51:12 +0200 Subject: [PATCH] Fix being able to vote on polls past their end date (#13) --- lib/philomena/poll_votes.ex | 8 ++++++++ lib/philomena/polls.ex | 8 ++++++++ lib/philomena/polls/poll.ex | 2 ++ lib/philomena_web/controllers/topic_controller.ex | 7 +++++-- .../templates/topic/poll/_display.html.slime | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/philomena/poll_votes.ex b/lib/philomena/poll_votes.ex index 318b1fdd..d18979bc 100644 --- a/lib/philomena/poll_votes.ex +++ b/lib/philomena/poll_votes.ex @@ -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 diff --git a/lib/philomena/polls.ex b/lib/philomena/polls.ex index fa77ae8c..13a4f37e 100644 --- a/lib/philomena/polls.ex +++ b/lib/philomena/polls.ex @@ -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 diff --git a/lib/philomena/polls/poll.ex b/lib/philomena/polls/poll.ex index cb320431..7343d693 100644 --- a/lib/philomena/polls/poll.ex +++ b/lib/philomena/polls/poll.ex @@ -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 diff --git a/lib/philomena_web/controllers/topic_controller.ex b/lib/philomena_web/controllers/topic_controller.ex index 04f1be4f..ea2dc4b8 100644 --- a/lib/philomena_web/controllers/topic_controller.ex +++ b/lib/philomena_web/controllers/topic_controller.ex @@ -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 diff --git a/lib/philomena_web/templates/topic/poll/_display.html.slime b/lib/philomena_web/templates/topic/poll/_display.html.slime index 08684008..eb253fbc 100644 --- a/lib/philomena_web/templates/topic/poll/_display.html.slime +++ b/lib/philomena_web/templates/topic/poll/_display.html.slime @@ -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