9ec4203eb97dd543a1443b459dbb5cfcbd88f52a
[akkoma] / priv / repo / migrations / 20200716195806_autolinker_to_linkify.exs
1 defmodule Pleroma.Repo.Migrations.AutolinkerToLinkify do
2 use Ecto.Migration
3
4 alias Pleroma.Repo
5 alias Pleroma.ConfigDB
6
7 @autolinker_path %{group: :auto_linker, key: :opts}
8 @linkify_path %{group: :pleroma, key: Pleroma.Formatter}
9
10 @compat_opts [:class, :rel, :new_window, :truncate, :strip_prefix, :extra]
11
12 def change do
13 with {:ok, {old, new}} <- maybe_get_params() do
14 move_config(old, new)
15 end
16 end
17
18 defp move_config(%{} = old, %{} = new) do
19 {:ok, _} = ConfigDB.update_or_create(new)
20 {:ok, _} = ConfigDB.delete(old)
21 :ok
22 end
23
24 defp maybe_get_params() do
25 with %ConfigDB{value: opts} <- ConfigDB.get_by_params(@autolinker_path),
26 %{} = opts <- transform_opts(opts),
27 %{} = linkify_params <- Map.put(@linkify_path, :value, opts) do
28 {:ok, {@autolinker_path, linkify_params}}
29 end
30 end
31
32 defp transform_opts(opts) when is_list(opts) do
33 opts
34 |> Enum.into(%{})
35 |> Map.take(@compat_opts)
36 end
37 end