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