Merge branch 'backups' into 'develop'

Include following/followers in backups

See merge request pleroma/pleroma!4085
This commit is contained in:
feld 2024-03-15 19:32:35 +00:00
commit 0b5bba23ce
3 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1 @@
Include following/followers in backups

View File

@ -196,7 +196,14 @@ defp wait_backup(backup, current_processed, task) do
end
end
@files ['actor.json', 'outbox.json', 'likes.json', 'bookmarks.json']
@files [
'actor.json',
'outbox.json',
'likes.json',
'bookmarks.json',
'followers.json',
'following.json'
]
@spec export(Pleroma.User.Backup.t(), pid()) :: {:ok, String.t()} | :error
def export(%__MODULE__{} = backup, caller_pid) do
backup = Repo.preload(backup, :user)
@ -207,6 +214,8 @@ def export(%__MODULE__{} = backup, caller_pid) do
:ok <- statuses(dir, backup.user, caller_pid),
:ok <- likes(dir, backup.user, caller_pid),
:ok <- bookmarks(dir, backup.user, caller_pid),
:ok <- followers(dir, backup.user, caller_pid),
:ok <- following(dir, backup.user, caller_pid),
{:ok, zip_path} <- :zip.create(backup.file_name, @files, cwd: dir),
{:ok, _} <- File.rm_rf(dir) do
{:ok, zip_path}
@ -357,6 +366,16 @@ defp statuses(dir, user, caller_pid) do
caller_pid
)
end
defp followers(dir, user, caller_pid) do
User.get_followers_query(user)
|> write(dir, "followers", fn a -> {:ok, a.ap_id} end, caller_pid)
end
defp following(dir, user, caller_pid) do
User.get_friends_query(user)
|> write(dir, "following", fn a -> {:ok, a.ap_id} end, caller_pid)
end
end
defmodule Pleroma.User.Backup.ProcessorAPI do

View File

@ -166,6 +166,7 @@ test "it removes outdated backups after creating a fresh one" do
test "it creates a zip archive with user data" do
user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
%{ap_id: other_ap_id} = other_user = insert(:user)
{:ok, %{object: %{data: %{"id" => id1}}} = status1} =
CommonAPI.post(user, %{status: "status1"})
@ -182,6 +183,8 @@ test "it creates a zip archive with user data" do
Bookmark.create(user.id, status2.id)
Bookmark.create(user.id, status3.id)
CommonAPI.follow(user, other_user)
assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
assert {:ok, path} = Backup.export(backup, self())
assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
@ -261,6 +264,16 @@ test "it creates a zip archive with user data" do
"type" => "OrderedCollection"
} = Jason.decode!(json)
assert {:ok, {'following.json', json}} = :zip.zip_get('following.json', zipfile)
assert %{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => "following.json",
"orderedItems" => [^other_ap_id],
"totalItems" => 1,
"type" => "OrderedCollection"
} = Jason.decode!(json)
:zip.zip_close(zipfile)
File.rm!(path)
end