logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

20200428221338_insert_skeletons_for_deleted_users.exs (1435B)


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