WIP: Add backfilling of posts #846
2 changed files with 23 additions and 29 deletions
|
@ -194,31 +194,24 @@ def get_or_fetch_by_key_id(key_id) do
|
|||
"""
|
||||
def fetch_remote_key(key_id) do
|
||||
Logger.debug("Fetching remote key: #{key_id}")
|
||||
resp = Pleroma.Object.Fetcher.fetch_and_contain_remote_object_from_id(key_id)
|
||||
|
||||
case resp do
|
||||
{:ok, _body} ->
|
||||
case handle_signature_response(resp) do
|
||||
{:ok, ap_id, public_key_pem} ->
|
||||
Logger.debug("Fetched remote key: #{ap_id}")
|
||||
# fetch the user
|
||||
{:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
|
||||
# store the key
|
||||
key = %__MODULE__{
|
||||
user_id: user.id,
|
||||
public_key: public_key_pem,
|
||||
key_id: key_id
|
||||
}
|
||||
with {:ok, _body} = resp <-
|
||||
Pleroma.Object.Fetcher.fetch_and_contain_remote_object_from_id(key_id),
|
||||
{:ok, ap_id, public_key_pem} <- handle_signature_response(resp) do
|
||||
Logger.debug("Fetched remote key: #{ap_id}")
|
||||
# fetch the user
|
||||
{:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
|
||||
# store the key
|
||||
key = %__MODULE__{
|
||||
user_id: user.id,
|
||||
public_key: public_key_pem,
|
||||
key_id: key_id
|
||||
}
|
||||
|
||||
Repo.insert(key, on_conflict: :replace_all, conflict_target: :key_id)
|
||||
|
||||
e ->
|
||||
Logger.debug("Failed to fetch remote key: #{inspect(e)}")
|
||||
{:error, "Could not fetch key"}
|
||||
end
|
||||
|
||||
_ ->
|
||||
Logger.debug("Failed to fetch remote key: #{inspect(resp)}")
|
||||
Repo.insert(key, on_conflict: :replace_all, conflict_target: :key_id)
|
||||
else
|
||||
e ->
|
||||
Logger.debug("Failed to fetch remote key: #{inspect(e)}")
|
||||
{:error, "Could not fetch key"}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,13 +8,14 @@ def up do
|
|||
# we do not handle remote users here!
|
||||
# because we want to store a key id -> user id mapping, and we don't
|
||||
# currently store key ids for remote users...
|
||||
query =
|
||||
from(u in User)
|
||||
|> where(local: true)
|
||||
|
||||
Repo.stream(query, timeout: :infinity)
|
||||
# Also this MUST use select, else the migration will fail in future installs with new user fields!
|
||||
from(u in Pleroma.User,
|
||||
where: u.local == true,
|
||||
select: {u.id, u.keys, u.ap_id}
|
||||
)
|
||||
|> Repo.stream(timeout: :infinity)
|
||||
|> Enum.each(fn
|
||||
%User{id: user_id, keys: private_key, local: true, ap_id: ap_id} ->
|
||||
{user_id, private_key, ap_id} ->
|
||||
IO.puts("Migrating user #{user_id}")
|
||||
# we can precompute the public key here...
|
||||
# we do use it on every user view which makes it a bit of a dos attack vector
|
||||
|
|
Loading…
Reference in a new issue