re-add fetching by url

This commit is contained in:
FloatingGhost 2021-12-15 10:57:47 +00:00
parent c50ad91d31
commit d5c8415496
2 changed files with 34 additions and 1 deletions

View file

@ -164,6 +164,7 @@ def search(:activities, q) do
results results
|> Enum.map(fn result -> result["_id"] end) |> Enum.map(fn result -> result["_id"] end)
|> Pleroma.Activity.all_by_ids_with_object() |> Pleroma.Activity.all_by_ids_with_object()
|> Enum.sort(&(&1.inserted_at >= &2.inserted_at))
else else
e -> e ->
Logger.error(e) Logger.error(e)

View file

@ -1,6 +1,8 @@
defmodule Pleroma.Search.Elasticsearch do defmodule Pleroma.Search.Elasticsearch do
@behaviour Pleroma.Search @behaviour Pleroma.Search
alias Pleroma.Activity
alias Pleroma.Object.Fetcher
alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.ActivityPub.Visibility
@ -18,7 +20,8 @@ def es_query(:activity, query) do
terminate_after: 50, terminate_after: 50,
timeout: "5s", timeout: "5s",
sort: [ sort: [
%{"_timestamp" => "desc"} "_score",
%{_timestamp: %{order: "desc", format: "basic_date_time"}}
], ],
query: %{ query: %{
bool: %{ bool: %{
@ -39,6 +42,9 @@ def es_query(:user, query) do
size: 50, size: 50,
terminate_after: 50, terminate_after: 50,
timeout: "5s", timeout: "5s",
sort: [
"_score"
],
query: %{ query: %{
bool: %{ bool: %{
must: must must: must
@ -58,6 +64,9 @@ def es_query(:hashtag, query) do
size: 50, size: 50,
terminate_after: 50, terminate_after: 50,
timeout: "5s", timeout: "5s",
sort: [
"_score"
],
query: %{ query: %{
bool: %{ bool: %{
must: Parsers.Hashtag.parse(query) must: Parsers.Hashtag.parse(query)
@ -67,6 +76,16 @@ def es_query(:hashtag, query) do
end end
end end
defp maybe_fetch(:activity, search_query) do
with true <- Regex.match?(~r/https?:/, search_query),
{:ok, object} <- Fetcher.fetch_object_from_id(search_query),
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]) do
activity
else
_ -> nil
end
end
@impl Pleroma.Search @impl Pleroma.Search
def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) do def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) do
parsed_query = parsed_query =
@ -74,6 +93,11 @@ def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) d
|> String.trim() |> String.trim()
|> SearchParser.parse!() |> SearchParser.parse!()
activity_fetch_task =
Task.async(fn ->
maybe_fetch(:activity, String.trim(query))
end)
activity_task = activity_task =
Task.async(fn -> Task.async(fn ->
q = es_query(:activity, parsed_query) q = es_query(:activity, parsed_query)
@ -100,6 +124,14 @@ def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) d
activity_results = Task.await(activity_task) activity_results = Task.await(activity_task)
user_results = Task.await(user_task) user_results = Task.await(user_task)
hashtag_results = Task.await(hashtag_task) hashtag_results = Task.await(hashtag_task)
direct_activity = Task.await(activity_fetch_task)
activity_results =
if direct_activity == nil do
activity_results
else
[direct_activity | activity_results]
end
%{ %{
"accounts" => "accounts" =>