philomena/lib/philomena_web/controllers/comment_controller.ex

43 lines
1.1 KiB
Elixir
Raw Normal View History

2019-11-11 22:10:41 -05:00
defmodule PhilomenaWeb.CommentController do
use PhilomenaWeb, :controller
2019-11-30 20:06:08 -05:00
alias Philomena.{Comments.Query, Comments.Comment, Textile.Renderer}
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-11-30 20:06:08 -05:00
{:ok, query} = Query.compile(conn.assigns.current_user, cq)
2019-11-11 22:10:41 -05:00
comments =
Comment.search_records(
%{
query: %{
bool: %{
2019-11-30 20:06:08 -05:00
must: query,
must_not: [
%{terms: %{image_tag_ids: conn.assigns.current_filter.hidden_tag_ids}},
%{term: %{hidden_from_users: true}}
]
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,
2019-11-17 15:37:01 -05:00
Comment |> preload([image: [:tags], user: [awards: :badge]])
2019-11-11 22:10:41 -05:00
)
rendered =
comments.entries
2019-12-01 12:11:00 -05:00
|> Renderer.render_collection(conn)
2019-11-11 22:10:41 -05:00
comments =
2019-11-30 20:06:08 -05:00
%{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
2019-11-11 22:10:41 -05:00
end