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