Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3KB

  1. defmodule Pleroma.Repo.Migrations.InsertSkeletonsForDeletedUsers do
  2. use Ecto.Migration
  3. alias Pleroma.User
  4. alias Pleroma.Repo
  5. import Ecto.Query
  6. def change do
  7. Application.ensure_all_started(:flake_id)
  8. local_ap_id =
  9. User.Query.build(%{local: true})
  10. |> select([u], u.ap_id)
  11. |> limit(1)
  12. |> Repo.one()
  13. unless local_ap_id == nil do
  14. # Hack to get instance base url because getting it from Phoenix
  15. # would require starting the whole application
  16. instance_uri =
  17. local_ap_id
  18. |> URI.parse()
  19. |> Map.put(:query, nil)
  20. |> Map.put(:path, nil)
  21. |> URI.to_string()
  22. {:ok, %{rows: ap_ids}} =
  23. Ecto.Adapters.SQL.query(
  24. Repo,
  25. "select distinct unnest(nonexistent_locals.recipients) from activities, lateral (select array_agg(recipient) as recipients from unnest(activities.recipients) as recipient where recipient similar to '#{
  26. instance_uri
  27. }/users/[A-Za-z0-9]*' and not(recipient in (select ap_id from users))) nonexistent_locals;",
  28. [],
  29. timeout: :infinity
  30. )
  31. ap_ids
  32. |> Enum.each(fn [ap_id] ->
  33. Ecto.Changeset.change(%User{}, deactivated: true, ap_id: ap_id)
  34. |> Repo.insert()
  35. end)
  36. end
  37. end
  38. end