AP C2S: allow limit & order on outbox & read_inbox

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-05-08 03:05:56 +02:00
parent 2c18830d0d
commit a43b435c0a
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
3 changed files with 24 additions and 29 deletions

View File

@ -231,28 +231,22 @@ def outbox(
when page? in [true, "true"] do when page? in [true, "true"] do
with %User{} = user <- User.get_cached_by_nickname(nickname), with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- User.ensure_keys_present(user) do {:ok, user} <- User.ensure_keys_present(user) do
activities = # "include_poll_votes" is a hack because postgres generates inefficient
if params["max_id"] do # queries when filtering by 'Answer', poll votes will be hidden by the
ActivityPub.fetch_user_activities(user, for_user, %{ # visibility filter in this case anyway
"max_id" => params["max_id"], params =
# This is a hack because postgres generates inefficient queries when filtering by params
# 'Answer', poll votes will be hidden by the visibility filter in this case anyway |> Map.drop(["nickname", "page"])
"include_poll_votes" => true, |> Map.put("include_poll_votes", true)
"limit" => 10
}) activities = ActivityPub.fetch_user_activities(user, for_user, params)
else
ActivityPub.fetch_user_activities(user, for_user, %{
"limit" => 10,
"include_poll_votes" => true
})
end
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> put_view(UserView) |> put_view(UserView)
|> render("activity_collection_page.json", %{ |> render("activity_collection_page.json", %{
activities: activities, activities: activities,
pagination: ControllerHelper.get_pagination_fields(conn, activities, %{"limit" => "10"}), pagination: ControllerHelper.get_pagination_fields(conn, activities),
iri: "#{user.ap_id}/outbox" iri: "#{user.ap_id}/outbox"
}) })
end end
@ -355,22 +349,23 @@ def read_inbox(
%{"nickname" => nickname, "page" => page?} = params %{"nickname" => nickname, "page" => page?} = params
) )
when page? in [true, "true"] do when page? in [true, "true"] do
params =
params
|> Map.drop(["nickname", "page"])
|> Map.put("blocking_user", user)
|> Map.put("user", user)
activities = activities =
if params["max_id"] do [user.ap_id | User.following(user)]
ActivityPub.fetch_activities([user.ap_id | User.following(user)], %{ |> ActivityPub.fetch_activities(params)
"max_id" => params["max_id"], |> Enum.reverse()
"limit" => 10
})
else
ActivityPub.fetch_activities([user.ap_id | User.following(user)], %{"limit" => 10})
end
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> put_view(UserView) |> put_view(UserView)
|> render("activity_collection_page.json", %{ |> render("activity_collection_page.json", %{
activities: activities, activities: activities,
pagination: ControllerHelper.get_pagination_fields(conn, activities, %{"limit" => "10"}), pagination: ControllerHelper.get_pagination_fields(conn, activities),
iri: "#{user.ap_id}/inbox" iri: "#{user.ap_id}/inbox"
}) })
end end

View File

@ -63,8 +63,8 @@ def get_pagination_fields(conn, activities, extra_params \\ %{}) do
params = params =
conn.params conn.params
|> Map.drop(Map.keys(conn.path_params)) |> Map.drop(Map.keys(conn.path_params))
|> Map.drop(["since_id", "max_id", "min_id"])
|> Map.merge(extra_params) |> Map.merge(extra_params)
|> Map.drop(Pagination.page_keys() -- ["limit", "order"])
min_id = min_id =
activities activities

View File

@ -810,7 +810,7 @@ test "it paginates correctly", %{conn: conn} do
outbox_endpoint = user.ap_id <> "/outbox" outbox_endpoint = user.ap_id <> "/outbox"
_posts = _posts =
for i <- 0..15 do for i <- 0..25 do
{:ok, activity} = CommonAPI.post(user, %{status: "post #{i}"}) {:ok, activity} = CommonAPI.post(user, %{status: "post #{i}"})
activity activity
end end
@ -822,8 +822,8 @@ test "it paginates correctly", %{conn: conn} do
|> json_response(200) |> json_response(200)
result_ids = Enum.map(result["orderedItems"], fn x -> x["id"] end) result_ids = Enum.map(result["orderedItems"], fn x -> x["id"] end)
assert length(result["orderedItems"]) == 10 assert length(result["orderedItems"]) == 20
assert length(result_ids) == 10 assert length(result_ids) == 20
assert result["next"] assert result["next"]
assert String.starts_with?(result["next"], outbox_endpoint) assert String.starts_with?(result["next"], outbox_endpoint)