Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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
10 plug(Pleroma.Web.ApiSpec.CastAndValidate)
11
12 plug(
13 Pleroma.Web.Plugs.OAuthScopesPlug,
14 %{scopes: ["write:notifications"]} when action == :mark_as_read
15 )
16
17 plug(:put_view, Pleroma.Web.MastodonAPI.NotificationView)
18
19 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation
20
21 def mark_as_read(%{assigns: %{user: user}, body_params: %{id: notification_id}} = conn, _) do
22 with {:ok, notification} <- Notification.read_one(user, notification_id) do
23 render(conn, "show.json", notification: notification, for: user)
24 else
25 {:error, message} ->
26 conn
27 |> put_status(:bad_request)
28 |> json(%{"error" => message})
29 end
30 end
31
32 def mark_as_read(%{assigns: %{user: user}, body_params: %{max_id: max_id}} = conn, _) do
33 notifications =
34 user
35 |> Notification.set_read_up_to(max_id)
36 |> Enum.take(80)
37
38 render(conn, "index.json", notifications: notifications, for: user)
39 end
40 end