Merge branch '1873-pagination-fixes' into 'develop'

[#1873] :offset pagination param support, hashtags pagination

Closes #1873

See merge request pleroma/pleroma!2659
This commit is contained in:
lain 2020-06-19 13:42:57 +00:00
commit cafdf18408
5 changed files with 48 additions and 13 deletions

View File

@ -64,6 +64,12 @@ def fetch_paginated(query, params, :offset, table_binding) do
@spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()] @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
def paginate(query, options, method \\ :keyset, table_binding \\ nil) def paginate(query, options, method \\ :keyset, table_binding \\ nil)
def paginate(list, options, _method, _table_binding) when is_list(list) do
offset = options[:offset] || 0
limit = options[:limit] || 0
Enum.slice(list, offset, limit)
end
def paginate(query, options, :keyset, table_binding) do def paginate(query, options, :keyset, table_binding) do
query query
|> restrict(:min_id, options, table_binding) |> restrict(:min_id, options, table_binding)

View File

@ -39,6 +39,12 @@ def pagination_params do
:string, :string,
"Return the newest items newer than this ID" "Return the newest items newer than this ID"
), ),
Operation.parameter(
:offset,
:query,
%Schema{type: :integer, default: 0},
"Return items past this number of items"
),
Operation.parameter( Operation.parameter(
:limit, :limit,
:query, :query,

View File

@ -107,21 +107,21 @@ defp resource_search(_, "statuses", query, options) do
) )
end end
defp resource_search(:v2, "hashtags", query, _options) do defp resource_search(:v2, "hashtags", query, options) do
tags_path = Web.base_url() <> "/tag/" tags_path = Web.base_url() <> "/tag/"
query query
|> prepare_tags() |> prepare_tags(options)
|> Enum.map(fn tag -> |> Enum.map(fn tag ->
%{name: tag, url: tags_path <> tag} %{name: tag, url: tags_path <> tag}
end) end)
end end
defp resource_search(:v1, "hashtags", query, _options) do defp resource_search(:v1, "hashtags", query, options) do
prepare_tags(query) prepare_tags(query, options)
end end
defp prepare_tags(query, add_joined_tag \\ true) do defp prepare_tags(query, options) do
tags = tags =
query query
|> preprocess_uri_query() |> preprocess_uri_query()
@ -139,13 +139,20 @@ defp prepare_tags(query, add_joined_tag \\ true) do
tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end) tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end)
if Enum.empty?(explicit_tags) && add_joined_tag do tags =
tags if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do
|> Kernel.++([joined_tag(tags)]) add_joined_tag(tags)
|> Enum.uniq_by(&String.downcase/1) else
else tags
tags end
end
Pleroma.Pagination.paginate(tags, options)
end
defp add_joined_tag(tags) do
tags
|> Kernel.++([joined_tag(tags)])
|> Enum.uniq_by(&String.downcase/1)
end end
# If `query` is a URI, returns last component of its path, otherwise returns `query` # If `query` is a URI, returns last component of its path, otherwise returns `query`

View File

@ -151,6 +151,22 @@ test "constructs hashtags from search query", %{conn: conn} do
] ]
end end
test "supports pagination of hashtags search results", %{conn: conn} do
results =
conn
|> get(
"/api/v2/search?#{
URI.encode_query(%{q: "#some #text #with #hashtags", limit: 2, offset: 1})
}"
)
|> json_response_and_validate_schema(200)
assert results["hashtags"] == [
%{"name" => "text", "url" => "#{Web.base_url()}/tag/text"},
%{"name" => "with", "url" => "#{Web.base_url()}/tag/with"}
]
end
test "excludes a blocked users from search results", %{conn: conn} do test "excludes a blocked users from search results", %{conn: conn} do
user = insert(:user) user = insert(:user)
user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"}) user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})

View File

@ -1561,7 +1561,7 @@ test "favorites paginate correctly" do
# Using the header for pagination works correctly # Using the header for pagination works correctly
[next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ") [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
[_, max_id] = Regex.run(~r/max_id=(.*)>;/, next) [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
assert max_id == third_favorite.id assert max_id == third_favorite.id