aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/pleroma_api/controllers/notification_controller.ex
blob: f860eaf7e6649a76958a3549768db56ef4ac4fa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.PleromaAPI.NotificationController do
  use Pleroma.Web, :controller

  alias Pleroma.Notification

  plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)

  plug(
    Pleroma.Web.Plugs.OAuthScopesPlug,
    %{scopes: ["write:notifications"]} when action == :mark_as_read
  )

  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation

  def mark_as_read(
        %{
          assigns: %{user: user},
          private: %{open_api_spex: %{body_params: %{id: notification_id}}}
        } = conn,
        _
      ) do
    with {:ok, notification} <- Notification.read_one(user, notification_id) do
      render(conn, "show.json", notification: notification, for: user)
    else
      {:error, message} ->
        conn
        |> put_status(:bad_request)
        |> json(%{"error" => message})
    end
  end

  def mark_as_read(
        %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{max_id: max_id}}}} =
          conn,
        _
      ) do
    notifications =
      user
      |> Notification.set_read_up_to(max_id)
      |> Enum.take(80)

    render(conn, "index.json", notifications: notifications, for: user)
  end
end