From 4e7e95a17e3a880e5a04805d832e83da5631ba16 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Thu, 30 Dec 2021 18:03:02 +0000 Subject: [PATCH] don't error out --- lib/pleroma/elasticsearch/store.ex | 125 +++++++++++++++++++---------- 1 file changed, 83 insertions(+), 42 deletions(-) diff --git a/lib/pleroma/elasticsearch/store.ex b/lib/pleroma/elasticsearch/store.ex index db4822aac..01561a0df 100644 --- a/lib/pleroma/elasticsearch/store.ex +++ b/lib/pleroma/elasticsearch/store.ex @@ -19,7 +19,7 @@ def delete_by_id(:activity, id) do Elastix.Document.delete(url(), "activities", "activity", id) end end - + def put_by_id(:activity, id) do id |> Activity.get_by_id_with_object() @@ -59,31 +59,43 @@ def maybe_bulk_post(data, type) do end def put(%Activity{} = activity) do - {:ok, _} = - Elastix.Document.index( - url(), - "activities", - "activity", - DocumentMappings.Activity.id(activity), - DocumentMappings.Activity.encode(activity) - ) - - activity - |> Map.get(:object) - |> Object.hashtags() - |> Enum.map(fn x -> %{id: x, name: x, timestamp: DateTime.to_iso8601(DateTime.utc_now())} end) - |> bulk_post(:hashtags) + with {:ok, _} <- + Elastix.Document.index( + url(), + "activities", + "activity", + DocumentMappings.Activity.id(activity), + DocumentMappings.Activity.encode(activity) + ) do + activity + |> Map.get(:object) + |> Object.hashtags() + |> Enum.map(fn x -> + %{id: x, name: x, timestamp: DateTime.to_iso8601(DateTime.utc_now())} + end) + |> bulk_post(:hashtags) + else + {:error, %{reason: err}} -> + Logger.error("Could not put activity: #{err}") + :skipped + end end def put(%User{} = user) do - {:ok, _} = - Elastix.Document.index( - url(), - "users", - "user", - DocumentMappings.User.id(user), - DocumentMappings.User.encode(user) - ) + with {:ok, _} <- + Elastix.Document.index( + url(), + "users", + "user", + DocumentMappings.User.id(user), + DocumentMappings.User.encode(user) + ) do + :ok + else + {:error, %{reason: err}} -> + Logger.error("Could not put user: #{err}") + :skipped + end end def bulk_post(data, :activities) do @@ -105,13 +117,22 @@ def bulk_post(data, :activities) do end) |> List.flatten() - {:ok, %{body: %{"errors" => false}}} = - Elastix.Bulk.post( - url(), - d, - index: "activities", - type: "activity" - ) + with {:ok, %{body: %{"errors" => false}}} <- + Elastix.Bulk.post( + url(), + d, + index: "activities", + type: "activity" + ) do + :ok + else + {:error, %{reason: err}} -> + Logger.error("Could not bulk put activity: #{err}") + :skipped + {:ok, %{body: body}} -> + IO.inspect(body) + :skipped + end end def bulk_post(data, :users) do @@ -126,12 +147,22 @@ def bulk_post(data, :users) do end) |> List.flatten() - Elastix.Bulk.post( - url(), - d, - index: "users", - type: "user" - ) + with {:ok, %{body: %{"errors" => false}}} <- + Elastix.Bulk.post( + url(), + d, + index: "users", + type: "user" + ) do + :ok + else + {:error, %{reason: err}} -> + Logger.error("Could not bulk put users: #{err}") + :skipped + {:ok, %{body: body}} -> + IO.inspect(body) + :skipped + end end def bulk_post(data, :hashtags) when is_list(data) do @@ -145,12 +176,22 @@ def bulk_post(data, :hashtags) when is_list(data) do end) |> List.flatten() - Elastix.Bulk.post( - url(), - d, - index: "hashtags", - type: "hashtag" - ) + with {:ok, %{body: %{"errors" => false}}} <- + Elastix.Bulk.post( + url(), + d, + index: "hashtags", + type: "hashtag" + ) do + :ok + else + {:error, %{reason: err}} -> + Logger.error("Could not bulk put hashtags: #{err}") + :skipped + {:ok, %{body: body}} -> + IO.inspect(body) + :skipped + end end def bulk_post(_, :hashtags), do: {:ok, nil}