From fcbd2e9472b647a11aa97aa0fc2848a03edbde8e Mon Sep 17 00:00:00 2001 From: Moon Man Date: Thu, 29 Aug 2024 17:27:07 +0000 Subject: [PATCH] follow works on pleroma now --- lib/vonbraun/activity_pub/handler/follow.ex | 12 +++++++++--- lib/vonbraun/activity_pub/object.ex | 5 +++-- lib/vonbraun/activitypub_req.ex | 3 ++- lib/vonbraun/ecto/schema/pair.ex | 17 ++++++++++++++--- lib/vonbraun/router.ex | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/vonbraun/activity_pub/handler/follow.ex b/lib/vonbraun/activity_pub/handler/follow.ex index d048d0f..161bddc 100644 --- a/lib/vonbraun/activity_pub/handler/follow.ex +++ b/lib/vonbraun/activity_pub/handler/follow.ex @@ -11,10 +11,15 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do def type, do: @verb def handle( - %{"type" => @verb, "actor" => follow_requester_id, "object" => follow_target}, + %{ + "id" => activity_id, + "type" => @verb, + "actor" => follow_requester_id, + "object" => follow_target + }, actor = %{} ) - when is_binary(follow_requester_id) and is_binary(follow_target) do + when is_binary(follow_requester_id) and is_binary(follow_target) and is_binary(activity_id) do with {:valid_target, true} <- {:valid_target, Object.my_id() == follow_target}, {:add, {:ok, %Actor{:blocked => nil, :follows_me_state => follows_me_state}}} when not is_nil(follows_me_state) <- @@ -33,7 +38,8 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do if activity_type do payload = - Object.accept_follow_activity(follow_requester_id, activity_type) |> Jason.encode!() + Object.accept_follow_activity(follow_requester_id, activity_id, activity_type) + |> Jason.encode!() Logger.debug("Replying to follow request with: #{activity_type}") Logger.debug("And payload: `#{payload}`") diff --git a/lib/vonbraun/activity_pub/object.ex b/lib/vonbraun/activity_pub/object.ex index 4a76537..4d0fc63 100644 --- a/lib/vonbraun/activity_pub/object.ex +++ b/lib/vonbraun/activity_pub/object.ex @@ -56,8 +56,8 @@ defmodule Vonbraun.ActivityPub.Object do def follow_activity(to_follow_id) when is_binary(to_follow_id), do: activity("Follow", my_follow_activity_id(to_follow_id), to_follow_id, to: to_follow_id) - def accept_follow_activity(followee_id, type \\ :accept) - when is_binary(followee_id) and type in [:accept, :reject] do + def accept_follow_activity(followee_id, activity_id, type \\ :accept) + when is_binary(followee_id) and is_binary(activity_id) and type in [:accept, :reject] do activity_type = if type == :accept do "Accept" @@ -66,6 +66,7 @@ defmodule Vonbraun.ActivityPub.Object do end object = %{ + "id" => activity_id, "type" => "Follow", "actor" => followee_id } diff --git a/lib/vonbraun/activitypub_req.ex b/lib/vonbraun/activitypub_req.ex index 4c847d4..05f2f07 100644 --- a/lib/vonbraun/activitypub_req.ex +++ b/lib/vonbraun/activitypub_req.ex @@ -28,7 +28,8 @@ defmodule Vonbraun.ActivityPubReq do when is_binary(body) do headers = %{ "host" => host, - "accept" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" + "accept" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", + "content-type" => "application/activity+json" } target = diff --git a/lib/vonbraun/ecto/schema/pair.ex b/lib/vonbraun/ecto/schema/pair.ex index aeffae5..67264e4 100644 --- a/lib/vonbraun/ecto/schema/pair.ex +++ b/lib/vonbraun/ecto/schema/pair.ex @@ -15,7 +15,8 @@ defmodule Vonbraun.Ecto.Schema.Pair do |> Changeset.cast(params, [:value]) end - def set(key, value) do + @spec set(String.t(), String.t()) :: {:ok, String.t() | nil} | {:error, any()} + def set(key, value) when is_binary(key) and (is_binary(value) or is_nil(value)) do case Repo.get(__MODULE__, key) do nil -> changeset(%__MODULE__{key: key}, %{value: value}) @@ -27,7 +28,17 @@ defmodule Vonbraun.Ecto.Schema.Pair do end end - def get(key) do - Repo.get(__MODULE__, key) + @spec get(String.t()) :: String.t() | nil + def get(key) when is_binary(key) do + case Repo.get(__MODULE__, key) do + %__MODULE__{:value => value} -> value + _ -> nil + end + end + + @spec delete(String.t()) :: any() + def delete(key) when is_binary(key) do + Repo.delete(%__MODULE__{key: key}) + :ok end end diff --git a/lib/vonbraun/router.ex b/lib/vonbraun/router.ex index 917b2bb..5e61169 100644 --- a/lib/vonbraun/router.ex +++ b/lib/vonbraun/router.ex @@ -220,7 +220,7 @@ defmodule Vonbraun.MyRouter do nil -> send_resp(conn, 404, "fuck off") - %{:value => value} -> + value -> send_resp(conn, 200, value) end end