1 defmodule Pleroma.Repo.Migrations.MoveWelcomeSettings do
6 @old_keys [:welcome_user_nickname, :welcome_message]
9 with {:ok, config, {keep_values, move_values}} <- get_old_values() do
10 insert_welcome_settings(move_values)
11 update_instance_config(config, keep_values)
16 with {:ok, welcome_config, revert_values} <- get_revert_values() do
17 revert_instance_config(revert_values)
18 Pleroma.Repo.delete(welcome_config)
22 defp insert_welcome_settings([_ | _] = values) do
23 unless String.trim(values[:welcome_message]) == "" do
27 sender_nickname: values[:welcome_user_nickname],
28 message: values[:welcome_message]
33 subject: "Welcome to <%= instance_name %>",
34 html: "Welcome to <%= instance_name %>",
35 text: "Welcome to <%= instance_name %>"
41 |> ConfigDB.changeset(%{group: :pleroma, key: :welcome, value: config_values})
42 |> Pleroma.Repo.insert()
48 defp insert_welcome_settings(_), do: :noop
50 defp revert_instance_config(%{} = revert_values) do
52 welcome_user_nickname: revert_values[:sender_nickname],
53 welcome_message: revert_values[:message]
56 ConfigDB.update_or_create(%{group: :pleroma, key: :instance, value: values})
59 defp revert_instance_config(_), do: :noop
61 defp update_instance_config(config, values) do
64 |> ConfigDB.changeset(%{value: values})
65 |> Pleroma.Repo.update()
70 defp get_revert_values do
71 config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
74 is_nil(config) -> {:noop, nil, nil}
75 true -> {:ok, config, config.value[:direct_message]}
79 defp get_old_values do
80 config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
86 is_binary(config.value[:welcome_message]) ->
88 {Keyword.drop(config.value, @old_keys), Keyword.take(config.value, @old_keys)}}
91 {:ok, config, {Keyword.drop(config.value, @old_keys), []}}