2019-09-16 20:59:49 +02:00
|
|
|
# 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]
|
|
|
|
|
2019-09-17 16:44:10 +02:00
|
|
|
alias Pleroma.Activity
|
2019-09-16 20:59:49 +02:00
|
|
|
alias Pleroma.SubscriptionNotification
|
2019-09-17 16:44:10 +02:00
|
|
|
alias Pleroma.User
|
2019-09-16 20:59:49 +02:00
|
|
|
alias Pleroma.Web.PleromaAPI.PleromaAPI
|
|
|
|
|
2019-09-17 15:48:24 +02:00
|
|
|
def index(%{assigns: %{user: user}} = conn, params) do
|
2019-09-17 16:44:10 +02:00
|
|
|
notifications =
|
|
|
|
user
|
|
|
|
|> PleromaAPI.get_subscription_notifications(params)
|
|
|
|
|> Enum.map(&build_notification_data/1)
|
2019-09-16 20:59:49 +02:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(notifications)
|
|
|
|
|> render("index.json", %{notifications: notifications, for: user})
|
|
|
|
end
|
|
|
|
|
2019-09-17 15:48:24 +02:00
|
|
|
def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
|
2019-09-16 20:59:49 +02:00
|
|
|
with {:ok, notification} <- SubscriptionNotification.get(user, id) do
|
2019-09-17 16:44:10 +02:00
|
|
|
render(conn, "show.json", %{
|
|
|
|
subscription_notification: build_notification_data(notification),
|
|
|
|
for: user
|
|
|
|
})
|
2019-09-16 20:59:49 +02:00
|
|
|
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
|
2019-09-17 16:44:10 +02:00
|
|
|
|
|
|
|
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
|
2019-09-16 20:59:49 +02:00
|
|
|
end
|