From aa8ddfdbe2303375e3f019faca30a620bfc58fc7 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 25 Jan 2019 17:55:24 +0100 Subject: [PATCH] SPC fixes: Fix activities. --- lib/pleroma/spc_fixes/spc_fixes.ex | 31 +++++++++++++++++++++++++++++- test/spc_fixes_test.exs | 16 ++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/spc_fixes/spc_fixes.ex b/lib/pleroma/spc_fixes/spc_fixes.ex index e937d5871..41cf56fdd 100644 --- a/lib/pleroma/spc_fixes/spc_fixes.ex +++ b/lib/pleroma/spc_fixes/spc_fixes.ex @@ -4,6 +4,7 @@ alias Pleroma.Repo alias Pleroma.User +alias Pleroma.Activity import Ecto.Query defmodule Pleroma.SpcFixes do @@ -15,6 +16,7 @@ def upgrade_users 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 @@ def upgrade_users 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 @@ def upgrade_users 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 @@ def upgrade_users 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 diff --git a/test/spc_fixes_test.exs b/test/spc_fixes_test.exs index 01629e374..76c081248 100644 --- a/test/spc_fixes_test.exs +++ b/test/spc_fixes_test.exs @@ -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 @@ test "resets the ap_id and follower_address of old spc users" 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 @@ test "resets the ap_id and follower_address of old spc users" 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