[#3213] Reinstated DISTINCT clause for hashtag "any" filtering with 2+ terms. Added test.

This commit is contained in:
Ivan Tashkinov 2021-01-29 00:17:33 +03:00
parent e7864a32d7
commit 380d0cce6b
3 changed files with 28 additions and 8 deletions

View File

@ -846,11 +846,18 @@ defp restrict_hashtag_any(_query, %{tag: _tag, skip_preload: true}) do
end end
defp restrict_hashtag_any(query, %{tag: tags}) when is_list(tags) do defp restrict_hashtag_any(query, %{tag: tags}) when is_list(tags) do
query =
from( from(
[_activity, object] in query, [_activity, object] in query,
join: hashtag in assoc(object, :hashtags), join: hashtag in assoc(object, :hashtags),
where: hashtag.name in ^tags where: hashtag.name in ^tags
) )
if length(tags) > 1 do
distinct(query, [activity], true)
else
query
end
end end
defp restrict_hashtag_any(query, %{tag: tag}) when is_binary(tag) do defp restrict_hashtag_any(query, %{tag: tag}) when is_binary(tag) do

View File

@ -134,9 +134,9 @@ defp hashtag_fetching(params, user, local_only) do
tags = tags =
[params[:tag], params[:any]] [params[:tag], params[:any]]
|> List.flatten() |> List.flatten()
|> Enum.uniq()
|> Enum.reject(&is_nil/1) |> Enum.reject(&is_nil/1)
|> Enum.map(&String.downcase/1) |> Enum.map(&String.downcase/1)
|> Enum.uniq()
tag_all = tag_all =
params params

View File

@ -217,6 +217,9 @@ test "it fetches the appropriate tag-restricted posts" do
{:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"}) {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
{:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"}) {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
{:ok, status_four} = CommonAPI.post(user, %{status: ". #any1 #any2"})
{:ok, status_five} = CommonAPI.post(user, %{status: ". #any2 #any1"})
for hashtag_timeline_strategy <- [true, :prefer_aggregation, false] do for hashtag_timeline_strategy <- [true, :prefer_aggregation, false] do
clear_config([:instance, :improved_hashtag_timeline], hashtag_timeline_strategy) clear_config([:instance, :improved_hashtag_timeline], hashtag_timeline_strategy)
@ -238,8 +241,17 @@ test "it fetches the appropriate tag-restricted posts" do
tag_all: ["test", "reject"] tag_all: ["test", "reject"]
}) })
[fetch_one, fetch_two, fetch_three, fetch_four] = # This test would fail if JOIN with 2+ terms in "any" clause is done without DISTINCT.
Enum.map([fetch_one, fetch_two, fetch_three, fetch_four], fn statuses -> # The :limit is important (w/o DISTINCT 2 records are deduped by Ecto to 1 b/c of preload).
fetch_five =
ActivityPub.fetch_activities([], %{
type: "Create",
tag: ["any1", "any2"],
limit: 2
})
[fetch_one, fetch_two, fetch_three, fetch_four, fetch_five] =
Enum.map([fetch_one, fetch_two, fetch_three, fetch_four, fetch_five], fn statuses ->
Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end) Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end)
end) end)
@ -247,6 +259,7 @@ test "it fetches the appropriate tag-restricted posts" do
assert fetch_two == [status_one, status_two, status_three] assert fetch_two == [status_one, status_two, status_three]
assert fetch_three == [status_one, status_two] assert fetch_three == [status_one, status_two]
assert fetch_four == [status_three] assert fetch_four == [status_three]
assert fetch_five == [status_four, status_five]
end end
end end