don't error out

This commit is contained in:
FloatingGhost 2021-12-30 18:03:02 +00:00
parent 4e8ee48cff
commit 4e7e95a17e

View file

@ -59,31 +59,43 @@ def maybe_bulk_post(data, type) do
end end
def put(%Activity{} = activity) do def put(%Activity{} = activity) do
{:ok, _} = with {:ok, _} <-
Elastix.Document.index( Elastix.Document.index(
url(), url(),
"activities", "activities",
"activity", "activity",
DocumentMappings.Activity.id(activity), DocumentMappings.Activity.id(activity),
DocumentMappings.Activity.encode(activity) DocumentMappings.Activity.encode(activity)
) ) do
activity
activity |> Map.get(:object)
|> Map.get(:object) |> Object.hashtags()
|> Object.hashtags() |> Enum.map(fn x ->
|> Enum.map(fn x -> %{id: x, name: x, timestamp: DateTime.to_iso8601(DateTime.utc_now())} end) %{id: x, name: x, timestamp: DateTime.to_iso8601(DateTime.utc_now())}
|> bulk_post(:hashtags) end)
|> bulk_post(:hashtags)
else
{:error, %{reason: err}} ->
Logger.error("Could not put activity: #{err}")
:skipped
end
end end
def put(%User{} = user) do def put(%User{} = user) do
{:ok, _} = with {:ok, _} <-
Elastix.Document.index( Elastix.Document.index(
url(), url(),
"users", "users",
"user", "user",
DocumentMappings.User.id(user), DocumentMappings.User.id(user),
DocumentMappings.User.encode(user) DocumentMappings.User.encode(user)
) ) do
:ok
else
{:error, %{reason: err}} ->
Logger.error("Could not put user: #{err}")
:skipped
end
end end
def bulk_post(data, :activities) do def bulk_post(data, :activities) do
@ -105,13 +117,22 @@ def bulk_post(data, :activities) do
end) end)
|> List.flatten() |> List.flatten()
{:ok, %{body: %{"errors" => false}}} = with {:ok, %{body: %{"errors" => false}}} <-
Elastix.Bulk.post( Elastix.Bulk.post(
url(), url(),
d, d,
index: "activities", index: "activities",
type: "activity" 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 end
def bulk_post(data, :users) do def bulk_post(data, :users) do
@ -126,12 +147,22 @@ def bulk_post(data, :users) do
end) end)
|> List.flatten() |> List.flatten()
Elastix.Bulk.post( with {:ok, %{body: %{"errors" => false}}} <-
url(), Elastix.Bulk.post(
d, url(),
index: "users", d,
type: "user" 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 end
def bulk_post(data, :hashtags) when is_list(data) do 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) end)
|> List.flatten() |> List.flatten()
Elastix.Bulk.post( with {:ok, %{body: %{"errors" => false}}} <-
url(), Elastix.Bulk.post(
d, url(),
index: "hashtags", d,
type: "hashtag" 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 end
def bulk_post(_, :hashtags), do: {:ok, nil} def bulk_post(_, :hashtags), do: {:ok, nil}