pluralise settings
This commit is contained in:
parent
56e3d11807
commit
8a3b7a3757
8 changed files with 125 additions and 58 deletions
|
@ -1,4 +1,4 @@
|
|||
defmodule Pleroma.Akkoma.FrontendSettingProfile do
|
||||
defmodule Pleroma.Akkoma.FrontendSettingsProfile do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
@ -84,12 +84,13 @@ defp validate_settings_length(
|
|||
|
||||
defp validate_version(changeset, %{version: nil}), do: changeset
|
||||
|
||||
defp validate_version(%Ecto.Changeset{changes: %{version: version}} = changeset, %{version: prev_version}) do
|
||||
defp validate_version(%Ecto.Changeset{changes: %{version: version}} = changeset, %{
|
||||
version: prev_version
|
||||
}) do
|
||||
if version != prev_version + 1 do
|
||||
add_error(changeset, :version, "must be incremented by 1")
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -165,7 +165,7 @@ defmodule Pleroma.User do
|
|||
has_many(:outgoing_relationships, UserRelationship, foreign_key: :source_id)
|
||||
has_many(:incoming_relationships, UserRelationship, foreign_key: :target_id)
|
||||
|
||||
has_many(:frontend_profiles, Pleroma.Akkoma.FrontendSettingProfile)
|
||||
has_many(:frontend_profiles, Pleroma.Akkoma.FrontendSettingsProfile)
|
||||
|
||||
for {relationship_type,
|
||||
[
|
||||
|
|
|
@ -2,16 +2,18 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
|
|||
use Pleroma.Web, :controller
|
||||
|
||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||
alias Pleroma.Akkoma.FrontendSettingProfile
|
||||
alias Pleroma.Akkoma.FrontendSettingsProfile
|
||||
|
||||
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
||||
plug(
|
||||
OAuthScopesPlug,
|
||||
%{@unauthenticated_access | scopes: ["read:accounts"]}
|
||||
when action in [
|
||||
:list_profiles, :get_profile
|
||||
:list_profiles,
|
||||
:get_profile
|
||||
]
|
||||
)
|
||||
|
||||
plug(
|
||||
OAuthScopesPlug,
|
||||
%{@unauthenticated_access | scopes: ["write:accounts"]}
|
||||
|
@ -27,9 +29,17 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
|
|||
|
||||
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
|
||||
def get_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name}) do
|
||||
with %FrontendSettingProfile{} = profile <- FrontendSettingProfile.get_by_user_and_frontend_name_and_profile_name(conn.assigns.user, frontend_name, profile_name) do
|
||||
with %FrontendSettingsProfile{} = profile <-
|
||||
FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
|
||||
conn.assigns.user,
|
||||
frontend_name,
|
||||
profile_name
|
||||
) do
|
||||
conn
|
||||
|> json(profile.settings)
|
||||
|> json(%{
|
||||
settings: profile.settings,
|
||||
version: profile.version
|
||||
})
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
|
@ -37,15 +47,32 @@ def get_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name
|
|||
|
||||
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name"
|
||||
def list_profiles(conn, %{frontend_name: frontend_name}) do
|
||||
with profiles <- FrontendSettingProfile.get_all_by_user_and_frontend_name(conn.assigns.user, frontend_name),
|
||||
data <- Enum.map(profiles, fn profile -> profile.profile_name end) do
|
||||
with profiles <-
|
||||
FrontendSettingsProfile.get_all_by_user_and_frontend_name(
|
||||
conn.assigns.user,
|
||||
frontend_name
|
||||
),
|
||||
data <-
|
||||
Enum.map(profiles, fn profile ->
|
||||
%{name: profile.profile_name, version: profile.version}
|
||||
end) do
|
||||
json(conn, data)
|
||||
end
|
||||
end
|
||||
|
||||
@doc "PUT /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
|
||||
def update_profile(%{body_params: %{settings: settings, version: version}} = conn, %{frontend_name: frontend_name, profile_name: profile_name}) do
|
||||
with {:ok, profile} <- FrontendSettingProfile.create_or_update(conn.assigns.user, frontend_name, profile_name, settings, version) do
|
||||
def update_profile(%{body_params: %{settings: settings, version: version}} = conn, %{
|
||||
frontend_name: frontend_name,
|
||||
profile_name: profile_name
|
||||
}) do
|
||||
with {:ok, profile} <-
|
||||
FrontendSettingsProfile.create_or_update(
|
||||
conn.assigns.user,
|
||||
frontend_name,
|
||||
profile_name,
|
||||
settings,
|
||||
version
|
||||
) do
|
||||
conn
|
||||
|> json(profile.settings)
|
||||
end
|
||||
|
|
|
@ -20,7 +20,10 @@ def list_profiles_operation() do
|
|||
security: [%{"oAuth" => ["read:accounts"]}],
|
||||
responses: %{
|
||||
200 =>
|
||||
Operation.response("Profiles", "application/json", %Schema{type: :array, items: %Schema{type: :string}})
|
||||
Operation.response("Profiles", "application/json", %Schema{
|
||||
type: :array,
|
||||
items: %Schema{type: :string}
|
||||
})
|
||||
}
|
||||
}
|
||||
end
|
||||
|
@ -35,10 +38,8 @@ def get_profile_operation() do
|
|||
security: [%{"oAuth" => ["read:accounts"]}],
|
||||
parameters: [frontend_name_param(), profile_name_param()],
|
||||
responses: %{
|
||||
200 =>
|
||||
Operation.response("Translation", "application/json", %Schema{type: :object}),
|
||||
404 =>
|
||||
Operation.response("Not Found", "application/json", %Schema{type: :object})
|
||||
200 => Operation.response("Translation", "application/json", %Schema{type: :object}),
|
||||
404 => Operation.response("Not Found", "application/json", %Schema{type: :object})
|
||||
}
|
||||
}
|
||||
end
|
||||
|
@ -54,10 +55,8 @@ def update_profile_operation() do
|
|||
parameters: [frontend_name_param(), profile_name_param()],
|
||||
requestBody: profile_body_param(),
|
||||
responses: %{
|
||||
200 =>
|
||||
Operation.response("Settings", "application/json", %Schema{type: :object}),
|
||||
422 =>
|
||||
Operation.response("Invalid", "application/json", %Schema{type: :object})
|
||||
200 => Operation.response("Settings", "application/json", %Schema{type: :object}),
|
||||
422 => Operation.response("Invalid", "application/json", %Schema{type: :object})
|
||||
}
|
||||
}
|
||||
end
|
||||
|
@ -77,7 +76,8 @@ def profile_name_param do
|
|||
end
|
||||
|
||||
def profile_body_param do
|
||||
request_body("Settings",
|
||||
request_body(
|
||||
"Settings",
|
||||
%Schema{
|
||||
title: "Frontend Setting Profile",
|
||||
type: :object,
|
||||
|
@ -98,6 +98,7 @@ def profile_body_param do
|
|||
}
|
||||
}
|
||||
},
|
||||
required: true)
|
||||
required: true
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -468,8 +468,18 @@ defmodule Pleroma.Web.Router do
|
|||
get("/translation/languages", TranslationController, :languages)
|
||||
|
||||
get("/frontend_settings/:frontend_name", FrontendSettingsController, :list_profiles)
|
||||
get("/frontend_settings/:frontend_name/:profile_name", FrontendSettingsController, :get_profile)
|
||||
put("/frontend_settings/:frontend_name/:profile_name", FrontendSettingsController, :update_profile)
|
||||
|
||||
get(
|
||||
"/frontend_settings/:frontend_name/:profile_name",
|
||||
FrontendSettingsController,
|
||||
:get_profile
|
||||
)
|
||||
|
||||
put(
|
||||
"/frontend_settings/:frontend_name/:profile_name",
|
||||
FrontendSettingsController,
|
||||
:update_profile
|
||||
)
|
||||
end
|
||||
|
||||
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
defmodule Pleroma.Akkoma.FrontendSettingProfileTest do
|
||||
defmodule Pleroma.Akkoma.FrontendSettingsProfileTest do
|
||||
use Pleroma.DataCase, async: true
|
||||
use Oban.Testing, repo: Pleroma.Repo
|
||||
alias Pleroma.Akkoma.FrontendSettingProfile
|
||||
alias Pleroma.Akkoma.FrontendSettingsProfile
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
@ -11,7 +11,7 @@ test "valid" do
|
|||
frontend_name = "test"
|
||||
profile_name = "test"
|
||||
settings = %{"test" => "test"}
|
||||
struct = %FrontendSettingProfile{}
|
||||
struct = %FrontendSettingsProfile{}
|
||||
|
||||
attrs = %{
|
||||
user_id: user.id,
|
||||
|
@ -21,7 +21,7 @@ test "valid" do
|
|||
version: 1
|
||||
}
|
||||
|
||||
assert %{valid?: true} = FrontendSettingProfile.changeset(struct, attrs)
|
||||
assert %{valid?: true} = FrontendSettingsProfile.changeset(struct, attrs)
|
||||
end
|
||||
|
||||
test "when settings is too long" do
|
||||
|
@ -30,7 +30,7 @@ test "when settings is too long" do
|
|||
frontend_name = "test"
|
||||
profile_name = "test"
|
||||
settings = %{"verylong" => "verylongoops"}
|
||||
struct = %FrontendSettingProfile{}
|
||||
struct = %FrontendSettingsProfile{}
|
||||
|
||||
attrs = %{
|
||||
user_id: user.id,
|
||||
|
@ -41,7 +41,7 @@ test "when settings is too long" do
|
|||
}
|
||||
|
||||
assert %{valid?: false, errors: [settings: {"is too long", _}]} =
|
||||
FrontendSettingProfile.changeset(struct, attrs)
|
||||
FrontendSettingsProfile.changeset(struct, attrs)
|
||||
end
|
||||
|
||||
test "when frontend name is too short" do
|
||||
|
@ -49,7 +49,7 @@ test "when frontend name is too short" do
|
|||
frontend_name = ""
|
||||
profile_name = "test"
|
||||
settings = %{"test" => "test"}
|
||||
struct = %FrontendSettingProfile{}
|
||||
struct = %FrontendSettingsProfile{}
|
||||
|
||||
attrs = %{
|
||||
user_id: user.id,
|
||||
|
@ -60,7 +60,7 @@ test "when frontend name is too short" do
|
|||
}
|
||||
|
||||
assert %{valid?: false, errors: [frontend_name: {"can't be blank", _}]} =
|
||||
FrontendSettingProfile.changeset(struct, attrs)
|
||||
FrontendSettingsProfile.changeset(struct, attrs)
|
||||
end
|
||||
|
||||
test "when profile name is too short" do
|
||||
|
@ -68,7 +68,7 @@ test "when profile name is too short" do
|
|||
frontend_name = "test"
|
||||
profile_name = ""
|
||||
settings = %{"test" => "test"}
|
||||
struct = %FrontendSettingProfile{}
|
||||
struct = %FrontendSettingsProfile{}
|
||||
|
||||
attrs = %{
|
||||
user_id: user.id,
|
||||
|
@ -79,7 +79,7 @@ test "when profile name is too short" do
|
|||
}
|
||||
|
||||
assert %{valid?: false, errors: [profile_name: {"can't be blank", _}]} =
|
||||
FrontendSettingProfile.changeset(struct, attrs)
|
||||
FrontendSettingsProfile.changeset(struct, attrs)
|
||||
end
|
||||
|
||||
test "when version is negative" do
|
||||
|
@ -87,7 +87,7 @@ test "when version is negative" do
|
|||
frontend_name = "test"
|
||||
profile_name = "test"
|
||||
settings = %{"test" => "test"}
|
||||
struct = %FrontendSettingProfile{}
|
||||
struct = %FrontendSettingsProfile{}
|
||||
|
||||
attrs = %{
|
||||
user_id: user.id,
|
||||
|
@ -98,7 +98,7 @@ test "when version is negative" do
|
|||
}
|
||||
|
||||
assert %{valid?: false, errors: [version: {"must be greater than %{number}", _}]} =
|
||||
FrontendSettingProfile.changeset(struct, attrs)
|
||||
FrontendSettingsProfile.changeset(struct, attrs)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -109,8 +109,8 @@ test "it should create a new record" do
|
|||
profile_name = "test"
|
||||
settings = %{"test" => "test"}
|
||||
|
||||
assert {:ok, %FrontendSettingProfile{}} =
|
||||
FrontendSettingProfile.create_or_update(
|
||||
assert {:ok, %FrontendSettingsProfile{}} =
|
||||
FrontendSettingsProfile.create_or_update(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name,
|
||||
|
@ -134,8 +134,8 @@ test "it should update a record" do
|
|||
|
||||
settings = %{"test" => "test2"}
|
||||
|
||||
assert {:ok, %FrontendSettingProfile{settings: ^settings}} =
|
||||
FrontendSettingProfile.create_or_update(
|
||||
assert {:ok, %FrontendSettingsProfile{settings: ^settings}} =
|
||||
FrontendSettingsProfile.create_or_update(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name,
|
||||
|
@ -166,8 +166,8 @@ test "it should return all records" do
|
|||
version: 1
|
||||
)
|
||||
|
||||
assert [%FrontendSettingProfile{profile_name: "profileA"}, %{profile_name: "profileB"}] =
|
||||
FrontendSettingProfile.get_all_by_user_and_frontend_name(user, frontend_name)
|
||||
assert [%FrontendSettingsProfile{profile_name: "profileA"}, %{profile_name: "profileB"}] =
|
||||
FrontendSettingsProfile.get_all_by_user_and_frontend_name(user, frontend_name)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -185,8 +185,8 @@ test "it should return a record" do
|
|||
version: 1
|
||||
)
|
||||
|
||||
assert %FrontendSettingProfile{profile_name: "profileA"} =
|
||||
FrontendSettingProfile.get_by_user_and_frontend_name_and_profile_name(
|
||||
assert %FrontendSettingsProfile{profile_name: "profileA"} =
|
||||
FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsControllerTest do
|
|||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
import Pleroma.Factory
|
||||
alias Pleroma.Akkoma.FrontendSettingProfile
|
||||
alias Pleroma.Akkoma.FrontendSettingsProfile
|
||||
|
||||
describe "GET /api/v1/akkoma/frontend_settings/:frontend_name" do
|
||||
test "it returns a list of profiles" do
|
||||
|
@ -17,7 +17,8 @@ test "it returns a list of profiles" do
|
|||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert response == [
|
||||
"test1", "test2"
|
||||
"test1",
|
||||
"test2"
|
||||
]
|
||||
end
|
||||
end
|
||||
|
@ -33,12 +34,19 @@ test "it returns 404 if not found" do
|
|||
|
||||
test "it returns 200 if found" do
|
||||
%{conn: conn, user: user} = oauth_access(["read"])
|
||||
insert(:frontend_setting_profile, user: user, frontend_name: "test", profile_name: "test1", settings: %{"test" => "test"})
|
||||
|
||||
insert(:frontend_setting_profile,
|
||||
user: user,
|
||||
frontend_name: "test",
|
||||
profile_name: "test1",
|
||||
settings: %{"test" => "test"}
|
||||
)
|
||||
|
||||
response =
|
||||
conn
|
||||
|> get("/api/v1/akkoma/frontend_settings/test/test1")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert response == %{"test" => "test"}
|
||||
end
|
||||
end
|
||||
|
@ -47,23 +55,43 @@ test "it returns 200 if found" do
|
|||
test "puts a config" do
|
||||
%{conn: conn, user: user} = oauth_access(["write"])
|
||||
settings = %{"test" => "test2"}
|
||||
|
||||
response =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put("/api/v1/akkoma/frontend_settings/test/test1", %{"settings" => settings, "version" => 1})
|
||||
|> put("/api/v1/akkoma/frontend_settings/test/test1", %{
|
||||
"settings" => settings,
|
||||
"version" => 1
|
||||
})
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert response == settings
|
||||
assert %FrontendSettingProfile{settings: ^settings} = FrontendSettingProfile.get_by_user_and_frontend_name_and_profile_name(user, "test", "test1")
|
||||
|
||||
assert %FrontendSettingsProfile{settings: ^settings} =
|
||||
FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
|
||||
user,
|
||||
"test",
|
||||
"test1"
|
||||
)
|
||||
end
|
||||
|
||||
test "refuses to overwrite a newer config" do
|
||||
%{conn: conn, user: user} = oauth_access(["write"])
|
||||
insert(:frontend_setting_profile, user: user, frontend_name: "test", profile_name: "test1", settings: %{"test" => "test"}, version: 2)
|
||||
|
||||
insert(:frontend_setting_profile,
|
||||
user: user,
|
||||
frontend_name: "test",
|
||||
profile_name: "test1",
|
||||
settings: %{"test" => "test"},
|
||||
version: 2
|
||||
)
|
||||
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put("/api/v1/akkoma/frontend_settings/test/test1", %{"settings" => %{"test" => "test2"}, "version" => 1})
|
||||
|> put("/api/v1/akkoma/frontend_settings/test/test1", %{
|
||||
"settings" => %{"test" => "test2"},
|
||||
"version" => 1
|
||||
})
|
||||
|> json_response_and_validate_schema(422)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -665,7 +665,7 @@ def announcement_factory(params \\ %{}) do
|
|||
end
|
||||
|
||||
def frontend_setting_profile_factory(params \\ %{}) do
|
||||
%Pleroma.Akkoma.FrontendSettingProfile{
|
||||
%Pleroma.Akkoma.FrontendSettingsProfile{
|
||||
user: build(:user),
|
||||
frontend_name: "akkoma-fe",
|
||||
profile_name: "default",
|
||||
|
|
Loading…
Reference in a new issue