2019-11-12 04:10:41 +01:00
|
|
|
defmodule PhilomenaWeb.CommentController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2020-05-08 04:19:08 +02:00
|
|
|
alias PhilomenaWeb.TextileRenderer
|
2019-12-24 22:14:42 +01:00
|
|
|
alias Philomena.Elasticsearch
|
2020-05-08 04:19:08 +02:00
|
|
|
alias Philomena.{Comments.Query, Comments.Comment}
|
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-22 22:25:39 +01:00
|
|
|
user
|
|
|
|
|> Query.compile(cq)
|
|
|
|
|> render_index(conn, user)
|
|
|
|
end
|
2019-12-01 02:06:08 +01:00
|
|
|
|
2019-12-22 22:25:39 +01:00
|
|
|
defp render_index({:ok, query}, conn, user) do
|
2019-11-12 04:10:41 +01:00
|
|
|
comments =
|
2020-08-23 21:47:42 +02:00
|
|
|
Comment
|
|
|
|
|> Elasticsearch.search_definition(
|
2019-11-12 04:10:41 +01:00
|
|
|
%{
|
|
|
|
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
|
|
|
},
|
2020-08-23 21:47:42 +02:00
|
|
|
conn.assigns.pagination
|
|
|
|
)
|
|
|
|
|> Elasticsearch.search_records(
|
|
|
|
preload(Comment, [:deleted_by, image: [:tags], user: [awards: :badge]])
|
2019-11-12 04:10:41 +01:00
|
|
|
)
|
|
|
|
|
2020-05-08 04:19:08 +02:00
|
|
|
rendered = TextileRenderer.render_collection(comments.entries, conn)
|
2019-11-12 04:10:41 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
comments = %{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
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-22 22:25:39 +01:00
|
|
|
defp render_index({:error, msg}, conn, _user) do
|
|
|
|
render(conn, "index.html", title: "Comments", error: msg, comments: [])
|
|
|
|
end
|
2019-12-21 16:14:01 +01:00
|
|
|
|
|
|
|
defp filters(%{role: role}) when role in ["moderator", "admin"], do: []
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-21 16:14:01 +01:00
|
|
|
defp filters(_user),
|
|
|
|
do: [%{term: %{hidden_from_users: false}}]
|
2019-11-12 04:10:41 +01:00
|
|
|
end
|