2019-12-20 04:41:19 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.Poll.VoteController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Forums.Forum
|
|
|
|
alias Philomena.PollOptions.PollOption
|
|
|
|
alias Philomena.PollVotes
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-21 16:22:13 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:create]
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, index: :show, create: :show, delete: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Forum,
|
|
|
|
id_name: "forum_id",
|
|
|
|
id_field: "short_name",
|
|
|
|
persisted: true
|
2019-12-21 16:22:13 +01:00
|
|
|
|
2019-12-20 04:41:19 +01:00
|
|
|
plug PhilomenaWeb.LoadTopicPlug
|
|
|
|
plug PhilomenaWeb.LoadPollPlug
|
|
|
|
|
2019-12-30 14:32:41 +01:00
|
|
|
plug :verify_authorized when action in [:index, :delete]
|
|
|
|
|
2019-12-20 04:41:19 +01:00
|
|
|
def index(conn, _params) do
|
|
|
|
poll = conn.assigns.poll
|
|
|
|
|
|
|
|
options =
|
|
|
|
PollOption
|
|
|
|
|> where(poll_id: ^poll.id)
|
|
|
|
|> preload(poll_votes: :user)
|
|
|
|
|> Repo.all()
|
2020-01-11 05:20:19 +01:00
|
|
|
|> Enum.filter(&(&1.vote_count > 0))
|
2019-12-20 04:41:19 +01:00
|
|
|
|
|
|
|
render(conn, "index.html", layout: false, options: options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"poll" => poll_params}) do
|
|
|
|
poll = conn.assigns.poll
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
|
|
|
|
case PollVotes.create_poll_votes(conn.assigns.current_user, poll, poll_params) do
|
|
|
|
{:ok, _votes} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Your vote has been recorded.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-20 04:41:19 +01:00
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
2023-11-23 17:07:49 +01:00
|
|
|
|> put_flash(:warning, "Your vote was not recorded.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-20 04:41:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-10 01:30:18 +02:00
|
|
|
def create(conn, _params) do
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Your vote was not recorded.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2023-04-10 01:30:18 +02:00
|
|
|
end
|
|
|
|
|
2019-12-20 04:41:19 +01:00
|
|
|
def delete(conn, %{"id" => poll_vote_id}) do
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
poll_vote = PollVotes.get_poll_vote!(poll_vote_id)
|
|
|
|
|
|
|
|
{:ok, _poll_vote} = PollVotes.delete_poll_vote(poll_vote)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Vote successfully removed.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-20 04:41:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
2019-12-30 14:32:41 +01:00
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :hide, conn.assigns.topic) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-20 04:41:19 +01:00
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|