SPC fixes: Fix activities.
authorlain <lain@soykaf.club>
Fri, 25 Jan 2019 16:55:24 +0000 (17:55 +0100)
committerlain <lain@soykaf.club>
Fri, 25 Jan 2019 17:14:48 +0000 (18:14 +0100)
lib/pleroma/spc_fixes/spc_fixes.ex
test/spc_fixes_test.exs

index e937d58714cf3fb0b89ca130888d6076e03aeb98..41cf56fdd60925a696df1fec4dedde2e4ed68650 100644 (file)
@@ -4,6 +4,7 @@
 
 alias Pleroma.Repo
 alias Pleroma.User
+alias Pleroma.Activity
 import Ecto.Query
 
 defmodule Pleroma.SpcFixes do
@@ -15,6 +16,7 @@ defmodule Pleroma.SpcFixes do
 
     {:ok, file} = File.read("lib/pleroma/spc_fixes/users_conversion.txt")
 
+    # Mapping of old ap_id to new ap_id and vice reversa
     mapping =
       file
       |> String.trim()
@@ -24,7 +26,9 @@ defmodule Pleroma.SpcFixes do
         |> String.split("\t")
       end)
       |> Enum.reduce(%{}, fn [_id, old_ap_id, new_ap_id], acc ->
-        Map.put(acc, old_ap_id, String.trim(new_ap_id))
+        acc
+        |> Map.put(String.trim(old_ap_id), String.trim(new_ap_id))
+        |> Map.put(String.trim(new_ap_id), String.trim(old_ap_id))
       end)
 
     # First, refetch all the old users.
@@ -49,6 +53,7 @@ defmodule Pleroma.SpcFixes do
     |> Enum.each(fn user ->
       old_follower_address = User.ap_followers(user)
 
+      # Fix users
       query =
         from(u in User,
           where: ^old_follower_address in u.following,
@@ -58,6 +63,30 @@ defmodule Pleroma.SpcFixes do
         )
 
       Repo.update_all(query, [])
+
+      # Fix activities
+      query =
+        from(a in Activity,
+          where: fragment("?->>'actor' = ?", a.data, ^mapping[user.ap_id]),
+          update: [
+            set: [
+              data:
+                fragment(
+                  "jsonb_set(jsonb_set(?, '{actor}', ?), '{to}', (?->'to')::jsonb || ?)",
+                  a.data,
+                  ^user.ap_id,
+                  a.data,
+                  ^[user.follower_address]
+                ),
+              actor: ^user.ap_id
+            ],
+            push: [
+              recipients: ^user.follower_address
+            ]
+          ]
+        )
+
+      Repo.update_all(query, [])
     end)
   end
 end
index 01629e374e4b1335f522bde749d9d78f289d8ee6..76c081248ed7fce64c3154d71ba9586b4a013972 100644 (file)
@@ -9,6 +9,8 @@ defmodule Pleroma.SpcFixesTest do
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.User
+  alias Pleroma.Activity
+  alias Pleroma.Repo
 
   import Pleroma.Factory
 
@@ -33,6 +35,7 @@ defmodule Pleroma.SpcFixesTest do
     other_user = insert(:user)
     {:ok, other_user} = User.follow(other_user, user)
     {:ok, activity} = CommonAPI.post(user, %{"status" => "blabla"})
+    {:ok, _other_activity} = CommonAPI.post(other_user, %{"status" => "blabla"})
 
     assert User.following?(other_user, user)
     assert [activity] == ActivityPub.fetch_activities(other_user.following)
@@ -45,8 +48,19 @@ defmodule Pleroma.SpcFixesTest do
     assert user.ap_id == "https://shitposter.club/users/zep"
     assert user.follower_address == "https://shitposter.club/users/zep/followers"
 
+    aid = activity.id
     # Activites and following are correctly stitched.
     assert User.following?(other_user, user)
-    assert [activity] == ActivityPub.fetch_activities(other_user.following) |> IO.inspect()
+    assert [%{id: ^aid}] = ActivityPub.fetch_activities(other_user.following)
+
+    third_user = insert(:user)
+    {:ok, third_user} = User.follow(third_user, user)
+    assert [%{id: ^aid}] = ActivityPub.fetch_activities(third_user.following)
+
+    activity = Repo.get(Activity, aid)
+
+    assert activity.data["actor"] == user.ap_id
+    assert user.follower_address in activity.recipients
+    assert user.follower_address in activity.data["to"]
   end
 end