2019-11-17 21:37:01 +01:00
|
|
|
defmodule PhilomenaWeb.PostController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-01 02:44:57 +01:00
|
|
|
alias Philomena.{Posts.Query, Posts.Post, Textile.Renderer}
|
2019-11-17 23:14:20 +01:00
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def index(conn, params) do
|
2019-12-02 15:37:01 +01:00
|
|
|
pq = params["pq"] || "created_at.gte:1 week ago"
|
2019-12-01 02:44:57 +01:00
|
|
|
|
2019-12-02 15:37:01 +01:00
|
|
|
params = Map.put(conn.params, "pq", pq)
|
|
|
|
conn = Map.put(conn, :params, params)
|
|
|
|
|
|
|
|
{:ok, query} = Query.compile(conn.assigns.current_user, pq)
|
2019-11-17 23:14:20 +01:00
|
|
|
|
|
|
|
posts =
|
|
|
|
Post.search_records(
|
|
|
|
%{
|
|
|
|
query: %{
|
|
|
|
bool: %{
|
2019-12-01 02:44:57 +01:00
|
|
|
must: [
|
|
|
|
query,
|
|
|
|
%{term: %{access_level: "normal"}},
|
|
|
|
],
|
|
|
|
must_not: [
|
|
|
|
%{term: %{hidden_from_users: true}}
|
|
|
|
]
|
2019-11-17 23:14:20 +01:00
|
|
|
}
|
|
|
|
},
|
2019-12-01 02:44:57 +01:00
|
|
|
sort: %{created_at: :desc}
|
2019-11-17 23:14:20 +01:00
|
|
|
},
|
|
|
|
conn.assigns.pagination,
|
2019-12-20 19:20:26 +01:00
|
|
|
Post |> preload([:deleted_by, topic: :forum, user: [awards: :badge]])
|
2019-11-17 23:14:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
rendered =
|
|
|
|
posts.entries
|
2019-12-01 18:11:00 +01:00
|
|
|
|> Renderer.render_collection(conn)
|
2019-11-17 23:14:20 +01:00
|
|
|
|
|
|
|
posts =
|
2019-12-01 02:44:57 +01:00
|
|
|
%{posts | entries: Enum.zip(rendered, posts.entries)}
|
2019-11-17 23:14:20 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "Posts", posts: posts)
|
2019-11-17 21:37:01 +01:00
|
|
|
end
|
2019-12-01 02:44:57 +01:00
|
|
|
end
|