2019-02-21 18:11:49 +01:00
|
|
|
defmodule MobilizonWeb.API.Search do
|
2019-02-22 14:18:52 +01:00
|
|
|
@moduledoc """
|
|
|
|
API for Search
|
|
|
|
"""
|
2019-02-21 18:11:49 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.{Event, Comment}
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc """
|
2019-04-12 15:04:32 +02:00
|
|
|
Search actors
|
2019-02-21 18:11:49 +01:00
|
|
|
"""
|
2019-04-12 15:04:32 +02:00
|
|
|
@spec search_actors(String.t(), integer(), integer(), String.t()) ::
|
|
|
|
{:ok, %{total: integer(), elements: list(Actor.t())}} | {:error, any()}
|
|
|
|
def search_actors(search, page \\ 1, limit \\ 10, result_type) do
|
|
|
|
search = String.trim(search)
|
2019-02-21 18:11:49 +01:00
|
|
|
|
2019-04-12 15:04:32 +02:00
|
|
|
cond do
|
|
|
|
search == "" ->
|
|
|
|
{:error, "Search can't be empty"}
|
|
|
|
|
|
|
|
# Some URLs could be domain.tld/@username, so keep this condition above handle_search? function
|
|
|
|
url_search?(search) ->
|
|
|
|
# If this is not an actor, skip
|
|
|
|
with %{:total => total, :elements => [%Actor{}] = elements} <- process_from_url(search) do
|
|
|
|
{:ok, %{total: total, elements: elements}}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
{:ok, %{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
handle_search?(search) ->
|
|
|
|
{:ok, process_from_username(search)}
|
|
|
|
|
|
|
|
true ->
|
|
|
|
{:ok,
|
|
|
|
Actors.find_and_count_actors_by_username_or_name(search, [result_type], page, limit)}
|
|
|
|
end
|
2019-02-21 18:11:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-04-12 15:04:32 +02:00
|
|
|
Search events
|
2019-02-21 18:11:49 +01:00
|
|
|
"""
|
|
|
|
@spec search_events(String.t(), integer(), integer()) ::
|
2019-04-12 15:04:32 +02:00
|
|
|
{:ok, %{total: integer(), elements: list(Event.t())}} | {:error, any()}
|
2019-02-21 18:11:49 +01:00
|
|
|
def search_events(search, page \\ 1, limit \\ 10) do
|
|
|
|
search = String.trim(search)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
search == "" ->
|
|
|
|
{:error, "Search can't be empty"}
|
|
|
|
|
2019-04-12 15:04:32 +02:00
|
|
|
url_search?(search) ->
|
|
|
|
# If this is not an event, skip
|
|
|
|
with {total = total, [%Event{} = elements]} <- process_from_url(search) do
|
|
|
|
{:ok, %{total: total, elements: elements}}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
{:ok, %{total: 0, elements: []}}
|
|
|
|
end
|
2019-02-21 18:11:49 +01:00
|
|
|
|
|
|
|
true ->
|
2019-04-12 15:04:32 +02:00
|
|
|
{:ok, Events.find_and_count_events_by_name(search, page, limit)}
|
2019-02-21 18:11:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the search string is an username
|
2019-04-12 15:04:32 +02:00
|
|
|
@spec process_from_username(String.t()) :: %{total: integer(), elements: [Actor.t()]}
|
2019-02-21 18:11:49 +01:00
|
|
|
defp process_from_username(search) do
|
|
|
|
with {:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(search) do
|
2019-04-12 15:04:32 +02:00
|
|
|
%{total: 1, elements: [actor]}
|
2019-02-21 18:11:49 +01:00
|
|
|
else
|
|
|
|
{:error, _err} ->
|
2019-02-22 14:18:52 +01:00
|
|
|
Logger.debug(fn -> "Unable to find or make actor '#{search}'" end)
|
2019-04-12 15:04:32 +02:00
|
|
|
%{total: 0, elements: []}
|
2019-02-21 18:11:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the search string is an URL
|
2019-04-12 15:04:32 +02:00
|
|
|
@spec process_from_url(String.t()) :: %{
|
|
|
|
total: integer(),
|
|
|
|
elements: [Actor.t() | Event.t() | Comment.t()]
|
|
|
|
}
|
2019-02-21 18:11:49 +01:00
|
|
|
defp process_from_url(search) do
|
|
|
|
with {:ok, object} <- ActivityPub.fetch_object_from_url(search) do
|
2019-04-12 15:04:32 +02:00
|
|
|
%{total: 1, elements: [object]}
|
2019-02-21 18:11:49 +01:00
|
|
|
else
|
|
|
|
{:error, _err} ->
|
2019-02-22 14:18:52 +01:00
|
|
|
Logger.debug(fn -> "Unable to find or make object from URL '#{search}'" end)
|
2019-04-12 15:04:32 +02:00
|
|
|
%{total: 0, elements: []}
|
2019-02-21 18:11:49 +01:00
|
|
|
end
|
|
|
|
end
|
2019-04-12 15:04:32 +02:00
|
|
|
|
|
|
|
# Is the search an URL search?
|
|
|
|
@spec url_search?(String.t()) :: boolean
|
|
|
|
defp url_search?(search) do
|
|
|
|
String.starts_with?(search, "https://") or String.starts_with?(search, "http://")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Is the search an handle search?
|
|
|
|
@spec handle_search?(String.t()) :: boolean
|
|
|
|
defp handle_search?(search) do
|
|
|
|
String.match?(search, ~r/@/)
|
|
|
|
end
|
2019-02-21 18:11:49 +01:00
|
|
|
end
|