2022-02-25 23:11:42 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2022-02-25 23:11:42 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-07-21 16:18:17 -06:00
|
|
|
defmodule Pleroma.Repo.Migrations.AutolinkerToLinkify do
|
|
|
|
use Ecto.Migration
|
|
|
|
alias Pleroma.ConfigDB
|
|
|
|
|
|
|
|
@autolinker_path %{group: :auto_linker, key: :opts}
|
|
|
|
@linkify_path %{group: :pleroma, key: Pleroma.Formatter}
|
|
|
|
|
|
|
|
@compat_opts [:class, :rel, :new_window, :truncate, :strip_prefix, :extra]
|
|
|
|
|
|
|
|
def change do
|
|
|
|
with {:ok, {old, new}} <- maybe_get_params() do
|
|
|
|
move_config(old, new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp move_config(%{} = old, %{} = new) do
|
|
|
|
{:ok, _} = ConfigDB.update_or_create(new)
|
|
|
|
{:ok, _} = ConfigDB.delete(old)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_get_params() do
|
|
|
|
with %ConfigDB{value: opts} <- ConfigDB.get_by_params(@autolinker_path),
|
2020-07-22 10:27:52 -06:00
|
|
|
opts <- transform_opts(opts),
|
2020-07-21 16:18:17 -06:00
|
|
|
%{} = linkify_params <- Map.put(@linkify_path, :value, opts) do
|
|
|
|
{:ok, {@autolinker_path, linkify_params}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-22 12:10:10 -06:00
|
|
|
def transform_opts(opts) when is_list(opts) do
|
2020-07-21 16:18:17 -06:00
|
|
|
opts
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|> Map.take(@compat_opts)
|
2020-07-22 10:27:52 -06:00
|
|
|
|> Map.to_list()
|
2020-07-21 16:18:17 -06:00
|
|
|
end
|
|
|
|
end
|