Add account export

This commit is contained in:
Egor Kislitsyn 2020-08-19 15:31:33 +04:00
parent f9ece1a7f6
commit 257e059e61
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
1 changed files with 118 additions and 0 deletions

118
lib/pleroma/export.ex Normal file
View File

@ -0,0 +1,118 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Export do
alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.UserView
import Ecto.Query
def run(user) do
with {:ok, dir} <- create_dir(),
:ok <- actor(dir, user),
:ok <- statuses(dir, user),
:ok <- likes(dir, user),
:ok <- bookmarks(dir, user) do
IO.inspect({"DONE", dir})
else
err -> IO.inspect({"export error", err})
end
end
def actor(dir, user) do
with {:ok, json} <-
UserView.render("user.json", %{user: user})
|> Map.merge(%{"likes" => "likes.json", "bookmarks" => "bookmarks.json"})
|> Jason.encode() do
File.write(dir <> "/actor.json", json)
end
end
defp create_dir do
datetime = Calendar.NaiveDateTime.Format.iso8601_basic(NaiveDateTime.utc_now())
dir = Path.join(System.tmp_dir!(), "archive-" <> datetime)
with :ok <- File.mkdir(dir), do: {:ok, dir}
end
defp write_header(file, name) do
IO.write(
file,
"""
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "#{name}.json",
"type": "OrderedCollection",
"orderedItems": [
"""
)
end
defp write(query, dir, name, fun) do
path = dir <> "/#{name}.json"
with {:ok, file} <- File.open(path, [:write, :utf8]),
:ok <- write_header(file, name) do
counter = :counters.new(1, [])
query
|> Pleroma.RepoStreamer.chunk_stream(100)
|> Stream.each(fn items ->
Enum.each(items, fn i ->
with {:ok, str} <- fun.(i),
:ok <- IO.write(file, str <> ",\n") do
:counters.add(counter, 1, 1)
end
end)
end)
|> Stream.run()
total = :counters.get(counter, 1)
with :ok <- :file.pwrite(file, {:eof, -2}, "\n],\n \"totalItems\": #{total}}") do
File.close(file)
end
end
end
def bookmarks(dir, %{id: user_id} = _user) do
Bookmark
|> where(user_id: ^user_id)
|> join(:inner, [b], activity in assoc(b, :activity))
|> select([b, a], %{id: b.id, object: fragment("(?)->>'object'", a.data)})
|> write(dir, "bookmarks", fn a -> {:ok, "\"#{a.object}\""} end)
end
def likes(dir, user) do
user.ap_id
|> Activity.Queries.by_actor()
|> Activity.Queries.by_type("Like")
|> select([like], %{id: like.id, object: fragment("(?)->>'object'", like.data)})
|> write(dir, "likes", fn a -> {:ok, "\"#{a.object}\""} end)
end
def statuses(dir, user) do
opts =
%{}
|> Map.put(:type, ["Create", "Announce"])
|> Map.put(:blocking_user, user)
|> Map.put(:muting_user, user)
|> Map.put(:reply_filtering_user, user)
|> Map.put(:announce_filtering_user, user)
|> Map.put(:user, user)
[[user.ap_id], User.following(user), Pleroma.List.memberships(user)]
|> Enum.concat()
|> ActivityPub.fetch_activities_query(opts)
|> write(dir, "outbox", fn a ->
with {:ok, activity} <- Transmogrifier.prepare_outgoing(a.data) do
activity |> Map.delete("@context") |> Jason.encode()
end
end)
end
end