Refactor User.post_register_action/1 emails
[akkoma] / lib / pleroma / web / pleroma_api / controllers / notification_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.NotificationController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Notification
9 alias Pleroma.Plugs.OAuthScopesPlug
10
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
12 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :mark_as_read)
13 plug(:put_view, Pleroma.Web.MastodonAPI.NotificationView)
14
15 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation
16
17 def mark_as_read(%{assigns: %{user: user}, body_params: %{id: notification_id}} = conn, _) do
18 with {:ok, notification} <- Notification.read_one(user, notification_id) do
19 render(conn, "show.json", notification: notification, for: user)
20 else
21 {:error, message} ->
22 conn
23 |> put_status(:bad_request)
24 |> json(%{"error" => message})
25 end
26 end
27
28 def mark_as_read(%{assigns: %{user: user}, body_params: %{max_id: max_id}} = conn, _) do
29 notifications =
30 user
31 |> Notification.set_read_up_to(max_id)
32 |> Enum.take(80)
33
34 render(conn, "index.json", notifications: notifications, for: user)
35 end
36 end