alias Pleroma.Repo
alias Pleroma.User
+alias Pleroma.Activity
import Ecto.Query
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()
|> 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.
|> 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,
)
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
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.User
+ alias Pleroma.Activity
+ alias Pleroma.Repo
import Pleroma.Factory
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)
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