logo

pleroma

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

20200806175913_rename_instance_chat.exs (2700B)


  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.RenameInstanceChat do
  5. use Ecto.Migration
  6. alias Pleroma.ConfigDB
  7. @instance_params %{group: :pleroma, key: :instance}
  8. @shout_params %{group: :pleroma, key: :shout}
  9. @chat_params %{group: :pleroma, key: :chat}
  10. def up do
  11. instance_updated? = maybe_update_instance_key(:up) != :noop
  12. chat_updated? = maybe_update_chat_key(:up) != :noop
  13. case Enum.any?([instance_updated?, chat_updated?]) do
  14. true -> :ok
  15. false -> :noop
  16. end
  17. end
  18. def down do
  19. instance_updated? = maybe_update_instance_key(:down) != :noop
  20. chat_updated? = maybe_update_chat_key(:down) != :noop
  21. case Enum.any?([instance_updated?, chat_updated?]) do
  22. true -> :ok
  23. false -> :noop
  24. end
  25. end
  26. # pleroma.instance.chat_limit -> pleroma.shout.limit
  27. defp maybe_update_instance_key(:up) do
  28. with %ConfigDB{value: values} <- ConfigDB.get_by_params(@instance_params),
  29. limit when is_integer(limit) <- values[:chat_limit] do
  30. @shout_params |> Map.put(:value, limit: limit) |> ConfigDB.update_or_create()
  31. @instance_params |> Map.put(:subkeys, [":chat_limit"]) |> ConfigDB.delete()
  32. else
  33. _ ->
  34. :noop
  35. end
  36. end
  37. # pleroma.shout.limit -> pleroma.instance.chat_limit
  38. defp maybe_update_instance_key(:down) do
  39. with %ConfigDB{value: values} <- ConfigDB.get_by_params(@shout_params),
  40. limit when is_integer(limit) <- values[:limit] do
  41. @instance_params |> Map.put(:value, chat_limit: limit) |> ConfigDB.update_or_create()
  42. @shout_params |> Map.put(:subkeys, [":limit"]) |> ConfigDB.delete()
  43. else
  44. _ ->
  45. :noop
  46. end
  47. end
  48. # pleroma.chat.enabled -> pleroma.shout.enabled
  49. defp maybe_update_chat_key(:up) do
  50. with %ConfigDB{value: values} <- ConfigDB.get_by_params(@chat_params),
  51. enabled? when is_boolean(enabled?) <- values[:enabled] do
  52. @shout_params |> Map.put(:value, enabled: enabled?) |> ConfigDB.update_or_create()
  53. @chat_params |> Map.put(:subkeys, [":enabled"]) |> ConfigDB.delete()
  54. else
  55. _ ->
  56. :noop
  57. end
  58. end
  59. # pleroma.shout.enabled -> pleroma.chat.enabled
  60. defp maybe_update_chat_key(:down) do
  61. with %ConfigDB{value: values} <- ConfigDB.get_by_params(@shout_params),
  62. enabled? when is_boolean(enabled?) <- values[:enabled] do
  63. @chat_params |> Map.put(:value, enabled: enabled?) |> ConfigDB.update_or_create()
  64. @shout_params |> Map.put(:subkeys, [":enabled"]) |> ConfigDB.delete()
  65. else
  66. _ ->
  67. :noop
  68. end
  69. end
  70. end