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
|
|
|
|
|
2020-05-09 09:05:44 -06:00
|
|
|
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
|
2019-08-02 11:53:08 -06:00
|
|
|
|
|
|
|
alias Pleroma.Conversation.Participation
|
2019-09-04 02:33:08 -06:00
|
|
|
alias Pleroma.Notification
|
2019-09-15 09:22:08 -06:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-08-02 11:53:08 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-08-05 07:09:19 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.ConversationView
|
2019-09-04 02:33:08 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.NotificationView
|
2019-08-05 08:11:23 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2019-08-02 11:53:08 -06:00
|
|
|
|
2020-05-19 11:52:26 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2019-09-15 09:22:08 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2019-12-15 12:32:42 -07:00
|
|
|
%{scopes: ["read:statuses"]}
|
2020-01-20 05:05:35 -07:00
|
|
|
when action in [:conversation, :conversation_statuses]
|
2019-12-15 12:32:42 -07:00
|
|
|
)
|
|
|
|
|
2019-09-15 09:22:08 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2020-04-21 07:29:19 -06:00
|
|
|
%{scopes: ["write:conversations"]}
|
|
|
|
when action in [:update_conversation, :mark_conversations_as_read]
|
2019-09-15 09:22:08 -06:00
|
|
|
)
|
|
|
|
|
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 conversation(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
|
2019-08-12 05:58:04 -06:00
|
|
|
with %Participation{} = participation <- Participation.get(participation_id),
|
|
|
|
true <- user.id == participation.user_id do
|
|
|
|
conn
|
|
|
|
|> put_view(ConversationView)
|
2019-08-12 06:23:06 -06:00
|
|
|
|> render("participation.json", %{participation: participation, for: user})
|
2020-03-01 20:23:29 -07:00
|
|
|
else
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json(%{"error" => "Unknown conversation id"})
|
2019-08-12 05:58:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-02 11:53:08 -06:00
|
|
|
def conversation_statuses(
|
2020-03-24 10:18:27 -06:00
|
|
|
%{assigns: %{user: %{id: user_id} = user}} = conn,
|
2020-05-19 11:52:26 -06:00
|
|
|
%{id: participation_id} = params
|
2019-08-02 11:53:08 -06:00
|
|
|
) do
|
2020-03-24 10:18:27 -06:00
|
|
|
with %Participation{user_id: ^user_id} = participation <-
|
|
|
|
Participation.get(participation_id, preload: [:conversation]) do
|
2019-09-06 04:08:47 -06:00
|
|
|
params =
|
|
|
|
params
|
2020-05-19 11:52:26 -06:00
|
|
|
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|
2019-09-06 04:08:47 -06:00
|
|
|
|> Map.put("blocking_user", user)
|
|
|
|
|> Map.put("muting_user", user)
|
|
|
|
|> Map.put("user", user)
|
|
|
|
|
2019-08-02 11:53:08 -06:00
|
|
|
activities =
|
|
|
|
participation.conversation.ap_id
|
2020-03-24 10:18:27 -06:00
|
|
|
|> ActivityPub.fetch_activities_for_context_query(params)
|
|
|
|
|> Pleroma.Pagination.fetch_paginated(Map.put(params, "total", false))
|
2019-08-02 11:53:08 -06:00
|
|
|
|> Enum.reverse()
|
|
|
|
|
|
|
|
conn
|
2019-09-06 04:08:47 -06:00
|
|
|
|> add_link_headers(activities)
|
2019-08-02 11:53:08 -06:00
|
|
|
|> put_view(StatusView)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-05-09 09:05:44 -06:00
|
|
|
as: :activity
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2020-03-01 20:23:29 -07:00
|
|
|
else
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json(%{"error" => "Unknown conversation id"})
|
2019-08-02 11:53:08 -06:00
|
|
|
end
|
|
|
|
end
|
2019-08-05 07:09:19 -06:00
|
|
|
|
|
|
|
def update_conversation(
|
|
|
|
%{assigns: %{user: user}} = conn,
|
2020-05-19 11:52:26 -06:00
|
|
|
%{id: participation_id, recipients: recipients}
|
2019-08-05 07:09:19 -06:00
|
|
|
) do
|
2020-03-01 20:23:29 -07:00
|
|
|
with %Participation{} = participation <- Participation.get(participation_id),
|
|
|
|
true <- user.id == participation.user_id,
|
2019-08-14 07:56:15 -06:00
|
|
|
{:ok, participation} <- Participation.set_recipients(participation, recipients) do
|
2019-08-05 07:09:19 -06:00
|
|
|
conn
|
|
|
|
|> put_view(ConversationView)
|
2019-08-12 06:23:06 -06:00
|
|
|
|> render("participation.json", %{participation: participation, for: user})
|
2020-03-01 20:23:29 -07:00
|
|
|
else
|
|
|
|
{:error, message} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{"error" => message})
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json(%{"error" => "Unknown conversation id"})
|
2019-08-05 07:09:19 -06:00
|
|
|
end
|
|
|
|
end
|
2019-09-04 02:33:08 -06:00
|
|
|
|
2020-04-21 07:29:19 -06:00
|
|
|
def mark_conversations_as_read(%{assigns: %{user: user}} = conn, _params) do
|
2019-10-25 12:29:23 -06:00
|
|
|
with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
|
2019-10-10 21:40:58 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(participations)
|
|
|
|
|> put_view(ConversationView)
|
|
|
|
|> render("participations.json", participations: participations, for: user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 11:52:26 -06:00
|
|
|
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
|