2019-08-02 11:53:08 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-08-02 11:53:08 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2019-09-04 02:33:08 -06:00
|
|
|
alias Pleroma.Notification
|
2019-09-15 09:22:08 -06:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-09-04 02:33:08 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.NotificationView
|
2019-08-02 11:53:08 -06:00
|
|
|
|
2020-05-19 11:52:26 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2020-04-21 07:29:19 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:notifications"]} when action == :mark_notifications_as_read
|
|
|
|
)
|
2019-10-02 11:42:40 -06:00
|
|
|
|
2020-05-19 11:52:26 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaOperation
|
|
|
|
|
|
|
|
def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{id: notification_id}) do
|
2019-09-04 02:33:08 -06:00
|
|
|
with {:ok, notification} <- Notification.read_one(user, notification_id) do
|
|
|
|
conn
|
|
|
|
|> put_view(NotificationView)
|
|
|
|
|> render("show.json", %{notification: notification, for: user})
|
|
|
|
else
|
|
|
|
{:error, message} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{"error" => message})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 11:52:26 -06:00
|
|
|
def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{max_id: max_id}) do
|
2019-09-04 02:33:08 -06:00
|
|
|
with notifications <- Notification.set_read_up_to(user, max_id) do
|
|
|
|
notifications = Enum.take(notifications, 80)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(NotificationView)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
notifications: notifications,
|
2020-05-09 09:05:44 -06:00
|
|
|
for: user
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-04 02:33:08 -06:00
|
|
|
end
|
|
|
|
end
|
2019-08-02 11:53:08 -06:00
|
|
|
end
|