2019-10-06 23:31:48 +02:00
|
|
|
defmodule PhilomenaWeb.ForumController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-11-29 20:29:01 +01:00
|
|
|
alias Philomena.{Forums, Forums.Forum, Topics.Topic}
|
2019-10-06 23:31:48 +02:00
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
plug :load_and_authorize_resource, model: Forum, id_field: "short_name"
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-10-06 23:31:48 +02:00
|
|
|
forums =
|
|
|
|
Forum
|
2019-11-17 23:55:20 +01:00
|
|
|
|> order_by(asc: :name)
|
2020-01-11 05:20:19 +01:00
|
|
|
|> preload(last_post: [:user, topic: :forum])
|
2019-10-06 23:31:48 +02:00
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.filter(&Canada.Can.can?(user, :show, &1))
|
|
|
|
|
|
|
|
topic_count = Repo.aggregate(Forum, :sum, :topic_count)
|
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "Forums", forums: forums, topic_count: topic_count)
|
2019-10-06 23:31:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => _id}) do
|
2019-11-29 20:29:01 +01:00
|
|
|
forum = conn.assigns.forum
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2019-10-06 23:31:48 +02:00
|
|
|
topics =
|
|
|
|
Topic
|
2019-11-29 20:29:01 +01:00
|
|
|
|> where(forum_id: ^forum.id)
|
2019-10-06 23:31:48 +02:00
|
|
|
|> where(hidden_from_users: false)
|
|
|
|
|> order_by(desc: :sticky, desc: :last_replied_to_at)
|
|
|
|
|> preload([:poll, :forum, :user, last_post: :user])
|
2019-10-09 01:42:22 +02:00
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
2019-10-06 23:31:48 +02:00
|
|
|
|
2019-11-29 20:29:01 +01:00
|
|
|
watching = Forums.subscribed?(forum, user)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "show.html",
|
|
|
|
title: forum.name,
|
|
|
|
forum: conn.assigns.forum,
|
|
|
|
watching: watching,
|
|
|
|
topics: topics
|
|
|
|
)
|
2019-10-06 23:31:48 +02:00
|
|
|
end
|
|
|
|
end
|