Handle client submitted activitypub like activity
authorsxsdv1 <sxsdv1@gmail.com>
Tue, 8 Jan 2019 18:22:26 +0000 (19:22 +0100)
committersxsdv1 <sxsdv1@gmail.com>
Sat, 12 Jan 2019 19:24:35 +0000 (20:24 +0100)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
test/web/activity_pub/activity_pub_controller_test.exs

index 73ca07e84a893c436e04d3122ec58c8de31943bb..6bc8b7195c720a167a21699cb66e668f6b4afe4e 100644 (file)
@@ -204,6 +204,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
     end
   end
 
+  def handle_user_activity(user, %{"type" => "Like"} = params) do
+    with %Object{} = object <- Object.normalize(params["object"]),
+         {:ok, activity, _object} <- ActivityPub.like(user, object) do
+      {:ok, activity}
+    else
+      _ -> {:error, "Can't like object"}
+    end
+  end
+
   def handle_user_activity(_, _) do
     {:error, "Unhandled activity type"}
   end
index 7aed8c71d812f0278ebcc75e5d43eb50e011e614..82ad4214454ee142384f4d794cee4dcce6932ff6 100644 (file)
@@ -292,6 +292,31 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
 
       assert json_response(conn, 400)
     end
+
+    test "it increases like count when receiving a like action", %{conn: conn} do
+      note_activity = insert(:note_activity)
+      user = User.get_cached_by_ap_id(note_activity.data["actor"])
+
+      data = %{
+        type: "Like",
+        object: %{
+          id: note_activity.data["object"]["id"]
+        }
+      }
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> put_req_header("content-type", "application/activity+json")
+        |> post("/users/#{user.nickname}/outbox", data)
+
+      result = json_response(conn, 201)
+      assert Activity.get_by_ap_id(result["id"])
+
+      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+      assert object
+      assert object.data["like_count"] == 1
+    end
   end
 
   describe "/users/:nickname/followers" do