philomena/lib/philomena_web/controllers/comment_controller.ex

47 lines
1.2 KiB
Elixir
Raw Normal View History

2019-11-12 04:10:41 +01:00
defmodule PhilomenaWeb.CommentController do
use PhilomenaWeb, :controller
2019-12-01 02:06:08 +01:00
alias Philomena.{Comments.Query, Comments.Comment, Textile.Renderer}
2019-11-12 04:10:41 +01:00
import Ecto.Query
2019-11-17 22:30:20 +01:00
def index(conn, params) do
2019-12-01 02:30:05 +01:00
cq = params["cq"] || "created_at.gte:1 week ago"
2019-12-01 02:06:08 +01:00
2019-12-02 15:37:01 +01:00
params = Map.put(conn.params, "cq", cq)
conn = Map.put(conn, :params, params)
2019-12-21 16:14:01 +01:00
user = conn.assigns.current_user
2019-12-02 15:37:01 +01:00
2019-12-21 16:14:01 +01:00
{:ok, query} = Query.compile(user, cq)
2019-12-01 02:06:08 +01:00
2019-11-12 04:10:41 +01:00
comments =
Comment.search_records(
%{
query: %{
bool: %{
2019-12-21 16:14:01 +01:00
must: [query | filters(user)],
must_not: %{
terms: %{image_tag_ids: conn.assigns.current_filter.hidden_tag_ids}
}
2019-11-12 04:10:41 +01:00
}
},
2019-12-01 02:06:08 +01:00
sort: %{posted_at: :desc}
2019-11-12 04:10:41 +01:00
},
conn.assigns.pagination,
2019-12-21 16:14:01 +01:00
Comment |> preload([:deleted_by, image: [:tags], user: [awards: :badge]])
2019-11-12 04:10:41 +01:00
)
rendered =
comments.entries
2019-12-01 18:11:00 +01:00
|> Renderer.render_collection(conn)
2019-11-12 04:10:41 +01:00
comments =
2019-12-01 02:06:08 +01:00
%{comments | entries: Enum.zip(rendered, comments.entries)}
2019-11-17 23:14:20 +01:00
2019-12-16 20:24:38 +01:00
render(conn, "index.html", title: "Comments", comments: comments)
2019-11-17 23:14:20 +01:00
end
2019-12-21 16:14:01 +01:00
defp filters(%{role: role}) when role in ["moderator", "admin"], do: []
defp filters(_user),
do: [%{term: %{hidden_from_users: false}}]
2019-11-12 04:10:41 +01:00
end