SPC fixes: Fix activities.
This commit is contained in:
parent
bd6c12592e
commit
aa8ddfdbe2
2 changed files with 45 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue