From f341e686221598612670c712f754f44cf4e9baed Mon Sep 17 00:00:00 2001 From: sadposter Date: Sun, 12 Dec 2021 19:31:47 +0000 Subject: [PATCH] fix bug in inbound --- lib/pleroma/elasticsearch/store.ex | 27 +++++++++++++++++++- lib/pleroma/search/elasticsearch.ex | 2 +- lib/pleroma/web/activity_pub/pipeline.ex | 2 +- lib/pleroma/web/activity_pub/side_effects.ex | 4 ++- lib/pleroma/web/common_api.ex | 20 +++------------ 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/elasticsearch/store.ex b/lib/pleroma/elasticsearch/store.ex index 31f77fadf..17ba71990 100644 --- a/lib/pleroma/elasticsearch/store.ex +++ b/lib/pleroma/elasticsearch/store.ex @@ -1,9 +1,34 @@ defmodule Pleroma.Elasticsearch do alias Pleroma.Activity alias Pleroma.Elasticsearch.DocumentMappings + alias Pleroma.Config defp url do - Pleroma.Config.get([:elasticsearch, :url]) + Config.get([:elasticsearch, :url]) + end + + def put_by_id(id) do + id + |> Activity.get_by_id_with_object() + |> maybe_put_into_elasticsearch() + end + + def maybe_put_into_elasticsearch({:ok, activity}) do + maybe_put_into_elasticsearch(activity) + end + + def maybe_put_into_elasticsearch(%{data: %{"type" => "Create"}, object: %{data: %{type: "Note"}}} = activity) do + if Config.get([:search, :provider]) == Pleroma.Search.Elasticsearch do + actor = Pleroma.Activity.user_actor(activity) + + activity + |> Map.put(:user_actor, actor) + |> put() + end + end + + def maybe_put_into_elasticsearch(_) do + {:ok, :skipped} end def put(%Activity{} = activity) do diff --git a/lib/pleroma/search/elasticsearch.ex b/lib/pleroma/search/elasticsearch.ex index 4c0127f0f..181009ad6 100644 --- a/lib/pleroma/search/elasticsearch.ex +++ b/lib/pleroma/search/elasticsearch.ex @@ -48,7 +48,7 @@ def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) d q = %{ query: %{ bool: %{ - must: parse(query) + must: parse(String.trim(query)) } } } diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex index 0d6e8aad2..6831e963f 100644 --- a/lib/pleroma/web/activity_pub/pipeline.ex +++ b/lib/pleroma/web/activity_pub/pipeline.ex @@ -27,7 +27,7 @@ defp config, do: Config.get([:pipeline, :config], Config) def common_pipeline(object, meta) do case Repo.transaction(fn -> do_common_pipeline(object, meta) end, Utils.query_timeout()) do {:ok, {:ok, activity, meta}} -> - side_effects().handle_after_transaction(meta) + side_effects().handle_after_transaction(activity, meta) {:ok, activity, meta} {:ok, value} -> diff --git a/lib/pleroma/web/activity_pub/side_effects.ex b/lib/pleroma/web/activity_pub/side_effects.ex index 701181a14..a4169d41f 100644 --- a/lib/pleroma/web/activity_pub/side_effects.ex +++ b/lib/pleroma/web/activity_pub/side_effects.ex @@ -537,7 +537,9 @@ defp add_notifications(meta, notifications) do end @impl true - def handle_after_transaction(meta) do + def handle_after_transaction(activity, meta) do + Pleroma.Elasticsearch.put_by_id(activity.id) + meta |> send_notifications() |> send_streamables() diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index 0c93b1976..f6a131c21 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -16,8 +16,6 @@ defmodule Pleroma.Web.CommonAPI do alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.CommonAPI.ActivityDraft - alias Pleroma.Elasticsearch - alias Pleroma.Config import Pleroma.Web.Gettext import Pleroma.Web.CommonAPI.Utils @@ -397,24 +395,12 @@ def listen(user, data) do end end - def maybe_put_into_elasticsearch({:ok, activity}) do - if Config.get([:search, :provider]) == Pleroma.Search.Elasticsearch do - actor = Pleroma.Activity.user_actor(activity) - - activity - |> Map.put(:user_actor, actor) - |> Elasticsearch.put() - end - end - - def maybe_put_into_elasticsearch(_) do - {:ok, :skipped} - end - def post(user, %{status: _} = data) do with {:ok, draft} <- ActivityDraft.create(user, data) do activity = ActivityPub.create(draft.changes, draft.preview?) - maybe_put_into_elasticsearch(activity) + unless draft.preview? do + Pleroma.Elasticsearch.maybe_put_into_elasticsearch(activity) + end activity end end