handle already following more gracefully

This commit is contained in:
Moon Man 2024-08-29 13:15:47 +00:00
parent d7ad3da610
commit 7bc76058a6
1 changed files with 2 additions and 25 deletions

View File

@ -1,14 +1,9 @@
defmodule Vonbraun.Control do defmodule Vonbraun.Control do
require Logger require Logger
alias Vonbraun.ActivityPub.Object
alias Vonbraun.ActivityPubReq alias Vonbraun.ActivityPubReq
alias Vonbraun.Ecto.Schema.Actor alias Vonbraun.Ecto.Schema.Actor
defp self_id() do
domain = Application.fetch_env!(:vonbraun, :domain)
nickname = Application.fetch_env!(:vonbraun, :nickname)
"https://#{domain}/users/#{nickname}"
end
@spec follow(String.t()) :: @spec follow(String.t()) ::
:ok :ok
| {:error, | {:error,
@ -23,25 +18,7 @@ defmodule Vonbraun.Control do
{:parse_inbox, inbox = %URI{}} <- {:parse_inbox, URI.parse(raw_inbox)}, {:parse_inbox, inbox = %URI{}} <- {:parse_inbox, URI.parse(raw_inbox)},
{:pending, {:ok, %{:blocks_me => nil, :following_state => "pending"}}} <- {:pending, {:ok, %{:blocks_me => nil, :following_state => "pending"}}} <-
{:pending, Actor.mark_pending_follow(followee_actor_id, "pending")} do {:pending, Actor.mark_pending_follow(followee_actor_id, "pending")} do
domain = Application.fetch_env!(:vonbraun, :domain) payload = Object.follow_activity(followee_actor_id) |> Jason.encode!()
# Is it a bad idea making this absolute?
activity_id = "https://#{domain}/id/follow:#{URI.encode(followee_actor_id)}"
payload =
%{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => activity_id,
"to" => [
followee_actor_id
],
"cc" => [],
"bcc" => [],
"bto" => [],
"type" => "Follow",
"actor" => self_id(),
"object" => followee_actor_id
}
|> Jason.encode!()
case ActivityPubReq.post(inbox, payload) do case ActivityPubReq.post(inbox, payload) do
{:ok, %{:status => status, :body => body}} when status >= 200 and status <= 299 -> {:ok, %{:status => status, :body => body}} when status >= 200 and status <= 299 ->