Fix unfollows
authorFrancis Dinh <normandy@firemail.cc>
Mon, 21 May 2018 01:01:14 +0000 (21:01 -0400)
committerFrancis Dinh <normandy@firemail.cc>
Mon, 21 May 2018 01:01:14 +0000 (21:01 -0400)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/twitter_api/representers/activity_representer.ex
test/fixtures/mastodon-unfollow-activity.json
test/web/activity_pub/activity_pub_test.exs

index a711e6b76d2248d2962cabcec1a165c6c868162b..c2d540db97187d29503961f8885dc2cfa51a0ca6 100644 (file)
@@ -192,12 +192,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     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),
-         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,
-         maybe_federate(activity) do
+         :ok <- maybe_federate(activity) do
       {:ok, activity}
     end
   end
@@ -221,23 +220,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     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)
 
     if follow_activity do
       unfollow(blocker, blocked, local)
     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 <- maybe_federate(activity) do
       {:ok, activity}
     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),
-         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 <- maybe_federate(activity) do
       {:ok, activity}
index d92ca9b65d06f5b22651918a787363edf1dba403..20b1603df11075f78cdbd45a1d773741635fa319 100644 (file)
@@ -251,7 +251,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       ) do
     with %User{local: true} = followed = User.get_cached_by_ap_id(followed),
          %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)
       {:ok, activity}
     else
@@ -264,7 +264,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       ) do
     with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
          %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.block(blocker, blocked)
       {:ok, activity}
index 050413d51040f7f60d69aec7cd9c795aaf98a575..acf23c53e006b9f4589acbb0801e5c54fa8dc18c 100644 (file)
@@ -346,13 +346,14 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   #### 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",
       "actor" => follower.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
 
   #### Block-related helpers
@@ -373,22 +374,24 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     Repo.one(query)
   end
 
-  def make_block_data(blocker, blocked) do
-    %{
+  def make_block_data(blocker, blocked, activity_id) do
+    data = %{
       "type" => "Block",
       "actor" => blocker.ap_id,
       "to" => [blocked.ap_id],
       "object" => blocked.ap_id
     }
+    if activity_id, do: Map.put(data, "id", activity_id), else: data
   end
 
-  def make_unblock_data(blocker, blocked, block_activity) do
-    %{
+  def make_unblock_data(blocker, blocked, block_activity, activity_id) do
+    data = %{
       "type" => "Undo",
       "actor" => blocker.ap_id,
       "to" => [blocked.ap_id],
       "object" => block_activity.data
     }
+    if activity_id, do: Map.put(data, "id", activity_id), else: data
   end
 
   #### Create-related helpers
index c2e1f07a5a4e6b823360860a48c7d9eb847d50c8..57837205e08132f67efbf55b1dcc095a6a98c934 100644 (file)
@@ -99,7 +99,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       ) do
     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,
index 022b47acdbe37a11b7c7299f7116ac2e6908a62e..ae5ab7c66fd0465f0a7bd04ad2f49060c20df345 100644 (file)
@@ -1,33 +1,34 @@
 {
-    "@context": [
-            "https://www.w3.org/ns/activitystreams",
-            "https://w3id.org/security/v1",
-            {
-              "toot": "http://joinmastodon.org/ns#",
-              "sensitive": "as:sensitive",
-              "ostatus": "http://ostatus.org#",
-              "movedTo": "as:movedTo",
-              "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
-              "inReplyToAtomUri": "ostatus:inReplyToAtomUri",
-              "conversation": "ostatus:conversation",
-              "atomUri": "ostatus:atomUri",
-              "Hashtag": "as:Hashtag",
-              "Emoji": "toot:Emoji"
-            }
-          ],
-    "signature": {
-        "type": "RsaSignature2017",
-        "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==",
-        "creator": "http://mastodon.example.org/users/admin#main-key",
-        "created": "2018-02-17T13:29:31Z"
-      },
-    "type": "Undo",
-    "object": {
-      "type": "Follow", 
-      "object": "http://localtesting.pleroma.lol/users/lain",
-      "nickname": "lain",
-      "id": "http://mastodon.example.org/users/admin#follows/2",
-      "actor": "http://mastodon.example.org/users/admin" 
-    },
-    "actor": "http://mastodon.example.org/users/admin"
+   "@context":[
+      "https://www.w3.org/ns/activitystreams",
+      "https://w3id.org/security/v1",
+      {
+         "toot":"http://joinmastodon.org/ns#",
+         "sensitive":"as:sensitive",
+         "ostatus":"http://ostatus.org#",
+         "movedTo":"as:movedTo",
+         "manuallyApprovesFollowers":"as:manuallyApprovesFollowers",
+         "inReplyToAtomUri":"ostatus:inReplyToAtomUri",
+         "conversation":"ostatus:conversation",
+         "atomUri":"ostatus:atomUri",
+         "Hashtag":"as:Hashtag",
+         "Emoji":"toot:Emoji"
+      }
+   ],
+   "signature":{
+      "type":"RsaSignature2017",
+      "signatureValue":"Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==",
+      "creator":"http://mastodon.example.org/users/admin#main-key",
+      "created":"2018-02-17T13:29:31Z"
+   },
+   "type":"Undo",
+   "object":{
+      "type":"Follow",
+      "object":"http://localtesting.pleroma.lol/users/lain",
+      "nickname":"lain",
+      "id":"http://mastodon.example.org/users/admin#follows/2",
+      "actor":"http://mastodon.example.org/users/admin"
+   },
+   "actor":"http://mastodon.example.org/users/admin",
+   "id": "http://mastodon.example.org/users/admin#follow/2/undo"
 }
index 9adce84b57f7ef67eb55b2d81d911a768b6805c6..35f0deffebd65cb3cfec8b87bf3940e478396544 100644 (file)
@@ -425,7 +425,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
 
       assert activity.data["type"] == "Undo"
       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