Implement viewing source

This commit is contained in:
Tusooa Zhu 2022-05-30 00:59:23 -04:00
parent c004eb0fa2
commit 393b508846
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
4 changed files with 74 additions and 1 deletions

View File

@ -456,6 +456,20 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
end
def show_source_operation do
%Operation{
tags: ["Retrieve status source"],
summary: "Status source",
description: "View source of a status",
operationId: "StatusController.show_source",
security: [%{"oAuth" => ["read:statuses"]}],
parameters: [
id_param()
],
responses: %{
200 => status_source_response(),
404 => Operation.response("Not Found", "application/json", ApiError)
}
}
end
def update_operation do
@ -661,6 +675,28 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
)
end
defp status_source_response do
Operation.response(
"Status Source",
"application/json",
%Schema{
type: :object,
properties: %{
id: FlakeID,
text: %Schema{
type: :string,
description: "Raw source of status content"
},
spoiler_text: %Schema{
type: :string,
description:
"Subject or summary line, below which status content is collapsed until expanded"
}
}
}
)
end
defp context do
%Schema{
title: "StatusContext",

View File

@ -210,7 +210,16 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
end
@doc "GET /api/v1/statuses/:id/source"
def show_source(%{assigns: %{user: _user}} = _conn, %{id: _id} = _params) do
def show_source(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
true <- Visibility.visible_for_user?(activity, user) do
try_render(conn, "source.json",
activity: activity,
for: user
)
else
_ -> {:error, :not_found}
end
end
@doc "PUT /api/v1/statuses/:id"

View File

@ -449,6 +449,16 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
}
end
def render("source.json", %{activity: %{data: %{"object" => _object}} = activity} = _opts) do
object = Object.normalize(activity, fetch: false)
%{
id: activity.id,
text: Map.get(object.data, "source", ""),
spoiler_text: Map.get(object.data, "summary", "")
}
end
def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
page_url_data = URI.parse(page_url)

View File

@ -2036,4 +2036,22 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
json_response_and_validate_schema(conn, 200)
end
end
describe "get status source" do
setup do
oauth_access(["read:statuses"])
end
test "it returns the source", %{conn: conn} do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{status: "mew mew #abc", spoiler_text: "#def"})
conn = get(conn, "/api/v1/statuses/#{activity.id}/source")
id = activity.id
assert %{"id" => ^id, "text" => "mew mew #abc", "spoiler_text" => "#def"} = json_response_and_validate_schema(conn, 200)
end
end
end