philomena/lib/philomena_web/controllers/topic_controller.ex

117 lines
3.4 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
2019-11-19 04:38:22 +01:00
alias Philomena.Textile.Renderer
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
2019-10-06 23:31:48 +02:00
plug :load_and_authorize_resource, model: Forum, id_name: "forum_id", id_field: "short_name", persisted: true
2019-12-14 20:46:50 +01:00
plug PhilomenaWeb.LoadTopicPlug, [param: "id"] when action in [:show]
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"] || ""),
[post] <- Post |> where(id: ^post_id) |> Repo.all()
do
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()
2019-11-11 00:35:52 +01:00
rendered =
2019-12-01 18:11:00 +01:00
Renderer.render_collection(posts, conn)
2019-11-11 00:35:52 +01:00
posts =
Enum.zip(posts, rendered)
2019-10-09 01:40:26 +02: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
2019-11-17 19:05:50 +01:00
watching =
Topics.subscribed?(topic, conn.assigns.current_user)
2019-12-20 04:41:19 +01:00
voted =
PollVotes.voted?(topic.poll, conn.assigns.current_user)
2019-11-18 04:31:28 +01:00
changeset =
%Post{}
|> Posts.change_post()
2019-12-16 20:24:38 +01:00
title = "#{topic.title} - #{forum.name} - Forums"
2019-12-20 04:41:19 +01:00
render(conn, "show.html", title: title, posts: posts, changeset: changeset, 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)
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
2019-10-06 23:31:48 +02:00
end