Handle unfollows from OStatus
authorFrancis Dinh <normandy@firemail.cc>
Mon, 21 May 2018 08:36:20 +0000 (04:36 -0400)
committerFrancis Dinh <normandy@firemail.cc>
Mon, 21 May 2018 08:36:20 +0000 (04:36 -0400)
lib/pleroma/web/ostatus/activity_representer.ex
lib/pleroma/web/ostatus/handlers/unfollow_handler.ex [new file with mode: 0644]
lib/pleroma/web/ostatus/ostatus.ex

index 64cadba1bcad5d15e5c8040b31a242d3140cc6a6..a9c2b89b4d9b1c8bde5e43928085c61a8d3e4d6e 100644 (file)
@@ -232,7 +232,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
   end
 
   # Only undos of follow for now. Will need to get redone once there are more
-  def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
+  def to_simple_form(
+        %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
+          activity,
+        user,
+        with_author
+      ) do
     h = fn str -> [to_charlist(str)] end
 
     updated_at = activity.data["published"]
@@ -240,34 +245,25 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
 
     author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
 
-    follow_activity =
-      if is_map(activity.data["object"]) do
-        Activity.get_by_ap_id(activity.data["object"]["id"])
-      else
-        Activity.get_by_ap_id(activity.data["object"])
-      end
-
     mentions = (activity.recipients || []) |> get_mentions
-
-    if follow_activity do
-      [
-        {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
-        {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
-        {:id, h.(activity.data["id"])},
-        {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
-        {:content, [type: 'html'],
-         ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
-        {:published, h.(inserted_at)},
-        {:updated, h.(updated_at)},
-        {:"activity:object",
-         [
-           {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
-           {:id, h.(follow_activity.data["object"])},
-           {:uri, h.(follow_activity.data["object"])}
-         ]},
-        {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
-      ] ++ mentions ++ author
-    end
+    follow_activity = Activity.get_by_ap_id(follow_activity["id"])
+    [
+      {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
+      {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
+      {:id, h.(activity.data["id"])},
+      {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
+      {:content, [type: 'html'],
+       ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
+      {:published, h.(inserted_at)},
+      {:updated, h.(updated_at)},
+      {:"activity:object",
+       [
+         {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
+         {:id, h.(follow_activity.data["object"])},
+         {:uri, h.(follow_activity.data["object"])}
+       ]},
+      {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
+    ] ++ mentions ++ author
   end
 
   def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
diff --git a/lib/pleroma/web/ostatus/handlers/unfollow_handler.ex b/lib/pleroma/web/ostatus/handlers/unfollow_handler.ex
new file mode 100644 (file)
index 0000000..a115bf4
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Web.OStatus.UnfollowHandler do
+  alias Pleroma.Web.{XML, OStatus}
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.User
+
+  def handle(entry, doc) do
+    with {:ok, actor} <- OStatus.find_make_or_update_user(doc),
+         id when not is_nil(id) <- XML.string_from_xpath("/entry/id", entry),
+         followed_uri when not is_nil(followed_uri) <-
+           XML.string_from_xpath("/entry/activity:object/id", entry),
+         {:ok, followed} <- OStatus.find_or_make_user(followed_uri),
+         {:ok, activity} <- ActivityPub.unfollow(actor, followed, id, false) do
+      User.unfollow(actor, followed)
+      {:ok, activity}
+    end
+  end
+end
index 5c4a1fd695dde472f522170f3c5f1be4e1c5204a..f0ff0624fcab1f69b2369ed355bcd3423f75959b 100644 (file)
@@ -8,7 +8,7 @@ defmodule Pleroma.Web.OStatus do
   alias Pleroma.{Repo, User, Web, Object, Activity}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.{WebFinger, Websub}
-  alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler, DeleteHandler}
+  alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}
   alias Pleroma.Web.ActivityPub.Transmogrifier
 
   def feed_path(user) do
@@ -47,6 +47,9 @@ defmodule Pleroma.Web.OStatus do
               'http://activitystrea.ms/schema/1.0/follow' ->
                 with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
 
+              'http://activitystrea.ms/schema/1.0/unfollow' ->
+                with {:ok, activity} <- UnfollowHandler.handle(entry, doc), do: activity
+
               'http://activitystrea.ms/schema/1.0/share' ->
                 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc),
                      do: [activity, retweeted_activity]