1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Repo.Migrations.MoveWelcomeSettings do
10 @old_keys [:welcome_user_nickname, :welcome_message]
13 with {:ok, config, {keep_values, move_values}} <- get_old_values() do
14 insert_welcome_settings(move_values)
15 update_instance_config(config, keep_values)
20 with {:ok, welcome_config, revert_values} <- get_revert_values() do
21 revert_instance_config(revert_values)
22 Pleroma.Repo.delete(welcome_config)
26 defp insert_welcome_settings([_ | _] = values) do
27 unless String.trim(values[:welcome_message]) == "" do
31 sender_nickname: values[:welcome_user_nickname],
32 message: values[:welcome_message]
37 subject: "Welcome to <%= instance_name %>",
38 html: "Welcome to <%= instance_name %>",
39 text: "Welcome to <%= instance_name %>"
45 |> ConfigDB.changeset(%{group: :pleroma, key: :welcome, value: config_values})
46 |> Pleroma.Repo.insert()
52 defp insert_welcome_settings(_), do: :noop
54 defp revert_instance_config(%{} = revert_values) do
56 welcome_user_nickname: revert_values[:sender_nickname],
57 welcome_message: revert_values[:message]
60 ConfigDB.update_or_create(%{group: :pleroma, key: :instance, value: values})
63 defp revert_instance_config(_), do: :noop
65 defp update_instance_config(config, values) do
68 |> ConfigDB.changeset(%{value: values})
69 |> Pleroma.Repo.update()
74 defp get_revert_values do
75 config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
78 is_nil(config) -> {:noop, nil, nil}
79 true -> {:ok, config, config.value[:direct_message]}
83 defp get_old_values do
84 config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
90 is_binary(config.value[:welcome_message]) ->
92 {Keyword.drop(config.value, @old_keys), Keyword.take(config.value, @old_keys)}}
95 {:ok, config, {Keyword.drop(config.value, @old_keys), []}}