Implement viewing source

This commit is contained in:
Tusooa Zhu 2022-05-30 00:59:23 -04:00 committed by FloatingGhost
parent 5fe133fbc9
commit 3f186180ec
4 changed files with 74 additions and 1 deletions

View file

@ -444,6 +444,20 @@ def show_history_operation 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
@ -662,6 +676,28 @@ defp status_history_response 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

@ -212,7 +212,16 @@ def show_history(%{assigns: %{user: user}} = conn, %{id: id} = params) 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

@ -465,6 +465,16 @@ def render(
}
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

@ -2223,4 +2223,22 @@ test "should not allow translating of statuses you cannot see", %{conn: conn} do
json_response_and_validate_schema(conn, 404)
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