Fix unfollows

This commit is contained in:
Francis Dinh 2018-05-20 21:01:14 -04:00
parent 56de0216ae
commit 89c5de9096
6 changed files with 53 additions and 50 deletions

View File

@ -192,12 +192,11 @@ def follow(follower, followed, activity_id \\ nil, local \\ true) do
end end
end end
def unfollow(follower, followed, local \\ true) do def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed), with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
unfollow_data <- make_unfollow_data(follower, followed, follow_activity), unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
{:ok, activity} <- insert(unfollow_data, local), {:ok, activity} <- insert(unfollow_data, local),
:ok, :ok <- maybe_federate(activity) do
maybe_federate(activity) do
{:ok, activity} {:ok, activity}
end end
end end
@ -221,23 +220,23 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ tru
end end
end end
def block(blocker, blocked, local \\ true) do def block(blocker, blocked, activity_id \\ nil, local \\ true) do
follow_activity = fetch_latest_follow(blocker, blocked) follow_activity = fetch_latest_follow(blocker, blocked)
if follow_activity do if follow_activity do
unfollow(blocker, blocked, local) unfollow(blocker, blocked, local)
end end
with block_data <- make_block_data(blocker, blocked), with block_data <- make_block_data(blocker, blocked, activity_id),
{:ok, activity} <- insert(block_data, local), {:ok, activity} <- insert(block_data, local),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
end end
end end
def unblock(blocker, blocked, local \\ true) do def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked), with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
unblock_data <- make_unblock_data(blocker, blocked, block_activity), unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
{:ok, activity} <- insert(unblock_data, local), {:ok, activity} <- insert(unblock_data, local),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}

View File

@ -251,7 +251,7 @@ def handle_incoming(
) do ) do
with %User{local: true} = followed = User.get_cached_by_ap_id(followed), with %User{local: true} = followed = User.get_cached_by_ap_id(followed),
%User{} = follower = User.get_or_fetch_by_ap_id(follower), %User{} = follower = User.get_or_fetch_by_ap_id(follower),
{:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
User.unfollow(follower, followed) User.unfollow(follower, followed)
{:ok, activity} {:ok, activity}
else else
@ -264,7 +264,7 @@ def handle_incoming(
) do ) do
with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
%User{} = blocker = User.get_or_fetch_by_ap_id(blocker), %User{} = blocker = User.get_or_fetch_by_ap_id(blocker),
{:ok, activity} <- ActivityPub.block(blocker, blocked, false) do {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
User.unfollow(blocker, blocked) User.unfollow(blocker, blocked)
User.block(blocker, blocked) User.block(blocker, blocked)
{:ok, activity} {:ok, activity}

View File

@ -346,13 +346,14 @@ def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
#### Unfollow-related helpers #### Unfollow-related helpers
def make_unfollow_data(follower, followed, follow_activity) do def make_unfollow_data(follower, followed, follow_activity, activity_id) do
%{ data = %{
"type" => "Undo", "type" => "Undo",
"actor" => follower.ap_id, "actor" => follower.ap_id,
"to" => [followed.ap_id], "to" => [followed.ap_id],
"object" => follow_activity.data["id"] "object" => follow_activity.data
} }
if activity_id, do: Map.put(data, "id", activity_id), else: data
end end
#### Block-related helpers #### Block-related helpers
@ -373,22 +374,24 @@ def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
Repo.one(query) Repo.one(query)
end end
def make_block_data(blocker, blocked) do def make_block_data(blocker, blocked, activity_id) do
%{ data = %{
"type" => "Block", "type" => "Block",
"actor" => blocker.ap_id, "actor" => blocker.ap_id,
"to" => [blocked.ap_id], "to" => [blocked.ap_id],
"object" => blocked.ap_id "object" => blocked.ap_id
} }
if activity_id, do: Map.put(data, "id", activity_id), else: data
end end
def make_unblock_data(blocker, blocked, block_activity) do def make_unblock_data(blocker, blocked, block_activity, activity_id) do
%{ data = %{
"type" => "Undo", "type" => "Undo",
"actor" => blocker.ap_id, "actor" => blocker.ap_id,
"to" => [blocked.ap_id], "to" => [blocked.ap_id],
"object" => block_activity.data "object" => block_activity.data
} }
if activity_id, do: Map.put(data, "id", activity_id), else: data
end end
#### Create-related helpers #### Create-related helpers

View File

@ -99,7 +99,7 @@ def to_map(
) do ) do
created_at = created_at |> Utils.date_to_asctime() created_at = created_at |> Utils.date_to_asctime()
text = "#{user.nickname} undid the action at #{undid_activity}" text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
%{ %{
"id" => activity.id, "id" => activity.id,

View File

@ -1,33 +1,34 @@
{ {
"@context": [ "@context":[
"https://www.w3.org/ns/activitystreams", "https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1", "https://w3id.org/security/v1",
{ {
"toot": "http://joinmastodon.org/ns#", "toot":"http://joinmastodon.org/ns#",
"sensitive": "as:sensitive", "sensitive":"as:sensitive",
"ostatus": "http://ostatus.org#", "ostatus":"http://ostatus.org#",
"movedTo": "as:movedTo", "movedTo":"as:movedTo",
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers", "manuallyApprovesFollowers":"as:manuallyApprovesFollowers",
"inReplyToAtomUri": "ostatus:inReplyToAtomUri", "inReplyToAtomUri":"ostatus:inReplyToAtomUri",
"conversation": "ostatus:conversation", "conversation":"ostatus:conversation",
"atomUri": "ostatus:atomUri", "atomUri":"ostatus:atomUri",
"Hashtag": "as:Hashtag", "Hashtag":"as:Hashtag",
"Emoji": "toot:Emoji" "Emoji":"toot:Emoji"
} }
], ],
"signature": { "signature":{
"type": "RsaSignature2017", "type":"RsaSignature2017",
"signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", "signatureValue":"Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==",
"creator": "http://mastodon.example.org/users/admin#main-key", "creator":"http://mastodon.example.org/users/admin#main-key",
"created": "2018-02-17T13:29:31Z" "created":"2018-02-17T13:29:31Z"
}, },
"type": "Undo", "type":"Undo",
"object": { "object":{
"type": "Follow", "type":"Follow",
"object": "http://localtesting.pleroma.lol/users/lain", "object":"http://localtesting.pleroma.lol/users/lain",
"nickname": "lain", "nickname":"lain",
"id": "http://mastodon.example.org/users/admin#follows/2", "id":"http://mastodon.example.org/users/admin#follows/2",
"actor": "http://mastodon.example.org/users/admin" "actor":"http://mastodon.example.org/users/admin"
}, },
"actor": "http://mastodon.example.org/users/admin" "actor":"http://mastodon.example.org/users/admin",
"id": "http://mastodon.example.org/users/admin#follow/2/undo"
} }

View File

@ -425,7 +425,7 @@ test "creates an undo activity for the last follow" do
assert activity.data["type"] == "Undo" assert activity.data["type"] == "Undo"
assert activity.data["actor"] == follower.ap_id assert activity.data["actor"] == follower.ap_id
assert activity.data["object"] == follow_activity.data["id"] assert activity.data["object"]["id"] == follow_activity.data["id"]
end end
end end