SideEffects: Handle user updating.
authorlain <lain@soykaf.club>
Mon, 22 Jun 2020 11:15:37 +0000 (13:15 +0200)
committerlain <lain@soykaf.club>
Mon, 22 Jun 2020 11:15:37 +0000 (13:15 +0200)
lib/pleroma/web/activity_pub/side_effects.ex
test/web/activity_pub/side_effects_test.exs

index 1a1cc675cc3e5b0909c44e95157702c2666e4041..09fd7d7c9d7450bcec2004f5d008561ab4c7bee8 100644 (file)
@@ -20,6 +20,18 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
 
   def handle(object, meta \\ [])
 
+  # Tasks this handles:
+  # Update the user
+  def handle(%{data: %{"type" => "Update", "object" => updated_object}} = object, meta) do
+    {:ok, new_user_data} = ActivityPub.user_data_from_user_object(updated_object)
+
+    User.get_by_ap_id(updated_object["id"])
+    |> User.remote_user_changeset(new_user_data)
+    |> User.update_and_set_cache()
+
+    {:ok, object, meta}
+  end
+
   # Tasks this handles:
   # - Add like to object
   # - Set up notification
index 6bbbaae87c620aa0e9bb66e7e858087cb9f492a4..1d7c2736b6ecc0c81a0adbf7d2ccd42516fa6901 100644 (file)
@@ -64,6 +64,22 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
     end
   end
 
+  describe "update users" do
+    setup do
+      user = insert(:user)
+      {:ok, update_data, []} = Builder.update(user, %{"id" => user.ap_id, "name" => "new name!"})
+      {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
+
+      %{user: user, update_data: update_data, update: update}
+    end
+
+    test "it updates the user", %{user: user, update: update} do
+      {:ok, _, _} = SideEffects.handle(update)
+      user = User.get_by_id(user.id)
+      assert user.name == "new name!"
+    end
+  end
+
   describe "delete objects" do
     setup do
       user = insert(:user)