mobilizon/lib/graphql/api/search.ex

109 lines
2.9 KiB
Elixir
Raw Normal View History

2020-01-26 20:11:16 +00:00
defmodule Mobilizon.GraphQL.API.Search do
@moduledoc """
2019-09-08 22:52:49 +00:00
API for search.
"""
2019-09-08 22:52:49 +00:00
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, ActorType}
alias Mobilizon.Events
alias Mobilizon.Events.Event
2019-09-08 22:52:49 +00:00
alias Mobilizon.Storage.Page
2020-01-22 01:14:42 +00:00
alias Mobilizon.Federation.ActivityPub
require Logger
@doc """
2019-09-08 22:52:49 +00:00
Searches actors.
"""
@spec search_actors(map(), integer | nil, integer | nil, ActorType.t()) ::
2019-09-08 22:52:49 +00:00
{:ok, Page.t()} | {:error, String.t()}
def search_actors(%{term: term} = args, page \\ 1, limit \\ 10, result_type) do
term = String.trim(term)
cond do
2019-09-08 22:52:49 +00:00
# Some URLs could be domain.tld/@username, so keep this condition above
# the `is_handle` function
is_url(term) ->
2019-09-08 22:52:49 +00:00
# skip, if it's not an actor
case process_from_url(term) do
%Page{total: _total, elements: [%Actor{} = _actor]} = page ->
2019-09-08 22:52:49 +00:00
{:ok, page}
_ ->
{:ok, %{total: 0, elements: []}}
end
is_handle(term) ->
{:ok, process_from_username(term)}
true ->
page =
Actors.build_actors_by_username_or_name_page(
Map.put(args, :term, term),
[result_type],
page,
limit
)
2019-09-08 22:52:49 +00:00
{:ok, page}
end
end
@doc """
Search events
"""
2019-09-08 22:52:49 +00:00
@spec search_events(String.t(), integer | nil, integer | nil) ::
{:ok, Page.t()} | {:error, String.t()}
def search_events(%{term: term} = args, page \\ 1, limit \\ 10) do
term = String.trim(term)
if is_url(term) do
# skip, if it's w not an actor
case process_from_url(term) do
%Page{total: _total, elements: [%Event{} = _event]} = page ->
{:ok, page}
_ ->
{:ok, %{total: 0, elements: []}}
end
else
{:ok, Events.build_events_for_search(Map.put(args, :term, term), page, limit)}
end
end
# If the search string is an username
2019-09-08 22:52:49 +00:00
@spec process_from_username(String.t()) :: Page.t()
defp process_from_username(search) do
case ActivityPub.find_or_make_actor_from_nickname(search) do
{:ok, actor} ->
2019-09-08 22:52:49 +00:00
%Page{total: 1, elements: [actor]}
{:error, _err} ->
Logger.debug(fn -> "Unable to find or make actor '#{search}'" end)
2019-09-08 22:52:49 +00:00
%Page{total: 0, elements: []}
end
end
# If the search string is an URL
2019-09-08 22:52:49 +00:00
@spec process_from_url(String.t()) :: Page.t()
defp process_from_url(search) do
case ActivityPub.fetch_object_from_url(search) do
{:ok, object} ->
2019-09-08 22:52:49 +00:00
%Page{total: 1, elements: [object]}
{:error, _err} ->
Logger.debug(fn -> "Unable to find or make object from URL '#{search}'" end)
2019-09-08 22:52:49 +00:00
%Page{total: 0, elements: []}
end
end
2019-09-08 22:52:49 +00:00
@spec is_url(String.t()) :: boolean
defp is_url(search), do: String.starts_with?(search, ["http://", "https://"])
2019-09-08 22:52:49 +00:00
@spec is_handle(String.t()) :: boolean
defp is_handle(search), do: String.match?(search, ~r/@/)
end