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