philomena/lib/philomena_web/controllers/comment_controller.ex

58 lines
1.6 KiB
Elixir
Raw Normal View History

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