@@ -7,10 +7,10 @@ defmodule Mobilizon.GraphQL.API.Search do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Storage.Page
|
||||
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
||||
alias Mobilizon.Service.GlobalSearch
|
||||
alias Mobilizon.Storage.Page
|
||||
import Mobilizon.GraphQL.Resolvers.Event.Utils
|
||||
|
||||
require Logger
|
||||
@@ -40,23 +40,29 @@ defmodule Mobilizon.GraphQL.API.Search do
|
||||
{:ok, process_from_username(term)}
|
||||
|
||||
true ->
|
||||
page =
|
||||
Actors.search_actors(
|
||||
term,
|
||||
[
|
||||
actor_type: result_type,
|
||||
radius: Map.get(args, :radius),
|
||||
location: Map.get(args, :location),
|
||||
minimum_visibility: Map.get(args, :minimum_visibility, :public),
|
||||
current_actor_id: Map.get(args, :current_actor_id),
|
||||
exclude_my_groups: Map.get(args, :exclude_my_groups, false),
|
||||
exclude_stale_actors: true
|
||||
],
|
||||
page,
|
||||
limit
|
||||
)
|
||||
if is_global_search(args) do
|
||||
service = GlobalSearch.service()
|
||||
|
||||
{:ok, page}
|
||||
{:ok, service.search_groups(Keyword.new(args, fn {k, v} -> {k, v} end))}
|
||||
else
|
||||
page =
|
||||
Actors.search_actors(
|
||||
term,
|
||||
[
|
||||
actor_type: result_type,
|
||||
radius: Map.get(args, :radius),
|
||||
location: Map.get(args, :location),
|
||||
minimum_visibility: Map.get(args, :minimum_visibility, :public),
|
||||
current_actor_id: Map.get(args, :current_actor_id),
|
||||
exclude_my_groups: Map.get(args, :exclude_my_groups, false),
|
||||
exclude_stale_actors: true
|
||||
],
|
||||
page,
|
||||
limit
|
||||
)
|
||||
|
||||
{:ok, page}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -82,7 +88,13 @@ defmodule Mobilizon.GraphQL.API.Search do
|
||||
{:ok, %{total: 0, elements: []}}
|
||||
end
|
||||
else
|
||||
{:ok, Events.build_events_for_search(Map.put(args, :term, term), page, limit)}
|
||||
if is_global_search(args) do
|
||||
service = GlobalSearch.service()
|
||||
|
||||
{:ok, service.search_events(Keyword.new(args, fn {k, v} -> {k, v} end))}
|
||||
else
|
||||
{:ok, Events.build_events_for_search(Map.put(args, :term, term), page, limit)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -136,4 +148,18 @@ defmodule Mobilizon.GraphQL.API.Search do
|
||||
|
||||
@spec is_handle(String.t()) :: boolean
|
||||
defp is_handle(search), do: String.match?(search, ~r/@/)
|
||||
|
||||
defp is_global_search(%{search_target: :global}) do
|
||||
global_search_enabled?()
|
||||
end
|
||||
|
||||
defp is_global_search(_), do: global_search_enabled?() && global_search_default?()
|
||||
|
||||
defp global_search_enabled? do
|
||||
Application.get_env(:mobilizon, :search) |> get_in([:global]) |> get_in([:is_enabled])
|
||||
end
|
||||
|
||||
defp global_search_default? do
|
||||
Application.get_env(:mobilizon, :search) |> get_in([:global]) |> get_in([:is_default_search])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,7 +45,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Address do
|
||||
_context
|
||||
) do
|
||||
addresses =
|
||||
Geospatial.service().geocode(longitude, latitude, lang: locale, zoom: zoom)
|
||||
longitude
|
||||
|> Geospatial.service().geocode(latitude, lang: locale, zoom: zoom)
|
||||
|> Enum.map(fn address ->
|
||||
picture_info =
|
||||
Pictures.service().search(address.locality || address.region || address.country)
|
||||
|
||||
@@ -172,7 +172,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Config do
|
||||
get_in(Application.get_env(:web_push_encryption, :vapid_details), [:public_key])
|
||||
},
|
||||
export_formats: Config.instance_export_formats(),
|
||||
analytics: FrontEndAnalytics.config()
|
||||
analytics: FrontEndAnalytics.config(),
|
||||
search: %{
|
||||
global: %{
|
||||
is_enabled:
|
||||
Application.get_env(:mobilizon, :search) |> get_in([:global]) |> get_in([:is_enabled]),
|
||||
is_default:
|
||||
Application.get_env(:mobilizon, :search)
|
||||
|> get_in([:global])
|
||||
|> get_in([:is_default_search])
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,4 +67,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Followers do
|
||||
end
|
||||
|
||||
def update_follower(_, _, _), do: {:error, :unauthenticated}
|
||||
|
||||
def count_followers_for_group(%Actor{type: :Group} = group, _args, _resolution) do
|
||||
{:ok, Actors.count_followers_for_actor(group)}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -254,6 +254,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
|
||||
"You must be logged-in to remove a member"
|
||||
)}
|
||||
|
||||
def count_members_for_group(%Actor{type: :Group} = group, _args, _resolution) do
|
||||
{:ok, Actors.count_members_for_group(group)}
|
||||
end
|
||||
|
||||
# Rejected members can be invited again
|
||||
@spec check_member_not_existant_or_rejected(String.t() | integer, String.t() | integer()) ::
|
||||
boolean()
|
||||
|
||||
@@ -32,8 +32,8 @@ defmodule Mobilizon.GraphQL.Schema.ActorInterface do
|
||||
field(:banner, :media, description: "The actor's banner media")
|
||||
|
||||
# These one should have a privacy setting
|
||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||
field(:followers_count, :integer, description: "Number of followers for this actor")
|
||||
field(:following_count, :integer, description: "Number of actors following this actor")
|
||||
|
||||
field(:media_size, :integer, description: "The total size of the media from this actor")
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ defmodule Mobilizon.GraphQL.Schema.Actors.ApplicationType do
|
||||
field(:banner, :media, description: "The actor's banner media")
|
||||
|
||||
# These one should have a privacy setting
|
||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||
field(:followers_count, :integer, description: "Number of followers for this actor")
|
||||
field(:following_count, :integer, description: "Number of actors following this actor")
|
||||
|
||||
field(:media_size, :integer,
|
||||
resolve: &Media.actor_size/3,
|
||||
|
||||
@@ -29,7 +29,7 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||
Represents a group of actors
|
||||
"""
|
||||
object :group do
|
||||
interfaces([:actor, :interactable, :activity_object, :action_log_object])
|
||||
interfaces([:actor, :interactable, :activity_object, :action_log_object, :group_search_result])
|
||||
|
||||
field(:id, :id, description: "Internal ID for this group")
|
||||
field(:url, :string, description: "The ActivityPub actor's URL")
|
||||
@@ -59,8 +59,17 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||
)
|
||||
|
||||
# These one should have a privacy setting
|
||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||
field(:followers_count, :integer,
|
||||
description: "Number of followers for this actor",
|
||||
resolve: &Followers.count_followers_for_group/3
|
||||
)
|
||||
|
||||
field(:following_count, :integer, description: "Number of follows for this actor")
|
||||
|
||||
field(:members_count, :integer,
|
||||
description: "Number of members for this actor",
|
||||
resolve: &Member.count_members_for_group/3
|
||||
)
|
||||
|
||||
field(:media_size, :integer,
|
||||
resolve: &Media.actor_size/3,
|
||||
|
||||
@@ -43,9 +43,16 @@ defmodule Mobilizon.GraphQL.Schema.Actors.PersonType do
|
||||
field(:avatar, :media, description: "The actor's avatar media")
|
||||
field(:banner, :media, description: "The actor's banner media")
|
||||
|
||||
# These one should have a privacy setting
|
||||
field(:followersCount, :integer, description: "Number of followers for this actor")
|
||||
field(:followingCount, :integer, description: "Number of actors following this actor")
|
||||
# Persons have zero followers/followings
|
||||
field(:followers_count, :integer,
|
||||
description: "Number of followers for this actor",
|
||||
resolve: fn _, _, _ -> {:ok, 0} end
|
||||
)
|
||||
|
||||
field(:following_count, :integer,
|
||||
description: "Number of actors following this actor",
|
||||
resolve: fn _, _, _ -> {:ok, 0} end
|
||||
)
|
||||
|
||||
field(:media_size, :integer,
|
||||
resolve: &Media.actor_size/3,
|
||||
|
||||
@@ -79,6 +79,8 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
|
||||
field(:analytics, list_of(:analytics),
|
||||
description: "Configuration for diverse analytics services"
|
||||
)
|
||||
|
||||
field(:search, :search_settings, description: "The instance's search settings")
|
||||
end
|
||||
|
||||
@desc """
|
||||
@@ -354,6 +356,15 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
|
||||
field(:type, :analytics_configuration_type, description: "The analytics configuration type")
|
||||
end
|
||||
|
||||
object :search_settings do
|
||||
field(:global, :global_search_settings, description: "The instance's global search settings")
|
||||
end
|
||||
|
||||
object :global_search_settings do
|
||||
field(:is_enabled, :boolean, description: "Whether global search is enabled")
|
||||
field(:is_default, :boolean, description: "Whether global search is the default")
|
||||
end
|
||||
|
||||
@desc """
|
||||
Export formats configuration
|
||||
"""
|
||||
|
||||
@@ -17,7 +17,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
|
||||
|
||||
@desc "An event"
|
||||
object :event do
|
||||
interfaces([:action_log_object, :interactable, :activity_object])
|
||||
interfaces([:action_log_object, :interactable, :activity_object, :event_search_result])
|
||||
field(:id, :id, description: "Internal ID for this event")
|
||||
field(:uuid, :uuid, description: "The Event UUID")
|
||||
field(:url, :string, description: "The ActivityPub Event URL")
|
||||
|
||||
@@ -7,6 +7,97 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.GraphQL.Resolvers.Search
|
||||
alias Mobilizon.Service.GlobalSearch.{EventResult, GroupResult}
|
||||
|
||||
interface :event_search_result do
|
||||
field(:id, :id, description: "Internal ID for this event")
|
||||
field(:uuid, :uuid, description: "The Event UUID")
|
||||
field(:url, :string, description: "The ActivityPub Event URL")
|
||||
field(:title, :string, description: "The event's title")
|
||||
field(:begins_on, :datetime, description: "Datetime for when the event begins")
|
||||
field(:ends_on, :datetime, description: "Datetime for when the event ends")
|
||||
field(:status, :event_status, description: "Status of the event")
|
||||
field(:picture, :media, description: "The event's picture")
|
||||
field(:physical_address, :address, description: "The event's physical address")
|
||||
field(:attributed_to, :actor, description: "Who the event is attributed to (often a group)")
|
||||
field(:organizer_actor, :actor, description: "The event's organizer (as a person)")
|
||||
field(:tags, list_of(:tag), description: "The event's tags")
|
||||
field(:category, :event_category, description: "The event's category")
|
||||
field(:options, :event_options, description: "The event options")
|
||||
|
||||
resolve_type(fn
|
||||
%Event{}, _ ->
|
||||
:event
|
||||
|
||||
%EventResult{}, _ ->
|
||||
:event_result
|
||||
|
||||
_, _ ->
|
||||
nil
|
||||
end)
|
||||
end
|
||||
|
||||
@desc "Search event result"
|
||||
object :event_result do
|
||||
interfaces([:event_search_result])
|
||||
field(:id, :id, description: "Internal ID for this event")
|
||||
field(:uuid, :uuid, description: "The Event UUID")
|
||||
field(:url, :string, description: "The ActivityPub Event URL")
|
||||
field(:title, :string, description: "The event's title")
|
||||
field(:begins_on, :datetime, description: "Datetime for when the event begins")
|
||||
field(:ends_on, :datetime, description: "Datetime for when the event ends")
|
||||
field(:status, :event_status, description: "Status of the event")
|
||||
field(:picture, :media, description: "The event's picture")
|
||||
field(:physical_address, :address, description: "The event's physical address")
|
||||
field(:attributed_to, :actor, description: "Who the event is attributed to (often a group)")
|
||||
field(:organizer_actor, :actor, description: "The event's organizer (as a person)")
|
||||
field(:tags, list_of(:tag), description: "The event's tags")
|
||||
field(:category, :event_category, description: "The event's category")
|
||||
field(:options, :event_options, description: "The event options")
|
||||
end
|
||||
|
||||
interface :group_search_result do
|
||||
field(:id, :id, description: "Internal ID for this group")
|
||||
field(:url, :string, description: "The ActivityPub actor's URL")
|
||||
field(:type, :actor_type, description: "The type of Actor (Person, Group,…)")
|
||||
field(:name, :string, description: "The actor's displayed name")
|
||||
field(:domain, :string, description: "The actor's domain if (null if it's this instance)")
|
||||
field(:summary, :string, description: "The actor's summary")
|
||||
field(:preferred_username, :string, description: "The actor's preferred username")
|
||||
field(:avatar, :media, description: "The actor's avatar media")
|
||||
field(:banner, :media, description: "The actor's banner media")
|
||||
field(:followers_count, :integer, description: "Number of followers for this actor")
|
||||
field(:members_count, :integer, description: "Number of followers for this actor")
|
||||
field(:physical_address, :address, description: "The type of the event's address")
|
||||
|
||||
resolve_type(fn
|
||||
%Actor{type: :Group}, _ ->
|
||||
:group
|
||||
|
||||
%GroupResult{}, _ ->
|
||||
:group_result
|
||||
|
||||
_, _ ->
|
||||
nil
|
||||
end)
|
||||
end
|
||||
|
||||
@desc "Search group result"
|
||||
object :group_result do
|
||||
interfaces([:group_search_result])
|
||||
field(:id, :id, description: "Internal ID for this group")
|
||||
field(:url, :string, description: "The ActivityPub actor's URL")
|
||||
field(:type, :actor_type, description: "The type of Actor (Person, Group,…)")
|
||||
field(:name, :string, description: "The actor's displayed name")
|
||||
field(:domain, :string, description: "The actor's domain if (null if it's this instance)")
|
||||
field(:summary, :string, description: "The actor's summary")
|
||||
field(:preferred_username, :string, description: "The actor's preferred username")
|
||||
field(:avatar, :media, description: "The actor's avatar media")
|
||||
field(:banner, :media, description: "The actor's banner media")
|
||||
field(:followers_count, :integer, description: "Number of followers for this actor")
|
||||
field(:members_count, :integer, description: "Number of followers for this actor")
|
||||
field(:physical_address, :address, description: "The type of the event's address")
|
||||
end
|
||||
|
||||
@desc "Search persons result"
|
||||
object :persons do
|
||||
@@ -17,13 +108,13 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
|
||||
@desc "Search groups result"
|
||||
object :groups do
|
||||
field(:total, non_null(:integer), description: "Total elements")
|
||||
field(:elements, non_null(list_of(:group)), description: "Group elements")
|
||||
field(:elements, non_null(list_of(:group_search_result)), description: "Group elements")
|
||||
end
|
||||
|
||||
@desc "Search events result"
|
||||
object :events do
|
||||
field(:total, non_null(:integer), description: "Total elements")
|
||||
field(:elements, non_null(list_of(:event)), description: "Event elements")
|
||||
field(:elements, non_null(list_of(:event_search_result)), description: "Event elements")
|
||||
end
|
||||
|
||||
@desc """
|
||||
@@ -53,6 +144,14 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
|
||||
value(:online, description: "The event will only happen online. It has no physical address")
|
||||
end
|
||||
|
||||
enum :search_target do
|
||||
value(:internal,
|
||||
description: "Search on content from this instance and from the followed instances"
|
||||
)
|
||||
|
||||
value(:global, description: "Search using the global fediverse search")
|
||||
end
|
||||
|
||||
object :search_queries do
|
||||
@desc "Search persons"
|
||||
field :search_persons, :persons do
|
||||
@@ -81,6 +180,15 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
|
||||
description: "Radius around the location to search in"
|
||||
)
|
||||
|
||||
arg(:language_one_of, list_of(:string),
|
||||
description: "The list of languages this event can be in"
|
||||
)
|
||||
|
||||
arg(:search_target, :search_target,
|
||||
default_value: :internal,
|
||||
description: "The target of the search (internal or global)"
|
||||
)
|
||||
|
||||
arg(:page, :integer, default_value: 1, description: "Result page")
|
||||
arg(:limit, :integer, default_value: 10, description: "Results limit per page")
|
||||
|
||||
@@ -103,6 +211,15 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
|
||||
description: "The list of statuses this event can have"
|
||||
)
|
||||
|
||||
arg(:language_one_of, list_of(:string),
|
||||
description: "The list of languages this event can be in"
|
||||
)
|
||||
|
||||
arg(:search_target, :search_target,
|
||||
default_value: :internal,
|
||||
description: "The target of the search (internal or global)"
|
||||
)
|
||||
|
||||
arg(:radius, :float,
|
||||
default_value: 50,
|
||||
description: "Radius around the location to search in"
|
||||
|
||||
@@ -923,10 +923,10 @@ defmodule Mobilizon.Actors do
|
||||
Returns the number of members for a group
|
||||
"""
|
||||
@spec count_members_for_group(Actor.t()) :: integer()
|
||||
def count_members_for_group(%Actor{id: actor_id}) do
|
||||
def count_members_for_group(%Actor{id: actor_id}, roles \\ @member_roles) do
|
||||
actor_id
|
||||
|> members_for_group_query()
|
||||
# |> where([m], m.role in @member_roles)
|
||||
|> where([m], m.role in ^roles)
|
||||
|> Repo.aggregate(:count)
|
||||
end
|
||||
|
||||
|
||||
@@ -531,6 +531,7 @@ defmodule Mobilizon.Events do
|
||||
|> events_for_ends_on(args)
|
||||
|> events_for_category(args)
|
||||
|> events_for_categories(args)
|
||||
|> events_for_languages(args)
|
||||
|> events_for_statuses(args)
|
||||
|> events_for_tags(args)
|
||||
|> events_for_location(args)
|
||||
@@ -1323,13 +1324,22 @@ defmodule Mobilizon.Events do
|
||||
defp events_for_category(query, _args), do: query
|
||||
|
||||
@spec events_for_categories(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
|
||||
defp events_for_categories(query, %{category_one_of: category_one_of}) when length(category_one_of) > 0 do
|
||||
defp events_for_categories(query, %{category_one_of: category_one_of})
|
||||
when length(category_one_of) > 0 do
|
||||
where(query, [q], q.category in ^category_one_of)
|
||||
end
|
||||
|
||||
defp events_for_categories(query, _args), do: query
|
||||
|
||||
defp events_for_statuses(query, %{status_one_of: status_one_of}) when length(status_one_of) > 0 do
|
||||
defp events_for_languages(query, %{language_one_of: language_one_of})
|
||||
when length(language_one_of) > 0 do
|
||||
where(query, [q], q.language in ^language_one_of)
|
||||
end
|
||||
|
||||
defp events_for_languages(query, _args), do: query
|
||||
|
||||
defp events_for_statuses(query, %{status_one_of: status_one_of})
|
||||
when length(status_one_of) > 0 do
|
||||
where(query, [q], q.status in ^status_one_of)
|
||||
end
|
||||
|
||||
@@ -1622,6 +1632,7 @@ defmodule Mobilizon.Events do
|
||||
|
||||
def category_statistics do
|
||||
Event
|
||||
|> filter_local_or_from_followed_instances_events()
|
||||
|> group_by([e], e.category)
|
||||
|> select([e], {e.category, count(e.id)})
|
||||
|> Repo.all()
|
||||
|
||||
18
lib/service/global_search/event_result.ex
Normal file
18
lib/service/global_search/event_result.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule Mobilizon.Service.GlobalSearch.EventResult do
|
||||
@moduledoc """
|
||||
The structure holding search result information about an event
|
||||
"""
|
||||
defstruct [
|
||||
:id,
|
||||
:uuid,
|
||||
:url,
|
||||
:title,
|
||||
:begins_on,
|
||||
:ends_on,
|
||||
:picture,
|
||||
:category,
|
||||
:tags,
|
||||
:organizer_actor,
|
||||
:participants
|
||||
]
|
||||
end
|
||||
17
lib/service/global_search/global_search.ex
Normal file
17
lib/service/global_search/global_search.ex
Normal file
@@ -0,0 +1,17 @@
|
||||
defmodule Mobilizon.Service.GlobalSearch do
|
||||
@moduledoc """
|
||||
Module to load the service adapter defined inside the configuration.
|
||||
|
||||
See `Mobilizon.Service.GlobalSearch.Provider`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns the appropriate service adapter.
|
||||
|
||||
According to the config behind
|
||||
`config :mobilizon, Mobilizon.Service.GlobalSearch,
|
||||
service: Mobilizon.Service.GlobalSearch.Module`
|
||||
"""
|
||||
@spec service :: module
|
||||
def service, do: get_in(Application.get_env(:mobilizon, __MODULE__), [:service])
|
||||
end
|
||||
19
lib/service/global_search/group_result.ex
Normal file
19
lib/service/global_search/group_result.ex
Normal file
@@ -0,0 +1,19 @@
|
||||
defmodule Mobilizon.Service.GlobalSearch.GroupResult do
|
||||
@moduledoc """
|
||||
The structure holding search result information about a group
|
||||
"""
|
||||
defstruct [
|
||||
:id,
|
||||
:url,
|
||||
:name,
|
||||
:preferred_username,
|
||||
:domain,
|
||||
:avatar,
|
||||
:summary,
|
||||
:url,
|
||||
:members_count,
|
||||
:follower_count,
|
||||
:type,
|
||||
:physical_address
|
||||
]
|
||||
end
|
||||
40
lib/service/global_search/provider.ex
Normal file
40
lib/service/global_search/provider.ex
Normal file
@@ -0,0 +1,40 @@
|
||||
defmodule Mobilizon.Service.GlobalSearch.Provider do
|
||||
@moduledoc """
|
||||
Provider Behaviour for Global Search.
|
||||
|
||||
## Supported backends
|
||||
|
||||
* `Mobilizon.Service.GlobalSearch.Mobilizon` [🔗](https://framagit.org/framasoft/joinmobilizon/search-index/)
|
||||
|
||||
## Shared options
|
||||
|
||||
* `:lang` Lang in which to prefer results. Used as a request parameter or
|
||||
through an `Accept-Language` HTTP header. Defaults to `"en"`.
|
||||
* `:country_code` An ISO 3166 country code. String or `nil`
|
||||
* `:limit` Maximum limit for the number of results returned by the backend.
|
||||
Defaults to `10`
|
||||
* `:api_key` Allows to override the API key (if the backend requires one) set
|
||||
inside the configuration.
|
||||
* `:endpoint` Allows to override the endpoint set inside the configuration.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.GlobalSearch.{EventResult, GroupResult}
|
||||
|
||||
@doc """
|
||||
Get global search results for a search string
|
||||
|
||||
## Examples
|
||||
|
||||
iex> search_events(search: "London")
|
||||
[%EventResult{id: "provider-534", origin_url: "https://somewhere.else", title: "MyEvent", begins_on: "2022-08-25T08:13:47+0200", ends_on: "2022-08-25T10:13:47+0200", category: "FILM_MEDIA", tags: ["something", "what"], participants: 5}]
|
||||
"""
|
||||
@callback search_events(search_options :: keyword) ::
|
||||
Page.t(EventResult.t())
|
||||
@callback search_groups(search_options :: keyword) ::
|
||||
Page.t(GroupResult.t())
|
||||
|
||||
@spec endpoint(atom()) :: String.t()
|
||||
def endpoint(provider) do
|
||||
Application.get_env(:mobilizon, provider) |> get_in([:endpoint])
|
||||
end
|
||||
end
|
||||
225
lib/service/global_search/search_mobilizon.ex
Normal file
225
lib/service/global_search/search_mobilizon.ex
Normal file
@@ -0,0 +1,225 @@
|
||||
defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
|
||||
@moduledoc """
|
||||
[Search Mobilizon](https://search.joinmobilizon.org) backend.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Events.Tag
|
||||
alias Mobilizon.Service.GlobalSearch.{EventResult, GroupResult, Provider}
|
||||
alias Mobilizon.Service.HTTP.GenericJSONClient
|
||||
alias Mobilizon.Storage.Page
|
||||
require Logger
|
||||
import Plug.Conn.Query, only: [encode: 1]
|
||||
|
||||
@search_events_api "/api/v1/search/events"
|
||||
@search_groups_api "/api/v1/search/groups"
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Mobilizon Search implementation for `c:Mobilizon.Service.GlobalSearch.Provider.search_events/3`.
|
||||
"""
|
||||
@spec search_events(keyword()) :: Page.t(EventResult.t())
|
||||
def search_events(options \\ []) do
|
||||
Logger.debug("Search events options, #{inspect(Keyword.keys(options))}")
|
||||
|
||||
options =
|
||||
options
|
||||
|> Keyword.merge(
|
||||
term: options[:search],
|
||||
startDateMin: to_date(options[:begins_on]),
|
||||
startDateMax: to_date(options[:ends_on]),
|
||||
categoryOneOf: options[:category_one_of],
|
||||
languageOneOf: options[:language_one_of],
|
||||
statusOneOf:
|
||||
Enum.map(options[:status_one_of] || [], fn status ->
|
||||
status |> Atom.to_string() |> String.upcase()
|
||||
end),
|
||||
distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil),
|
||||
count: options[:limit],
|
||||
start: (options[:page] - 1) * options[:limit],
|
||||
latlon: to_lat_lon(options[:location])
|
||||
)
|
||||
|> Keyword.take([
|
||||
:search,
|
||||
:startDateMin,
|
||||
:startDateMax,
|
||||
:boostLanguages,
|
||||
:categoryOneOf,
|
||||
:languageOneOf,
|
||||
:latlon,
|
||||
:distance,
|
||||
:sort,
|
||||
:statusOneOf,
|
||||
:start,
|
||||
:count
|
||||
])
|
||||
|> Keyword.reject(fn {_key, val} -> is_nil(val) end)
|
||||
|
||||
events_url = "#{search_endpoint()}#{@search_events_api}?#{encode(options)}"
|
||||
Logger.debug("Calling global search engine url #{events_url}")
|
||||
|
||||
client = GenericJSONClient.client()
|
||||
|
||||
case GenericJSONClient.get(client, events_url) do
|
||||
{:ok, %{status: 200, body: body}} ->
|
||||
%Page{total: body["total"], elements: Enum.map(body["data"], &build_event/1)}
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Mobilizon Search implementation for `c:Mobilizon.Service.GlobalSearch.Provider.search_groups/3`.
|
||||
"""
|
||||
@spec search_groups(keyword()) :: Page.t(GroupResult.t())
|
||||
def search_groups(options \\ []) do
|
||||
options =
|
||||
options
|
||||
|> Keyword.merge(
|
||||
term: options[:search],
|
||||
languageOneOf: options[:language_one_of],
|
||||
distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil),
|
||||
count: options[:limit],
|
||||
start: (options[:page] - 1) * options[:limit],
|
||||
latlon: to_lat_lon(options[:location])
|
||||
)
|
||||
|> Keyword.take([
|
||||
:search,
|
||||
:boostLanguages,
|
||||
:latlon,
|
||||
:distance,
|
||||
:sort,
|
||||
:start,
|
||||
:count
|
||||
])
|
||||
|> Keyword.reject(fn {_key, val} -> is_nil(val) end)
|
||||
|
||||
groups_url = "#{search_endpoint()}#{@search_groups_api}?#{encode(options)}"
|
||||
Logger.debug("Calling global search engine url #{groups_url}")
|
||||
|
||||
client = GenericJSONClient.client()
|
||||
|
||||
case GenericJSONClient.get(client, groups_url) do
|
||||
{:ok, %{status: 200, body: body}} ->
|
||||
%Page{total: body["total"], elements: Enum.map(body["data"], &build_group/1)}
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp build_event(data) do
|
||||
picture =
|
||||
if data["banner"] do
|
||||
%{url: data["banner"], id: data["banner"]}
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
organizer_actor_avatar =
|
||||
if data["creator"]["avatar"] do
|
||||
%{url: data["creator"]["avatar"], id: data["creator"]["avatar"]}
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
%EventResult{
|
||||
id: data["id"],
|
||||
uuid: data["uuid"],
|
||||
title: data["name"],
|
||||
begins_on: parse_date(data["startTime"]),
|
||||
ends_on: parse_date(data["endTime"]),
|
||||
url: data["url"],
|
||||
picture: picture,
|
||||
category: String.to_existing_atom(String.downcase(data["category"])),
|
||||
organizer_actor: %Actor{
|
||||
id: data["creator"]["id"],
|
||||
name: data["creator"]["displayName"],
|
||||
preferred_username: data["creator"]["name"],
|
||||
avatar: organizer_actor_avatar
|
||||
},
|
||||
tags:
|
||||
Enum.map(data["tags"], fn tag ->
|
||||
tag = String.trim_leading(tag, "#")
|
||||
%Tag{id: tag, slug: tag, title: tag}
|
||||
end)
|
||||
}
|
||||
end
|
||||
|
||||
defp build_group(data) do
|
||||
avatar =
|
||||
if data["avatar"] do
|
||||
%{url: data["avatar"], id: data["avatar"]}
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
address =
|
||||
if data["location"] do
|
||||
%Address{
|
||||
id: data["location"]["id"],
|
||||
country: data["location"]["address"]["addressCountry"],
|
||||
locality: data["location"]["address"]["addressLocality"],
|
||||
region: data["location"]["address"]["addressRegion"],
|
||||
postal_code: data["location"]["address"]["postalCode"],
|
||||
street: data["location"]["address"]["streetAddress"],
|
||||
url: data["location"]["id"],
|
||||
description: data["location"]["name"],
|
||||
geom: %Geo.Point{
|
||||
coordinates:
|
||||
{data["location"]["location"]["lon"], data["location"]["location"]["lat"]},
|
||||
srid: 4326
|
||||
}
|
||||
}
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
%GroupResult{
|
||||
id: data["id"],
|
||||
name: data["displayName"],
|
||||
preferred_username: data["name"],
|
||||
domain: data["host"],
|
||||
avatar: avatar,
|
||||
summary: data["description"],
|
||||
url: data["url"],
|
||||
members_count: data["memberCount"],
|
||||
type: :Group,
|
||||
physical_address: address
|
||||
}
|
||||
end
|
||||
|
||||
defp search_endpoint do
|
||||
Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) ||
|
||||
"https://search.joinmobilizon.org"
|
||||
end
|
||||
|
||||
defp parse_date(nil), do: nil
|
||||
|
||||
defp parse_date(date_string) do
|
||||
case DateTime.from_iso8601(date_string) do
|
||||
{:ok, date, _} -> date
|
||||
{:error, _} -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp to_date(nil), do: nil
|
||||
defp to_date(date), do: DateTime.to_iso8601(date)
|
||||
|
||||
defp to_lat_lon(nil), do: nil
|
||||
|
||||
defp to_lat_lon(location) do
|
||||
case Geohax.decode(location) do
|
||||
{lon, lat} ->
|
||||
"#{lat}:#{lon}"
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,6 @@
|
||||
defmodule Mobilizon.Service.Pictures.Information do
|
||||
@moduledoc """
|
||||
The structure holding information about a picture
|
||||
"""
|
||||
defstruct [:url, :author, :source]
|
||||
end
|
||||
|
||||
@@ -3,8 +3,8 @@ defmodule Mobilizon.Service.Pictures.Unsplash do
|
||||
[Unsplash](https://unsplash.com) backend.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.Pictures.{Information, Provider}
|
||||
alias Mobilizon.Service.HTTP.GenericJSONClient
|
||||
alias Mobilizon.Service.Pictures.{Information, Provider}
|
||||
require Logger
|
||||
|
||||
@unsplash_api "/search/photos"
|
||||
@@ -24,12 +24,12 @@ defmodule Mobilizon.Service.Pictures.Unsplash do
|
||||
GenericJSONClient.client(headers: [{:Authorization, "Client-ID #{unsplash_access_key()}"}])
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <- GenericJSONClient.get(client, url),
|
||||
selectedPicture <- Enum.random(body["results"]) do
|
||||
selected_picture <- Enum.random(body["results"]) do
|
||||
%Information{
|
||||
url: selectedPicture["urls"]["small"],
|
||||
url: selected_picture["urls"]["small"],
|
||||
author: %{
|
||||
name: selectedPicture["user"]["name"],
|
||||
url: "#{selectedPicture["user"]["links"]["html"]}#{unsplash_utm_source()}"
|
||||
name: selected_picture["user"]["name"],
|
||||
url: "#{selected_picture["user"]["links"]["html"]}#{unsplash_utm_source()}"
|
||||
},
|
||||
source: %{
|
||||
name: @unsplash_name,
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="apple-touch-icon" href="/img/icons/apple-touch-icon-152x152.png" sizes="152x152">
|
||||
<link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color={theme_color()}>
|
||||
<meta name="theme-color" content={theme_color()}>
|
||||
<link rel="apple-touch-icon" href="/img/icons/apple-touch-icon-152x152.png" sizes="152x152" />
|
||||
<link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color={theme_color()} />
|
||||
<meta name="theme-color" content={theme_color()} />
|
||||
<%= tags(assigns) || assigns.tags %>
|
||||
<%= Vite.inlined_phx_manifest() %>
|
||||
<%= Vite.vite_client() %>
|
||||
|
||||
Reference in New Issue
Block a user