Fix the logic in multi-hashtag TLs

This commit is contained in:
Haelwenn (lanodan) Monnier 2019-01-10 16:44:28 +01:00
parent 4ad0ad14ed
commit 5a84def6a6
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
4 changed files with 37 additions and 7 deletions

View File

@ -426,15 +426,26 @@ defp restrict_since(query, %{"since_id" => since_id}) do
defp restrict_since(query, _), do: query defp restrict_since(query, _), do: query
defp restrict_tag(query, %{"tag" => tag, "tag_reject" => tag_reject}) defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
when is_list(tag) and tag_reject != [] do when is_list(tag_reject) and tag_reject != [] do
from( from(
activity in query, activity in query,
where: fragment("(? #> '{\"object\",\"tag\"}') \\?| ?", activity.data, ^tag),
where: fragment("(not (? #> '{\"object\",\"tag\"}') \\?| ?)", activity.data, ^tag_reject) where: fragment("(not (? #> '{\"object\",\"tag\"}') \\?| ?)", activity.data, ^tag_reject)
) )
end end
defp restrict_tag_reject(query, _), do: query
defp restrict_tag_all(query, %{"tag_all" => tag_all})
when is_list(tag_all) and tag_all != [] do
from(
activity in query,
where: fragment("(? #> '{\"object\",\"tag\"}') \\?& ?", activity.data, ^tag_all)
)
end
defp restrict_tag_all(query, _), do: query
defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
from( from(
activity in query, activity in query,
@ -591,6 +602,8 @@ def fetch_activities_query(recipients, opts \\ %{}) do
base_query base_query
|> restrict_recipients(recipients, opts["user"]) |> restrict_recipients(recipients, opts["user"])
|> restrict_tag(opts) |> restrict_tag(opts)
|> restrict_tag_reject(opts)
|> restrict_tag_all(opts)
|> restrict_since(opts) |> restrict_since(opts)
|> restrict_local(opts) |> restrict_local(opts)
|> restrict_limit(opts) |> restrict_limit(opts)

View File

@ -541,11 +541,16 @@ def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
local_only = params["local"] in [true, "True", "true", "1"] local_only = params["local"] in [true, "True", "true", "1"]
tags = tags =
([params["tag"]] ++ (params["all"] || []) ++ (params["any"] || [])) ([params["tag"]] ++ (params["any"] || []))
|> Enum.uniq() |> Enum.uniq()
|> Enum.filter(& &1) |> Enum.filter(& &1)
|> Enum.map(&String.downcase(&1)) |> Enum.map(&String.downcase(&1))
tag_all =
params["all"] ||
[]
|> Enum.map(&String.downcase(&1))
tag_reject = tag_reject =
params["none"] || params["none"] ||
[] []
@ -557,6 +562,7 @@ def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
|> Map.put("local_only", local_only) |> Map.put("local_only", local_only)
|> Map.put("blocking_user", user) |> Map.put("blocking_user", user)
|> Map.put("tag", tags) |> Map.put("tag", tags)
|> Map.put("tag_all", tag_all)
|> Map.put("tag_reject", tag_reject) |> Map.put("tag_reject", tag_reject)
activities = activities =

View File

@ -81,9 +81,16 @@ test "it fetches the appropriate tag-restricted posts" do
"tag_reject" => ["reject"] "tag_reject" => ["reject"]
}) })
fetch_four =
ActivityPub.fetch_activities([], %{
"tag" => ["test"],
"tag_all" => ["test", "reject"]
})
assert fetch_one == [status_one, status_three] assert fetch_one == [status_one, status_three]
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]
end end
end end

View File

@ -1051,11 +1051,11 @@ test "multi-hashtag timeline", %{conn: conn} do
{:ok, activity_test1} = CommonAPI.post(user, %{"status" => "#test1"}) {:ok, activity_test1} = CommonAPI.post(user, %{"status" => "#test1"})
{:ok, activity_none} = CommonAPI.post(user, %{"status" => "#test #none"}) {:ok, activity_none} = CommonAPI.post(user, %{"status" => "#test #none"})
all_test = any_test =
conn conn
|> get("/api/v1/timelines/tag/test", %{"all" => ["test1"]}) |> get("/api/v1/timelines/tag/test", %{"any" => ["none"]})
assert [status_none, status_test1, status_test] = json_response(all_test, 200) [status_none, status_test1, status_test] = json_response(any_test, 200)
assert to_string(activity_test.id) == status_test["id"] assert to_string(activity_test.id) == status_test["id"]
assert to_string(activity_test1.id) == status_test1["id"] assert to_string(activity_test1.id) == status_test1["id"]
@ -1066,6 +1066,10 @@ test "multi-hashtag timeline", %{conn: conn} do
|> get("/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]}) |> get("/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
assert [status_test1, status_test] == json_response(restricted_test, 200) assert [status_test1, status_test] == json_response(restricted_test, 200)
all_test = conn |> get("/api/v1/timelines/tag/test", %{"all" => ["none"]})
assert [status_none] == json_response(all_test, 200)
end end
test "getting followers", %{conn: conn} do test "getting followers", %{conn: conn} do