diff --git a/js/src/components/Event/FullAddressAutoComplete.vue b/js/src/components/Event/FullAddressAutoComplete.vue index 93115b4dd..bfec8033f 100644 --- a/js/src/components/Event/FullAddressAutoComplete.vue +++ b/js/src/components/Event/FullAddressAutoComplete.vue @@ -2,14 +2,14 @@
-
+
{ return new Promise((resolve, reject) => { if (!("geolocation" in navigator)) { diff --git a/js/src/graphql/actor.ts b/js/src/graphql/actor.ts index 5b6afa21a..0d9df8208 100644 --- a/js/src/graphql/actor.ts +++ b/js/src/graphql/actor.ts @@ -465,6 +465,19 @@ export const FETCH_GROUP = gql` summary preferredUsername suspended + visibility + physicalAddress { + description + street + locality + postalCode + region + country + geom + type + id + originId + } avatar { url } @@ -588,8 +601,18 @@ export const UPDATE_GROUP = gql` $summary: String $avatar: PictureInput $banner: PictureInput + $visibility: GroupVisibility + $physicalAddress: AddressInput ) { - createGroup(id: $id, name: $name, summary: $summary, banner: $banner, avatar: $avatar) { + updateGroup( + id: $id + name: $name + summary: $summary + banner: $banner + avatar: $avatar + visibility: $visibility + physicalAddress: $physicalAddress + ) { id preferredUsername name diff --git a/js/src/graphql/search.ts b/js/src/graphql/search.ts index 6f8b516b0..9274847f5 100644 --- a/js/src/graphql/search.ts +++ b/js/src/graphql/search.ts @@ -36,8 +36,8 @@ export const SEARCH_EVENTS = gql` `; export const SEARCH_GROUPS = gql` - query SearchGroups($searchText: String!) { - searchGroups(search: $searchText) { + query SearchGroups($term: String, $location: String, $radius: Float) { + searchGroups(term: $term, location: $location, radius: $radius) { total elements { avatar { @@ -54,7 +54,7 @@ export const SEARCH_GROUPS = gql` export const SEARCH_PERSONS = gql` query SearchPersons($searchText: String!, $page: Int, $limit: Int) { - searchPersons(search: $searchText, page: $page, limit: $limit) { + searchPersons(term: $searchText, page: $page, limit: $limit) { total elements { id diff --git a/js/src/types/actor/group.model.ts b/js/src/types/actor/group.model.ts index 3194396e0..759d8191c 100644 --- a/js/src/types/actor/group.model.ts +++ b/js/src/types/actor/group.model.ts @@ -6,6 +6,7 @@ import { IEvent } from "../event.model"; import { IDiscussion } from "../discussions"; import { IPerson } from "./person.model"; import { IPost } from "../post.model"; +import { IAddress, Address } from "../address.model"; export enum MemberRole { NOT_APPROVED = "NOT_APPROVED", @@ -23,6 +24,7 @@ export interface IGroup extends IActor { todoLists: Paginate; discussions: Paginate; organizedEvents: Paginate; + physicalAddress: IAddress; } export interface IMember { @@ -52,6 +54,7 @@ export class Group extends Actor implements IGroup { this.patch(hash); } + physicalAddress: IAddress = new Address(); patch(hash: any) { Object.assign(this, hash); diff --git a/js/src/views/Group/GroupMembers.vue b/js/src/views/Group/GroupMembers.vue index bc6e14d50..ea6191822 100644 --- a/js/src/views/Group/GroupMembers.vue +++ b/js/src/views/Group/GroupMembers.vue @@ -143,7 +143,6 @@ -
{{ group.members }}
diff --git a/js/src/views/Group/GroupSettings.vue b/js/src/views/Group/GroupSettings.vue index 7238dd6a0..9eb28fb13 100644 --- a/js/src/views/Group/GroupSettings.vue +++ b/js/src/views/Group/GroupSettings.vue @@ -39,6 +39,58 @@ +

{{ $t("Group visibility") }}

+
+ + {{ $t("Visible everywhere on the web") }}
+ {{ + $t( + "The group will be publicly listed in search results and may be suggested in the explore section. Only public informations will be shown on it's page." + ) + }} +
+
+
+ {{ $t("Only accessible through link") }}
+ {{ + $t("You'll need to transmit the group URL so people may access the group's profile.") + }} +
+

+ {{ group.url }} + + + +

+
+ + + {{ $t("Update group") }} @@ -50,8 +102,10 @@ import { Component, Vue } from "vue-property-decorator"; import RouteName from "../../router/name"; import { FETCH_GROUP, UPDATE_GROUP } from "../../graphql/actor"; import { IGroup, usernameWithDomain } from "../../types/actor"; +import { Address, IAddress } from "../../types/address.model"; import { IMember, Group } from "../../types/actor/group.model"; import { Paginate } from "../../types/paginate"; +import FullAddressAutoComplete from "@/components/Event/FullAddressAutoComplete.vue"; @Component({ apollo: { @@ -67,6 +121,9 @@ import { Paginate } from "../../types/paginate"; }, }, }, + components: { + FullAddressAutoComplete, + }, }) export default class GroupSettings extends Vue { group: IGroup = new Group(); @@ -79,13 +136,41 @@ export default class GroupSettings extends Vue { usernameWithDomain = usernameWithDomain; + GroupVisibility = { + PUBLIC: "PUBLIC", + UNLISTED: "UNLISTED", + }; + + showCopiedTooltip = false; + async updateGroup() { + const variables = { ...this.group }; + // eslint-disable-next-line + // @ts-ignore + delete variables.__typename; + // eslint-disable-next-line + // @ts-ignore + delete variables.physicalAddress.__typename; await this.$apollo.mutate<{ updateGroup: IGroup }>({ mutation: UPDATE_GROUP, - variables: { - ...this.group, - }, + variables, }); } + + async copyURL() { + await window.navigator.clipboard.writeText(this.group.url); + this.showCopiedTooltip = true; + setTimeout(() => { + this.showCopiedTooltip = false; + }, 2000); + } + + get canShowCopyButton(): boolean { + return window.isSecureContext; + } + + get currentAddress(): IAddress { + return new Address(this.group.physicalAddress); + } } diff --git a/js/src/views/Search.vue b/js/src/views/Search.vue index 3028941aa..873100e4c 100644 --- a/js/src/views/Search.vue +++ b/js/src/views/Search.vue @@ -177,11 +177,13 @@ const tabsName: { events: number; groups: number } = { query: SEARCH_GROUPS, variables() { return { - searchText: this.search, + term: this.search, + location: this.geohash, + radius: this.radius, }; }, skip() { - return this.search == null || this.search == ""; + return !this.search && !this.geohash; }, }, }, @@ -264,7 +266,7 @@ export default class Search extends Vue { radiusOptions: (number | null)[] = [1, 5, 10, 25, 50, 100, 150, null]; - radius: number | null = null; + radius: number = 50; submit() { this.$apollo.queries.searchEvents.refetch(); diff --git a/lib/graphql/api/search.ex b/lib/graphql/api/search.ex index 6087b3b14..dfc579006 100644 --- a/lib/graphql/api/search.ex +++ b/lib/graphql/api/search.ex @@ -15,20 +15,17 @@ defmodule Mobilizon.GraphQL.API.Search do @doc """ Searches actors. """ - @spec search_actors(String.t(), integer | nil, integer | nil, ActorType.t()) :: + @spec search_actors(map(), integer | nil, integer | nil, ActorType.t()) :: {:ok, Page.t()} | {:error, String.t()} - def search_actors(search, page \\ 1, limit \\ 10, result_type) do - search = String.trim(search) + def search_actors(%{term: term} = args, page \\ 1, limit \\ 10, result_type) do + term = String.trim(term) cond do - search == "" -> - {:error, "Search can't be empty"} - # Some URLs could be domain.tld/@username, so keep this condition above # the `is_handle` function - is_url(search) -> + is_url(term) -> # skip, if it's not an actor - case process_from_url(search) do + case process_from_url(term) do %Page{total: _total, elements: _elements} = page -> {:ok, page} @@ -36,11 +33,17 @@ defmodule Mobilizon.GraphQL.API.Search do {:ok, %{total: 0, elements: []}} end - is_handle(search) -> - {:ok, process_from_username(search)} + is_handle(term) -> + {:ok, process_from_username(term)} true -> - page = Actors.build_actors_by_username_or_name_page(search, [result_type], page, limit) + page = + Actors.build_actors_by_username_or_name_page( + Map.put(args, :term, term), + [result_type], + page, + limit + ) {:ok, page} end diff --git a/lib/graphql/resolvers/search.ex b/lib/graphql/resolvers/search.ex index 2e2d692c8..ad49e9290 100644 --- a/lib/graphql/resolvers/search.ex +++ b/lib/graphql/resolvers/search.ex @@ -8,15 +8,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Search do @doc """ Search persons """ - def search_persons(_parent, %{search: search, page: page, limit: limit}, _resolution) do - Search.search_actors(search, page, limit, :Person) + def search_persons(_parent, %{page: page, limit: limit} = args, _resolution) do + Search.search_actors(args, page, limit, :Person) end @doc """ Search groups """ - def search_groups(_parent, %{search: search, page: page, limit: limit}, _resolution) do - Search.search_actors(search, page, limit, :Group) + def search_groups(_parent, %{page: page, limit: limit} = args, _resolution) do + Search.search_actors(args, page, limit, :Group) end @doc """ diff --git a/lib/graphql/schema/actors/group.ex b/lib/graphql/schema/actors/group.ex index 18f24af4d..d9685d40c 100644 --- a/lib/graphql/schema/actors/group.ex +++ b/lib/graphql/schema/actors/group.ex @@ -5,6 +5,9 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do use Absinthe.Schema.Notation + import Absinthe.Resolution.Helpers, only: [dataloader: 1] + + alias Mobilizon.Addresses alias Mobilizon.GraphQL.Resolvers.{Discussion, Group, Member, Post, Resource, Todos} alias Mobilizon.GraphQL.Schema @@ -29,11 +32,20 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do description: "Whether the actors manually approves followers" ) + field(:visibility, :group_visibility, + description: "Whether the group can be found and/or promoted" + ) + field(:suspended, :boolean, description: "If the actor is suspended") field(:avatar, :picture, description: "The actor's avatar picture") field(:banner, :picture, description: "The actor's banner picture") + field(:physical_address, :address, + resolve: dataloader(Addresses), + description: "The type of the event's address" + ) + # These one should have a privacy setting field(:following, list_of(:follower), description: "List of followings") field(:followers, list_of(:follower), description: "List of followers") @@ -155,6 +167,8 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do "The banner for the group, either as an object or directly the ID of an existing Picture" ) + arg(:physical_address, :address_input) + resolve(&Group.create_group/3) end @@ -165,6 +179,8 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do arg(:name, :string, description: "The displayed name for the group") arg(:summary, :string, description: "The summary for the group", default_value: "") + arg(:visibility, :group_visibility, description: "The visibility for the group") + arg(:avatar, :picture_input, description: "The avatar for the group, either as an object or directly the ID of an existing Picture" @@ -175,6 +191,8 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do "The banner for the group, either as an object or directly the ID of an existing Picture" ) + arg(:physical_address, :address_input) + resolve(&Group.update_group/3) end diff --git a/lib/graphql/schema/search.ex b/lib/graphql/schema/search.ex index 02859d686..2493690fc 100644 --- a/lib/graphql/schema/search.ex +++ b/lib/graphql/schema/search.ex @@ -27,7 +27,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do object :search_queries do @desc "Search persons" field :search_persons, :persons do - arg(:search, non_null(:string)) + arg(:term, :string, default_value: "") arg(:page, :integer, default_value: 1) arg(:limit, :integer, default_value: 10) @@ -36,7 +36,9 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do @desc "Search groups" field :search_groups, :groups do - arg(:search, non_null(:string)) + arg(:term, :string, default_value: "") + arg(:location, :string, description: "A geohash for coordinates") + arg(:radius, :float, default_value: 50) arg(:page, :integer, default_value: 1) arg(:limit, :integer, default_value: 10) diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index 57f866095..2ed66532c 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -7,8 +7,9 @@ defmodule Mobilizon.Actors.Actor do import Ecto.Changeset - alias Mobilizon.{Actors, Config, Crypto, Mention, Share} + alias Mobilizon.{Actors, Addresses, Config, Crypto, Mention, Share} alias Mobilizon.Actors.{ActorOpenness, ActorType, ActorVisibility, Follower, Member} + alias Mobilizon.Addresses.Address alias Mobilizon.Discussions.Comment alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Media.File @@ -55,7 +56,8 @@ defmodule Mobilizon.Actors.Actor do shares: [Share.t()], owner_shares: [Share.t()], memberships: [t], - last_refreshed_at: DateTime.t() + last_refreshed_at: DateTime.t(), + physical_address: Address.t() } @required_attrs [:preferred_username, :keys, :suspended, :url] @@ -76,12 +78,13 @@ defmodule Mobilizon.Actors.Actor do :manually_approves_followers, :last_refreshed_at, :user_id, + :physical_address_id, :visibility ] @attrs @required_attrs ++ @optional_attrs @update_required_attrs @required_attrs -- [:url] - @update_optional_attrs [:name, :summary, :manually_approves_followers, :user_id] + @update_optional_attrs [:name, :summary, :manually_approves_followers, :user_id, :visibility] @update_attrs @update_required_attrs ++ @update_optional_attrs @registration_required_attrs [:preferred_username, :keys, :suspended, :url, :type] @@ -156,6 +159,7 @@ defmodule Mobilizon.Actors.Actor do embeds_one(:avatar, File, on_replace: :update) embeds_one(:banner, File, on_replace: :update) belongs_to(:user, User) + belongs_to(:physical_address, Address, on_replace: :nilify) has_many(:followers, Follower, foreign_key: :target_actor_id) has_many(:followings, Follower, foreign_key: :actor_id) has_many(:organized_events, Event, foreign_key: :organizer_actor_id) @@ -228,7 +232,7 @@ defmodule Mobilizon.Actors.Actor do actor |> cast(attrs, @attrs) |> build_urls() - |> common_changeset() + |> common_changeset(attrs) |> unique_username_validator() |> validate_required(@required_attrs) end @@ -238,7 +242,7 @@ defmodule Mobilizon.Actors.Actor do def update_changeset(%__MODULE__{} = actor, attrs) do actor |> cast(attrs, @update_attrs) - |> common_changeset() + |> common_changeset(attrs) |> validate_required(@update_required_attrs) end @@ -263,7 +267,7 @@ defmodule Mobilizon.Actors.Actor do actor |> cast(attrs, @registration_attrs) |> build_urls() - |> common_changeset() + |> common_changeset(attrs) |> unique_username_validator() |> validate_required(@registration_required_attrs) end @@ -277,7 +281,7 @@ defmodule Mobilizon.Actors.Actor do %__MODULE__{} |> cast(attrs, @remote_actor_creation_attrs) |> validate_required(@remote_actor_creation_required_attrs) - |> common_changeset() + |> common_changeset(attrs) |> unique_username_validator() |> validate_length(:summary, max: 5000) |> validate_length(:preferred_username, max: 100) @@ -287,11 +291,12 @@ defmodule Mobilizon.Actors.Actor do changeset end - @spec common_changeset(Ecto.Changeset.t()) :: Ecto.Changeset.t() - defp common_changeset(%Ecto.Changeset{} = changeset) do + @spec common_changeset(Ecto.Changeset.t(), map()) :: Ecto.Changeset.t() + defp common_changeset(%Ecto.Changeset{} = changeset, attrs) do changeset |> cast_embed(:avatar) |> cast_embed(:banner) + |> put_address(attrs) |> unique_constraint(:url, name: :actors_url_index) |> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index) |> validate_format(:preferred_username, ~r/[a-z0-9_]+/) @@ -306,7 +311,7 @@ defmodule Mobilizon.Actors.Actor do actor |> cast(params, @group_creation_attrs) |> build_urls(:Group) - |> common_changeset() + |> common_changeset(params) |> put_change(:domain, nil) |> put_change(:keys, Crypto.generate_rsa_2048_private_key()) |> put_change(:type, :Group) @@ -412,4 +417,36 @@ defmodule Mobilizon.Actors.Actor do |> Ecto.Changeset.cast(data, @attrs) |> build_urls() end + + # In case the provided addresses is an existing one + @spec put_address(Ecto.Changeset.t(), map) :: Ecto.Changeset.t() + defp put_address(%Ecto.Changeset{} = changeset, %{ + physical_address: %{id: id} = _physical_address + }) + when not is_nil(id) do + case Addresses.get_address(id) do + %Address{} = address -> + put_assoc(changeset, :physical_address, address) + + _ -> + cast_assoc(changeset, :physical_address) + end + end + + # In case it's a new address but the origin_id is an existing one + defp put_address(%Ecto.Changeset{} = changeset, %{physical_address: %{origin_id: origin_id}}) + when not is_nil(origin_id) do + case Addresses.get_address_by_origin_id(origin_id) do + %Address{} = address -> + put_assoc(changeset, :physical_address, address) + + _ -> + cast_assoc(changeset, :physical_address) + end + end + + # In case it's a new address without any origin_id (manual) + defp put_address(%Ecto.Changeset{} = changeset, _attrs) do + cast_assoc(changeset, :physical_address) + end end diff --git a/lib/mobilizon/actors/actors.ex b/lib/mobilizon/actors/actors.ex index de3e5ab42..5af4dabb0 100644 --- a/lib/mobilizon/actors/actors.ex +++ b/lib/mobilizon/actors/actors.ex @@ -5,10 +5,13 @@ defmodule Mobilizon.Actors do import Ecto.Query import EctoEnum + import Geo.PostGIS, only: [st_dwithin_in_meters: 3] + import Mobilizon.Service.Guards alias Ecto.Multi alias Mobilizon.Actors.{Actor, Bot, Follower, Member} + alias Mobilizon.Addresses.Address alias Mobilizon.{Crypto, Events} alias Mobilizon.Media.File alias Mobilizon.Service.Workers @@ -235,6 +238,7 @@ defmodule Mobilizon.Actors do @spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()} def update_actor(%Actor{} = actor, attrs) do actor + |> Repo.preload([:physical_address]) |> Actor.update_changeset(attrs) |> delete_files_if_media_changed() |> Repo.update() @@ -422,14 +426,20 @@ defmodule Mobilizon.Actors do Builds a page struct for actors by their name or displayed name. """ @spec build_actors_by_username_or_name_page( - String.t(), + map(), [ActorType.t()], integer | nil, integer | nil ) :: Page.t() - def build_actors_by_username_or_name_page(username, types, page \\ nil, limit \\ nil) do - username - |> actor_by_username_or_name_query() + def build_actors_by_username_or_name_page( + %{term: term} = args, + types, + page \\ nil, + limit \\ nil + ) do + Actor + |> actor_by_username_or_name_query(term) + |> actors_for_location(args) |> filter_by_types(types) |> Page.build_page(page, limit) end @@ -1129,29 +1139,54 @@ defmodule Mobilizon.Actors do ) end - @spec actor_by_username_or_name_query(String.t()) :: Ecto.Query.t() - defp actor_by_username_or_name_query(username) do - from( - a in Actor, - where: - fragment( - "f_unaccent(?) %> f_unaccent(?) or f_unaccent(coalesce(?, '')) %> f_unaccent(?)", - a.preferred_username, - ^username, - a.name, - ^username - ), - order_by: - fragment( - "word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc", - a.preferred_username, - ^username, - a.name, - ^username - ) + @spec actor_by_username_or_name_query(Ecto.Query.t(), String.t()) :: Ecto.Query.t() + defp actor_by_username_or_name_query(query, ""), do: query + + defp actor_by_username_or_name_query(query, username) do + query + |> where( + [a], + fragment( + "f_unaccent(?) %> f_unaccent(?) or f_unaccent(coalesce(?, '')) %> f_unaccent(?)", + a.preferred_username, + ^username, + a.name, + ^username + ) + ) + |> order_by( + [a], + fragment( + "word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc", + a.preferred_username, + ^username, + a.name, + ^username + ) ) end + @spec actors_for_location(Ecto.Query.t(), map()) :: Ecto.Query.t() + defp actors_for_location(query, %{radius: radius}) when is_nil(radius), + do: query + + defp actors_for_location(query, %{location: location, radius: radius}) + when is_valid_string?(location) and not is_nil(radius) do + with {lon, lat} <- Geohax.decode(location), + point <- Geo.WKT.decode!("SRID=4326;POINT(#{lon} #{lat})") do + query + |> join(:inner, [q], a in Address, on: a.id == q.physical_address_id, as: :address) + |> where( + [q], + st_dwithin_in_meters(^point, as(:address).geom, ^(radius * 1000)) + ) + else + _ -> query + end + end + + defp actors_for_location(query, _args), do: query + @spec person_query :: Ecto.Query.t() defp person_query do from(a in Actor, where: a.type == ^:Person) diff --git a/lib/mobilizon/addresses/addresses.ex b/lib/mobilizon/addresses/addresses.ex index 07bdca708..68137e000 100644 --- a/lib/mobilizon/addresses/addresses.ex +++ b/lib/mobilizon/addresses/addresses.ex @@ -29,6 +29,9 @@ defmodule Mobilizon.Addresses do @spec get_address_by_url(String.t()) :: Address.t() | nil def get_address_by_url(url), do: Repo.get_by(Address, url: url) + @spec get_address_by_origin_id(String.t()) :: Address.t() | nil + def get_address_by_origin_id(origin_id), do: Repo.get_by(Address, origin_id: origin_id) + @doc """ Creates an address. """ diff --git a/priv/repo/migrations/20200805124620_add_address_to_actors.exs b/priv/repo/migrations/20200805124620_add_address_to_actors.exs new file mode 100644 index 000000000..03c9496ea --- /dev/null +++ b/priv/repo/migrations/20200805124620_add_address_to_actors.exs @@ -0,0 +1,9 @@ +defmodule Mobilizon.Storage.Repo.Migrations.AddAddressToActors do + use Ecto.Migration + + def change do + alter table(:actors) do + add(:physical_address_id, references(:addresses, on_delete: :nothing)) + end + end +end diff --git a/test/graphql/api/search_test.exs b/test/graphql/api/search_test.exs index f4468746e..c9f095071 100644 --- a/test/graphql/api/search_test.exs +++ b/test/graphql/api/search_test.exs @@ -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 diff --git a/test/graphql/resolvers/search_test.exs b/test/graphql/resolvers/search_test.exs index cb5f634a9..8f820e8e6 100644 --- a/test/graphql/resolvers/search_test.exs +++ b/test/graphql/resolvers/search_test.exs @@ -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 diff --git a/test/mobilizon/actors/actors_test.exs b/test/mobilizon/actors/actors_test.exs index fd6ed2f7b..b1765701c 100644 --- a/test/mobilizon/actors/actors_test.exs +++ b/test/mobilizon/actors/actors_test.exs @@ -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