philomena/lib/philomena_web/controllers/topic_controller.ex

157 lines
4.6 KiB
Elixir
Raw Normal View History

2019-10-06 23:31:48 +02:00
defmodule PhilomenaWeb.TopicController do
use PhilomenaWeb, :controller
2019-12-02 16:02:48 +01:00
alias PhilomenaWeb.NotificationCountPlug
2019-11-19 04:38:22 +01:00
alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post, Polls.Poll, PollOptions.PollOption}
2019-12-01 06:03:45 +01:00
alias Philomena.{Forums, Topics, Posts}
2019-12-20 04:41:19 +01:00
alias Philomena.PollVotes
2020-05-08 04:19:08 +02:00
alias PhilomenaWeb.TextileRenderer
2019-10-06 23:31:48 +02:00
alias Philomena.Repo
import Ecto.Query
2019-11-19 04:38:22 +01:00
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
plug PhilomenaWeb.UserAttributionPlug when action in [:new, :create]
2019-11-29 07:26:05 +01:00
plug PhilomenaWeb.AdvertPlug when action in [:show]
plug PhilomenaWeb.CanaryMapPlug, new: :show, create: :show, update: :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-10-06 23:31:48 +02:00
plug PhilomenaWeb.LoadTopicPlug, [param: "id"] when action in [:show, :update]
plug :verify_authorized when action in [:update]
2019-12-14 20:46:50 +01:00
def show(conn, params) do
2019-10-06 23:59:47 +02:00
forum = conn.assigns.forum
2019-12-14 20:46:50 +01:00
topic = conn.assigns.topic
2019-10-06 23:59:47 +02:00
2019-12-01 06:03:45 +01:00
user = conn.assigns.current_user
Topics.clear_notification(topic, user)
Forums.clear_notification(forum, user)
2019-12-02 15:55:48 +01:00
# Update the notification ticker in the header
conn = NotificationCountPlug.call(conn)
2019-10-06 23:59:47 +02:00
conn = conn |> assign(:topic, topic)
2019-10-09 01:40:26 +02:00
%{page_number: page} = conn.assigns.pagination
2019-10-06 23:59:47 +02:00
2019-11-11 18:57:02 +01:00
page =
with {post_id, _extra} <- Integer.parse(params["post_id"] || ""),
2020-01-11 05:20:19 +01:00
[post] <- Post |> where(id: ^post_id) |> Repo.all() do
2019-11-11 18:57:02 +01:00
div(post.topic_position, 25) + 1
else
_ ->
page
end
2019-10-06 23:31:48 +02:00
posts =
Post
|> where(topic_id: ^conn.assigns.topic.id)
2019-10-09 01:37:14 +02:00
|> where([p], p.topic_position >= ^(25 * (page - 1)) and p.topic_position < ^(25 * page))
2019-10-07 00:06:22 +02:00
|> order_by(asc: :created_at)
|> preload([:deleted_by, :topic, topic: :forum, user: [awards: :badge]])
2019-10-09 01:40:26 +02:00
|> Repo.all()
2020-05-08 04:19:08 +02:00
rendered = TextileRenderer.render_collection(posts, conn)
2019-11-11 00:35:52 +01:00
2020-01-11 05:20:19 +01:00
posts = Enum.zip(posts, rendered)
2019-11-11 00:35:52 +01:00
2020-01-11 05:20:19 +01:00
posts = %Scrivener.Page{
entries: posts,
page_number: page,
page_size: 25,
total_entries: topic.post_count,
total_pages: div(topic.post_count + 25 - 1, 25)
}
2019-10-06 23:31:48 +02:00
2020-01-11 05:20:19 +01:00
watching = Topics.subscribed?(topic, conn.assigns.current_user)
2019-11-17 19:05:50 +01:00
2020-01-11 05:20:19 +01:00
voted = PollVotes.voted?(topic.poll, conn.assigns.current_user)
2019-12-20 04:41:19 +01:00
2019-11-18 04:31:28 +01:00
changeset =
%Post{}
|> Posts.change_post()
topic_changeset = Topics.change_topic(conn.assigns.topic)
2019-12-16 20:24:38 +01:00
title = "#{topic.title} - #{forum.name} - Forums"
2020-01-11 05:20:19 +01:00
render(conn, "show.html",
title: title,
posts: posts,
changeset: changeset,
topic_changeset: topic_changeset,
2020-01-11 05:20:19 +01:00
watching: watching,
voted: voted
)
2019-10-06 23:31:48 +02:00
end
2019-11-19 04:38:22 +01:00
def new(conn, _params) do
changeset =
%Topic{poll: %Poll{options: [%PollOption{}, %PollOption{}]}, posts: [%Post{}]}
|> Topics.change_topic()
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Topic", changeset: changeset)
2019-11-19 04:38:22 +01:00
end
def create(conn, %{"topic" => topic_params}) do
attributes = conn.assigns.attributes
forum = conn.assigns.forum
case Topics.create_topic(forum, attributes, topic_params) do
{:ok, %{topic: topic}} ->
post = hd(topic.posts)
Posts.reindex_post(post)
Topics.notify_topic(topic)
if forum.access_level == "normal" do
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"post:create",
PhilomenaWeb.Api.Json.Forum.Topic.PostView.render("firehose.json", %{post: post, topic: topic, forum: forum})
)
end
2019-11-19 04:38:22 +01:00
conn
|> put_flash(:info, "Successfully posted topic.")
|> redirect(to: Routes.forum_topic_path(conn, :show, forum, topic))
{:error, :topic, changeset, _} ->
conn
|> render("new.html", changeset: changeset)
_error ->
conn
|> put_flash(:error, "There was an error with your submission. Please try again.")
|> redirect(to: Routes.forum_topic_path(conn, :new, forum))
end
end
def update(conn, %{"topic" => topic_params}) do
case Topics.update_topic_title(conn.assigns.topic, topic_params) do
{:ok, topic} ->
conn
|> put_flash(:info, "Successfully updated topic.")
|> redirect(to: Routes.forum_topic_path(conn, :show, conn.assigns.forum, topic))
{:error, _changeset} ->
conn
|> put_flash(:error, "There was an error with your submission. Please try again.")
2020-05-20 20:23:57 +02:00
|> redirect(
to: Routes.forum_topic_path(conn, :show, conn.assigns.forum, conn.assigns.topic)
)
end
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :edit, conn.assigns.topic) do
true -> conn
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2019-10-06 23:31:48 +02:00
end