Allow to search groups by location

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-08-05 16:44:08 +02:00
parent 3bae65374f
commit 3c077c59ad
18 changed files with 408 additions and 149 deletions

View File

@@ -17,7 +17,7 @@ defmodule Mobilizon.GraphQL.API.SearchTest do
with_mock ActivityPub,
find_or_make_actor_from_nickname: fn "toto@domain.tld" -> {:ok, %Actor{id: 42}} end do
assert {:ok, %Page{total: 1, elements: [%Actor{id: 42}]}} ==
Search.search_actors("toto@domain.tld", 1, 10, :Person)
Search.search_actors(%{term: "toto@domain.tld"}, 1, 10, :Person)
assert_called(ActivityPub.find_or_make_actor_from_nickname("toto@domain.tld"))
end
@@ -27,7 +27,7 @@ defmodule Mobilizon.GraphQL.API.SearchTest do
with_mock ActivityPub,
fetch_object_from_url: fn "https://social.tcit.fr/users/tcit" -> {:ok, %Actor{id: 42}} end do
assert {:ok, %Page{total: 1, elements: [%Actor{id: 42}]}} ==
Search.search_actors("https://social.tcit.fr/users/tcit", 1, 10, :Person)
Search.search_actors(%{term: "https://social.tcit.fr/users/tcit"}, 1, 10, :Person)
assert_called(ActivityPub.fetch_object_from_url("https://social.tcit.fr/users/tcit"))
end
@@ -35,13 +35,15 @@ defmodule Mobilizon.GraphQL.API.SearchTest do
test "search actors" do
with_mock Actors,
build_actors_by_username_or_name_page: fn "toto", _type, 1, 10 ->
build_actors_by_username_or_name_page: fn %{term: "toto"}, _type, 1, 10 ->
%Page{total: 1, elements: [%Actor{id: 42}]}
end do
assert {:ok, %{total: 1, elements: [%Actor{id: 42}]}} =
Search.search_actors("toto", 1, 10, :Person)
Search.search_actors(%{term: "toto"}, 1, 10, :Person)
assert_called(Actors.build_actors_by_username_or_name_page("toto", [:Person], 1, 10))
assert_called(
Actors.build_actors_by_username_or_name_page(%{term: "toto"}, [:Person], 1, 10)
)
end
end

View File

@@ -208,6 +208,24 @@ defmodule Mobilizon.GraphQL.Resolvers.SearchTest do
end
describe "search_persons/3" do
@search_persons_query """
query SearchPersons($term: String!, $page: Int, $limit: Int) {
searchPersons(term: $term, page: $page, limit: $limit) {
total
elements {
id
avatar {
url
}
domain
preferredUsername
name
__typename
}
}
}
"""
test "finds persons with basic search", %{
conn: conn,
user: user
@@ -217,29 +235,17 @@ defmodule Mobilizon.GraphQL.Resolvers.SearchTest do
event = insert(:event, title: "test_event")
Workers.BuildSearch.insert_search_event(event)
query = """
{
search_persons(search: "test") {
total,
elements {
preferredUsername,
__typename
}
},
}
"""
res =
conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "search"))
AbsintheHelpers.graphql_query(conn,
query: @search_persons_query,
variables: %{term: "test"}
)
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["search_persons"]["total"] == 1
assert json_response(res, 200)["data"]["search_persons"]["elements"] |> length == 1
assert res["errors"] == nil
assert res["data"]["searchPersons"]["total"] == 1
assert res["data"]["searchPersons"]["elements"] |> length == 1
assert hd(json_response(res, 200)["data"]["search_persons"]["elements"])[
"preferredUsername"
] ==
assert hd(res["data"]["searchPersons"]["elements"])["preferredUsername"] ==
actor.preferred_username
end
@@ -256,36 +262,41 @@ defmodule Mobilizon.GraphQL.Resolvers.SearchTest do
Workers.BuildSearch.insert_search_event(event2)
Workers.BuildSearch.insert_search_event(event3)
query = """
{
search_persons(search: "pineapple") {
total,
elements {
preferredUsername,
__typename
}
}
}
"""
res =
conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "search"))
AbsintheHelpers.graphql_query(conn,
query: @search_persons_query,
variables: %{term: "pineapple"}
)
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["search_persons"]["total"] == 1
assert res["errors"] == nil
assert res["data"]["searchPersons"]["total"] == 1
assert json_response(res, 200)["data"]["search_persons"]["elements"]
assert res["data"]["searchPersons"]["elements"]
|> length == 1
assert hd(json_response(res, 200)["data"]["search_persons"]["elements"])[
"preferredUsername"
] ==
assert hd(res["data"]["searchPersons"]["elements"])["preferredUsername"] ==
actor.preferred_username
end
end
describe "search_groups/3" do
@search_groups_query """
query SearchGroups($term: String, $location: String, $radius: Float) {
searchGroups(term: $term, location: $location, radius: $radius) {
total
elements {
avatar {
url
}
domain
preferredUsername
name
__typename
}
}
}
"""
test "finds persons with basic search", %{
conn: conn,
user: user
@@ -295,27 +306,17 @@ defmodule Mobilizon.GraphQL.Resolvers.SearchTest do
event = insert(:event, title: "test_event")
Workers.BuildSearch.insert_search_event(event)
query = """
{
search_groups(search: "test") {
total,
elements {
preferredUsername,
__typename
}
},
}
"""
res =
conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "search"))
AbsintheHelpers.graphql_query(conn,
query: @search_groups_query,
variables: %{term: "test"}
)
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["search_groups"]["total"] == 1
assert json_response(res, 200)["data"]["search_groups"]["elements"] |> length == 1
assert res["errors"] == nil
assert res["data"]["searchGroups"]["total"] == 1
assert res["data"]["searchGroups"]["elements"] |> length == 1
assert hd(json_response(res, 200)["data"]["search_groups"]["elements"])["preferredUsername"] ==
assert hd(res["data"]["searchGroups"]["elements"])["preferredUsername"] ==
group.preferred_username
end
@@ -328,28 +329,54 @@ defmodule Mobilizon.GraphQL.Resolvers.SearchTest do
event = insert(:event, title: "Tour du monde des Kafés")
Workers.BuildSearch.insert_search_event(event)
# Elaborate query
query = """
{
search_groups(search: "Kafé") {
total,
elements {
preferredUsername,
__typename
}
}
}
"""
res =
AbsintheHelpers.graphql_query(conn,
query: @search_groups_query,
variables: %{term: "Kafé"}
)
assert res["errors"] == nil
assert res["data"]["searchGroups"]["total"] == 1
assert hd(res["data"]["searchGroups"]["elements"])["preferredUsername"] ==
group.preferred_username
end
test "finds groups with location", %{conn: conn} do
{lon, lat} = {45.75, 4.85}
point = %Geo.Point{coordinates: {lon, lat}, srid: 4326}
geohash = Geohax.encode(lon, lat, 6)
geohash_2 = Geohax.encode(25, -19, 6)
address = insert(:address, geom: point)
group =
insert(:actor,
type: :Group,
preferred_username: "want_coffee",
name: "Want coffee ?",
physical_address: address
)
res =
conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "search"))
AbsintheHelpers.graphql_query(conn,
query: @search_groups_query,
variables: %{location: geohash}
)
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["search_groups"]["total"] == 1
assert res["errors"] == nil
assert res["data"]["searchGroups"]["total"] == 1
assert hd(json_response(res, 200)["data"]["search_groups"]["elements"])["preferredUsername"] ==
assert hd(res["data"]["searchGroups"]["elements"])["preferredUsername"] ==
group.preferred_username
res =
AbsintheHelpers.graphql_query(conn,
query: @search_groups_query,
variables: %{location: geohash_2}
)
assert res["errors"] == nil
assert res["data"]["searchGroups"]["total"] == 0
end
end
end

View File

@@ -188,7 +188,7 @@ defmodule Mobilizon.ActorsTest do
with {:ok, %Actor{id: actor2_id}} <-
ActivityPub.get_or_fetch_actor_by_url(@remote_account_url) do
%Page{total: 2, elements: actors} =
Actors.build_actors_by_username_or_name_page("tcit", [:Person])
Actors.build_actors_by_username_or_name_page(%{term: "tcit"}, [:Person])
actors_ids = actors |> Enum.map(& &1.id)
@@ -199,7 +199,7 @@ defmodule Mobilizon.ActorsTest do
test "test build_actors_by_username_or_name_page/4 returns actors with similar names" do
%{total: 0, elements: actors} =
Actors.build_actors_by_username_or_name_page("ohno", [:Person])
Actors.build_actors_by_username_or_name_page(%{term: "ohno"}, [:Person])
assert actors == []
end