Move subscription notifications to a separate controller
[akkoma] / lib / pleroma / web / pleroma_api / subscription_notification_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
9
10 alias Pleroma.SubscriptionNotification
11 alias Pleroma.Web.PleromaAPI.PleromaAPI
12 alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView
13
14 def list(%{assigns: %{user: user}} = conn, params) do
15 notifications = PleromaAPI.get_subscription_notifications(user, params)
16
17 conn
18 |> add_link_headers(notifications)
19 |> put_view(SubscriptionNotificationView)
20 |> render("index.json", %{notifications: notifications, for: user})
21 end
22
23 def get(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
24 with {:ok, notification} <- SubscriptionNotification.get(user, id) do
25 conn
26 |> put_view(SubscriptionNotificationView)
27 |> render("show.json", %{subscription_notification: notification, for: user})
28 else
29 {:error, reason} ->
30 conn
31 |> put_status(:forbidden)
32 |> json(%{"error" => reason})
33 end
34 end
35
36 def clear(%{assigns: %{user: user}} = conn, _params) do
37 SubscriptionNotification.clear(user)
38 json(conn, %{})
39 end
40
41 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
42 with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do
43 json(conn, %{})
44 else
45 {:error, reason} ->
46 conn
47 |> put_status(:forbidden)
48 |> json(%{"error" => reason})
49 end
50 end
51
52 def destroy_multiple(
53 %{assigns: %{user: user}} = conn,
54 %{"ids" => ids} = _params
55 ) do
56 SubscriptionNotification.destroy_multiple(user, ids)
57 json(conn, %{})
58 end
59 end