...
 
Commits (2)
......@@ -118,6 +118,8 @@
streaming_enabled: true,
public_endpoint: nil
config :tzdata, :autoupdate, :disabled
if File.exists?("./config/test.secret.exs") do
import_config "test.secret.exs"
else
......
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Config.Utils do
alias Pleroma.Config
def instance_name, do: Config.get([:instance, :name])
defp instance_notify_email do
Config.get([:instance, :notify_email]) || Config.get([:instance, :email])
end
def sender do
{instance_name(), instance_notify_email()}
end
end
......@@ -11,7 +11,7 @@ defmodule Pleroma.User.WelcomeEmail do
alias Pleroma.Emails
alias Pleroma.User
import Pleroma.Config.Utils, only: [instance_name: 0]
import Pleroma.Config.Helpers, only: [instance_name: 0]
@spec enabled?() :: boolean()
def enabled?, do: Config.get([:welcome, :email, :enabled], false)
......
defmodule Pleroma.Repo.Migrations.MoveWelcomeSettings do
use Ecto.Migration
alias Pleroma.ConfigDB
@old_keys [:welcome_user_nickname, :welcome_message]
def up do
with {:ok, config, {keep_values, move_values}} <- get_old_values() do
insert_welcome_settings(move_values)
update_instance_config(config, keep_values)
end
end
def down do
with {:ok, welcome_config, revert_values} <- get_revert_values() do
revert_instance_config(revert_values)
Pleroma.Repo.delete(welcome_config)
end
end
defp insert_welcome_settings([_ | _] = values) do
unless String.trim(values[:welcome_message]) == "" do
config_values = [
direct_message: %{
enabled: true,
sender_nickname: values[:welcome_user_nickname],
message: values[:welcome_message]
},
email: %{
enabled: false,
sender: nil,
subject: "Welcome to <%= instance_name %>",
html: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>"
}
]
{:ok, _} =
%ConfigDB{}
|> ConfigDB.changeset(%{group: :pleroma, key: :welcome, value: config_values})
|> Pleroma.Repo.insert()
end
:ok
end
defp insert_welcome_settings(_), do: :noop
defp revert_instance_config(%{} = revert_values) do
values = [
welcome_user_nickname: revert_values[:sender_nickname],
welcome_message: revert_values[:message]
]
ConfigDB.update_or_create(%{group: :pleroma, key: :instance, value: values})
end
defp revert_instance_config(_), do: :noop
defp update_instance_config(config, values) do
{:ok, _} =
config
|> ConfigDB.changeset(%{value: values})
|> Pleroma.Repo.update()
:ok
end
defp get_revert_values do
config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
cond do
is_nil(config) -> {:noop, nil, nil}
true -> {:ok, config, config.value[:direct_message]}
end
end
defp get_old_values do
config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
cond do
is_nil(config) ->
{:noop, config, {}}
is_binary(config.value[:welcome_message]) ->
{:ok, config,
{Keyword.drop(config.value, @old_keys), Keyword.take(config.value, @old_keys)}}
true ->
{:ok, config, {Keyword.drop(config.value, @old_keys), []}}
end
end
end
defmodule Pleroma.Repo.Migrations.MoveWelcomeSettingsTest do
use Pleroma.DataCase
import Pleroma.Factory
import Pleroma.Tests.Helpers
alias Pleroma.ConfigDB
setup_all do: require_migration("20200724133313_move_welcome_settings")
describe "up/0" do
test "converts welcome settings", %{migration: migration} do
insert(:config,
group: :pleroma,
key: :instance,
value: [
welcome_message: "Test message",
welcome_user_nickname: "jimm",
name: "Pleroma"
]
)
migration.up()
instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
welcome_config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
assert instance_config.value == [name: "Pleroma"]
assert welcome_config.value == [
direct_message: %{
enabled: true,
message: "Test message",
sender_nickname: "jimm"
},
email: %{
enabled: false,
html: "Welcome to <%= instance_name %>",
sender: nil,
subject: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>"
}
]
end
test "does nothing when message empty", %{migration: migration} do
insert(:config,
group: :pleroma,
key: :instance,
value: [
welcome_message: "",
welcome_user_nickname: "jimm",
name: "Pleroma"
]
)
migration.up()
instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
assert instance_config.value == [name: "Pleroma"]
end
test "does nothing when welcome_message not set", %{migration: migration} do
insert(:config,
group: :pleroma,
key: :instance,
value: [welcome_user_nickname: "jimm", name: "Pleroma"]
)
migration.up()
instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
assert instance_config.value == [name: "Pleroma"]
end
end
describe "down/0" do
test "revert new settings to old when instance setting not exists", %{migration: migration} do
insert(:config,
group: :pleroma,
key: :welcome,
value: [
direct_message: %{
enabled: true,
message: "Test message",
sender_nickname: "jimm"
},
email: %{
enabled: false,
html: "Welcome to <%= instance_name %>",
sender: nil,
subject: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>"
}
]
)
migration.down()
refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
assert instance_config.value == [
welcome_user_nickname: "jimm",
welcome_message: "Test message"
]
end
test "revert new settings to old when instance setting exists", %{migration: migration} do
insert(:config, group: :pleroma, key: :instance, value: [name: "Pleroma App"])
insert(:config,
group: :pleroma,
key: :welcome,
value: [
direct_message: %{
enabled: true,
message: "Test message",
sender_nickname: "jimm"
},
email: %{
enabled: false,
html: "Welcome to <%= instance_name %>",
sender: nil,
subject: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>"
}
]
)
migration.down()
refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
assert instance_config.value == [
name: "Pleroma App",
welcome_user_nickname: "jimm",
welcome_message: "Test message"
]
end
end
end