total rebase
[anni] / lib / pleroma / web / pleroma_api / controllers / notification_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
10   plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
11
12   plug(
13     Pleroma.Web.Plugs.OAuthScopesPlug,
14     %{scopes: ["write:notifications"]} when action == :mark_as_read
15   )
16
17   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation
18
19   def mark_as_read(
20         %{
21           assigns: %{user: user},
22           private: %{open_api_spex: %{body_params: %{id: notification_id}}}
23         } = conn,
24         _
25       ) do
26     with {:ok, notification} <- Notification.read_one(user, notification_id) do
27       render(conn, "show.json", notification: notification, for: user)
28     else
29       {:error, message} ->
30         conn
31         |> put_status(:bad_request)
32         |> json(%{"error" => message})
33     end
34   end
35
36   def mark_as_read(
37         %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{max_id: max_id}}}} =
38           conn,
39         _
40       ) do
41     notifications =
42       user
43       |> Notification.set_read_up_to(max_id)
44       |> Enum.take(80)
45
46     render(conn, "index.json", notifications: notifications, for: user)
47   end
48 end