handle already following better, cache actor query

This commit is contained in:
Moon Man 2024-08-29 13:09:11 +00:00
parent 03c8d936fa
commit d7ad3da610
2 changed files with 6 additions and 1 deletions

View File

@ -59,7 +59,7 @@ defmodule Vonbraun.Control do
{:error, {:post, error}} {:error, {:post, error}}
end end
else else
{:pending, {:ok, %{:blocks_me => nil, :following_state => "accepted"}}} -> {:pending, {:error, :already_following}} ->
Logger.info("I think you already follow them.") Logger.info("I think you already follow them.")
:ok :ok

View File

@ -104,6 +104,8 @@ defmodule Vonbraun.Ecto.Schema.Actor do
with {:actor, with {:actor,
{:ok, actor = %{:blocked => blocked?, :following_state => existing_following_state}}} <- {:ok, actor = %{:blocked => blocked?, :following_state => existing_following_state}}} <-
{:actor, maybe_insert(id)} do {:actor, maybe_insert(id)} do
already_following? = existing_following_state == "accepted" && new_state == "pending"
valid_set_pending? = existing_following_state == nil && new_state == "pending" valid_set_pending? = existing_following_state == nil && new_state == "pending"
valid_set_response? = valid_set_response? =
@ -120,6 +122,9 @@ defmodule Vonbraun.Ecto.Schema.Actor do
changeset(actor, %{following_state: new_state, following_ts: DateTime.now!("Etc/UTC")}) changeset(actor, %{following_state: new_state, following_ts: DateTime.now!("Etc/UTC")})
|> Repo.update() |> Repo.update()
already_following? ->
{:error, :already_following}
true -> true ->
{:error, :invalid_state} {:error, :invalid_state}
end end