philomena/lib/philomena_web/controllers/notification_controller.ex

35 lines
928 B
Elixir
Raw Normal View History

2019-11-16 04:50:58 +01:00
defmodule PhilomenaWeb.NotificationController do
use PhilomenaWeb, :controller
alias Philomena.Notifications.{UnreadNotification, Notification}
alias Philomena.Polymorphic
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
user = conn.assigns.current_user
notifications =
from n in Notification,
2020-01-11 05:20:19 +01:00
join: un in UnreadNotification,
on: un.notification_id == n.id,
2019-11-16 04:50:58 +01:00
where: un.user_id == ^user.id
notifications =
notifications
2019-12-20 15:35:49 +01:00
|> order_by(desc: :updated_at)
2019-11-16 04:50:58 +01:00
|> Repo.paginate(conn.assigns.scrivener)
entries =
notifications.entries
|> Polymorphic.load_polymorphic(
actor: [actor_id: :actor_type],
actor_child: [actor_child_id: :actor_child_type]
)
2020-01-11 05:20:19 +01:00
notifications = %{notifications | entries: entries}
2019-11-16 04:50:58 +01:00
2019-12-16 20:24:38 +01:00
render(conn, "index.html", title: "Notification Area", notifications: notifications)
2019-11-16 04:50:58 +01:00
end
2019-12-16 20:24:38 +01:00
end