summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200323122421_mrf_config_move_from_instance_namespace.exs
blob: ef36c4eb7db070044b6a3186688271b3c76640db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
defmodule Pleroma.Repo.Migrations.MrfConfigMoveFromInstanceNamespace do
  use Ecto.Migration

  alias Pleroma.ConfigDB

  @old_keys [:rewrite_policy, :mrf_transparency, :mrf_transparency_exclusions]
  def change do
    config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})

    if config do
      mrf =
        config.value
        |> Keyword.take(@old_keys)
        |> Keyword.new(fn
          {:rewrite_policy, policies} -> {:policies, policies}
          {:mrf_transparency, transparency} -> {:transparency, transparency}
          {:mrf_transparency_exclusions, exclusions} -> {:transparency_exclusions, exclusions}
        end)

      if mrf != [] do
        {:ok, _} =
          %ConfigDB{}
          |> ConfigDB.changeset(%{group: :pleroma, key: :mrf, value: mrf})
          |> Pleroma.Repo.insert()

        new_instance = Keyword.drop(config.value, @old_keys)

        if new_instance != [] do
          {:ok, _} =
            config
            |> ConfigDB.changeset(%{value: new_instance})
            |> Pleroma.Repo.update()
        else
          {:ok, _} = ConfigDB.delete(config)
        end
      end
    end
  end
end