logo

pleroma

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

20200802170532_fix_legacy_tags.exs (1226B)


  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. # Fix legacy tags set by AdminFE that don't align with TagPolicy MRF
  5. defmodule Pleroma.Repo.Migrations.FixLegacyTags do
  6. use Ecto.Migration
  7. alias Pleroma.Repo
  8. alias Pleroma.User
  9. import Ecto.Query
  10. @old_new_map %{
  11. "force_nsfw" => "mrf_tag:media-force-nsfw",
  12. "strip_media" => "mrf_tag:media-strip",
  13. "force_unlisted" => "mrf_tag:force-unlisted",
  14. "sandbox" => "mrf_tag:sandbox",
  15. "disable_remote_subscription" => "mrf_tag:disable-remote-subscription",
  16. "disable_any_subscription" => "mrf_tag:disable-any-subscription"
  17. }
  18. def change do
  19. legacy_tags = Map.keys(@old_new_map)
  20. from(u in User,
  21. where: fragment("? && ?", u.tags, ^legacy_tags),
  22. select: struct(u, [:tags, :id])
  23. )
  24. |> Repo.chunk_stream(100)
  25. |> Enum.each(fn user ->
  26. fix_tags_changeset(user)
  27. |> Repo.update()
  28. end)
  29. end
  30. defp fix_tags_changeset(%User{tags: tags} = user) do
  31. new_tags =
  32. Enum.map(tags, fn tag ->
  33. Map.get(@old_new_map, tag, tag)
  34. end)
  35. Ecto.Changeset.change(user, tags: new_tags)
  36. end
  37. end