logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

20200724133313_move_welcome_settings.exs (2610B)


  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. defmodule Pleroma.Repo.Migrations.MoveWelcomeSettings do
  5. use Ecto.Migration
  6. alias Pleroma.ConfigDB
  7. @old_keys [:welcome_user_nickname, :welcome_message]
  8. def up do
  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)
  12. end
  13. end
  14. def down do
  15. with {:ok, welcome_config, revert_values} <- get_revert_values() do
  16. revert_instance_config(revert_values)
  17. Pleroma.Repo.delete(welcome_config)
  18. end
  19. end
  20. defp insert_welcome_settings([_ | _] = values) do
  21. unless String.trim(values[:welcome_message]) == "" do
  22. config_values = [
  23. direct_message: %{
  24. enabled: true,
  25. sender_nickname: values[:welcome_user_nickname],
  26. message: values[:welcome_message]
  27. },
  28. email: %{
  29. enabled: false,
  30. sender: nil,
  31. subject: "Welcome to <%= instance_name %>",
  32. html: "Welcome to <%= instance_name %>",
  33. text: "Welcome to <%= instance_name %>"
  34. }
  35. ]
  36. {:ok, _} =
  37. %ConfigDB{}
  38. |> ConfigDB.changeset(%{group: :pleroma, key: :welcome, value: config_values})
  39. |> Pleroma.Repo.insert()
  40. end
  41. :ok
  42. end
  43. defp insert_welcome_settings(_), do: :noop
  44. defp revert_instance_config(%{} = revert_values) do
  45. values = [
  46. welcome_user_nickname: revert_values[:sender_nickname],
  47. welcome_message: revert_values[:message]
  48. ]
  49. ConfigDB.update_or_create(%{group: :pleroma, key: :instance, value: values})
  50. end
  51. defp revert_instance_config(_), do: :noop
  52. defp update_instance_config(config, values) do
  53. {:ok, _} =
  54. config
  55. |> ConfigDB.changeset(%{value: values})
  56. |> Pleroma.Repo.update()
  57. :ok
  58. end
  59. defp get_revert_values do
  60. config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
  61. cond do
  62. is_nil(config) -> {:noop, nil, nil}
  63. true -> {:ok, config, config.value[:direct_message]}
  64. end
  65. end
  66. defp get_old_values do
  67. config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
  68. cond do
  69. is_nil(config) ->
  70. {:noop, config, {}}
  71. is_binary(config.value[:welcome_message]) ->
  72. {:ok, config,
  73. {Keyword.drop(config.value, @old_keys), Keyword.take(config.value, @old_keys)}}
  74. true ->
  75. {:ok, config, {Keyword.drop(config.value, @old_keys), []}}
  76. end
  77. end
  78. end