aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex
blob: 37c2222defd5926cf48f0f7275377755f51b8311 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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

  import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]

  alias Pleroma.Activity
  alias Pleroma.SubscriptionNotification
  alias Pleroma.User
  alias Pleroma.Web.PleromaAPI.PleromaAPI

  def index(%{assigns: %{user: user}} = conn, params) do
    notifications =
      user
      |> PleromaAPI.get_subscription_notifications(params)
      |> Enum.map(&build_notification_data/1)

    conn
    |> add_link_headers(notifications)
    |> render("index.json", %{notifications: notifications, for: user})
  end

  def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
    with {:ok, notification} <- SubscriptionNotification.get(user, id) do
      render(conn, "show.json", %{
        subscription_notification: build_notification_data(notification),
        for: user
      })
    else
      {:error, reason} ->
        conn
        |> put_status(:forbidden)
        |> json(%{"error" => reason})
    end
  end

  def clear(%{assigns: %{user: user}} = conn, _params) do
    SubscriptionNotification.clear(user)
    json(conn, %{})
  end

  def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
    with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do
      json(conn, %{})
    else
      {:error, reason} ->
        conn
        |> put_status(:forbidden)
        |> json(%{"error" => reason})
    end
  end

  def destroy_multiple(
        %{assigns: %{user: user}} = conn,
        %{"ids" => ids} = _params
      ) do
    SubscriptionNotification.destroy_multiple(user, ids)
    json(conn, %{})
  end

  defp build_notification_data(%{activity: %{data: data}} = notification) do
    %{
      notification: notification,
      actor: User.get_cached_by_ap_id(data["actor"]),
      parent_activity: Activity.get_create_by_object_ap_id(data["object"])
    }
  end
end