2019-11-17 21:37:01 +01:00
|
|
|
defmodule PhilomenaWeb.PostController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2020-05-08 04:19:08 +02:00
|
|
|
alias Philomena.{Posts.Query, Posts.Post}
|
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)
|
2019-12-21 16:14:01 +01:00
|
|
|
user = conn.assigns.current_user
|
2019-12-02 15:37:01 +01:00
|
|
|
|
2024-07-13 20:47:45 +02:00
|
|
|
pq
|
|
|
|
|> Query.compile(user: user)
|
2019-12-22 22:25:39 +01:00
|
|
|
|> render_index(conn, user)
|
|
|
|
end
|
2019-11-17 23:14:20 +01:00
|
|
|
|
2019-12-22 22:25:39 +01:00
|
|
|
defp render_index({:ok, query}, conn, user) do
|
2019-11-17 23:14:20 +01:00
|
|
|
posts =
|
2020-08-23 21:47:42 +02:00
|
|
|
Post
|
2024-05-25 20:03:45 +02:00
|
|
|
|> Search.search_definition(
|
2019-11-17 23:14:20 +01:00
|
|
|
%{
|
|
|
|
query: %{
|
|
|
|
bool: %{
|
2019-12-21 16:14:01 +01:00
|
|
|
must: [query | filters(user)]
|
2019-11-17 23:14:20 +01:00
|
|
|
}
|
2019-12-21 16:15:05 +01:00
|
|
|
},
|
2019-12-01 02:44:57 +01:00
|
|
|
sort: %{created_at: :desc}
|
2019-11-17 23:14:20 +01:00
|
|
|
},
|
2020-08-23 21:47:42 +02:00
|
|
|
conn.assigns.pagination
|
|
|
|
)
|
2024-05-25 20:03:45 +02:00
|
|
|
|> Search.search_records(
|
2020-08-23 21:47:42 +02:00
|
|
|
preload(Post, [:deleted_by, topic: :forum, user: [awards: :badge]])
|
2019-11-17 23:14:20 +01:00
|
|
|
)
|
|
|
|
|
2021-09-29 22:24:38 +02:00
|
|
|
rendered = MarkdownRenderer.render_collection(posts.entries, conn)
|
2019-11-17 23:14:20 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
posts = %{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
|
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: "Posts", error: msg, posts: [])
|
|
|
|
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(%{role: "assistant"}) do
|
|
|
|
[
|
2020-09-26 16:10:15 +02:00
|
|
|
%{terms: %{access_level: ["normal", "assistant"]}}
|
2019-12-21 16:14:01 +01:00
|
|
|
]
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-21 16:14:01 +01:00
|
|
|
defp filters(_user) do
|
|
|
|
[
|
|
|
|
%{term: %{access_level: "normal"}},
|
|
|
|
%{term: %{deleted: false}}
|
|
|
|
]
|
|
|
|
end
|
2019-12-01 02:44:57 +01:00
|
|
|
end
|