add checks for backend sync
This commit is contained in:
parent
d93e521f4a
commit
fb7cc0d7cc
4 changed files with 114 additions and 9 deletions
|
@ -27,7 +27,7 @@ def changeset(%__MODULE__{} = struct, attrs) do
|
|||
|
||||
def create_or_update(%User{} = user, frontend_name, profile_name, settings) do
|
||||
struct =
|
||||
case get_by_user_and_frontend_name_and_profile_name(user.id, frontend_name, profile_name) do
|
||||
case get_by_user_and_frontend_name_and_profile_name(user, frontend_name, profile_name) do
|
||||
nil ->
|
||||
%__MODULE__{}
|
||||
|
||||
|
@ -45,13 +45,17 @@ def create_or_update(%User{} = user, frontend_name, profile_name, settings) do
|
|||
|> Repo.insert_or_update()
|
||||
end
|
||||
|
||||
def get_all_by_user_and_frontend_name(user_id, frontend_name) do
|
||||
def get_all_by_user_and_frontend_name(%User{id: user_id}, frontend_name) do
|
||||
Repo.all(
|
||||
from(p in __MODULE__, where: p.user_id == ^user_id and p.frontend_name == ^frontend_name)
|
||||
)
|
||||
end
|
||||
|
||||
def get_by_user_and_frontend_name_and_profile_name(user_id, frontend_name, profile_name) do
|
||||
def get_by_user_and_frontend_name_and_profile_name(
|
||||
%User{id: user_id},
|
||||
frontend_name,
|
||||
profile_name
|
||||
) do
|
||||
Repo.one(
|
||||
from(p in __MODULE__,
|
||||
where:
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||
alias Pleroma.Akkoma.FrontendSettingProfile
|
||||
|
||||
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
||||
plug(
|
||||
OAuthScopesPlug,
|
||||
%{@unauthenticated_access | scopes: ["read:account"]}
|
||||
when action in [
|
||||
:list, :get, :update
|
||||
]
|
||||
)
|
||||
|
||||
#plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
||||
#defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TranslationOperation
|
||||
|
||||
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
||||
|
||||
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
|
||||
def get(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
|
||||
conn
|
||||
|> json(profile.settings)
|
||||
else
|
||||
nil -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name"
|
||||
def list(conn, %{"frontend_name" => frontend_name}) do
|
||||
with profiles <- FrontendSettingProfile.get_by_user_and_frontend_name(conn.assigns.user, frontend_name) do
|
||||
conn
|
||||
|> json(profiles)
|
||||
end
|
||||
end
|
||||
|
||||
@doc "PUT /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
|
||||
def update(conn, %{"frontend_name" => frontend_name, "profile_name" => profile_name, "settings" => settings}) do
|
||||
with %FrontendSettingProfile{} = profile <- FrontendSettingProfile.get_by_user_and_frontend_name_and_profile_name(conn.assigns.user, frontend_name, profile_name),
|
||||
{:ok, profile} <- FrontendSettingProfile.create_or_update(conn.assigns.user, frontend_name, profile_name, settings) do
|
||||
conn
|
||||
|> json(profile.settings)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,9 +10,7 @@ def up do
|
|||
timestamps()
|
||||
end
|
||||
|
||||
create_if_not_exists(
|
||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name])
|
||||
)
|
||||
create_if_not_exists(index(:user_frontend_setting_profiles, [:user_id, :frontend_name]))
|
||||
|
||||
create_if_not_exists(
|
||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
||||
|
@ -21,7 +19,7 @@ def up do
|
|||
|
||||
def down do
|
||||
drop_if_exists(table("user_frontend_setting_profiles"))
|
||||
drop_if_exists(unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name]))
|
||||
drop_if_exists(index(:user_frontend_setting_profiles, [:user_id, :frontend_name]))
|
||||
|
||||
drop_if_exists(
|
||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
||||
|
|
|
@ -87,7 +87,12 @@ test "it should create a new record" do
|
|||
settings = %{"test" => "test"}
|
||||
|
||||
assert {:ok, %FrontendSettingProfile{}} =
|
||||
FrontendSettingProfile.create_or_update(user, frontend_name, profile_name, settings)
|
||||
FrontendSettingProfile.create_or_update(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name,
|
||||
settings
|
||||
)
|
||||
end
|
||||
|
||||
test "it should update a record" do
|
||||
|
@ -105,7 +110,58 @@ test "it should update a record" do
|
|||
settings = %{"test" => "test2"}
|
||||
|
||||
assert {:ok, %FrontendSettingProfile{settings: ^settings}} =
|
||||
FrontendSettingProfile.create_or_update(user, frontend_name, profile_name, settings)
|
||||
FrontendSettingProfile.create_or_update(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name,
|
||||
settings
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "get_all_by_user_and_frontend_name/2" do
|
||||
test "it should return all records" do
|
||||
user = insert(:user)
|
||||
frontend_name = "test"
|
||||
|
||||
insert(:frontend_setting_profile,
|
||||
user: user,
|
||||
frontend_name: frontend_name,
|
||||
profile_name: "profileA",
|
||||
settings: %{"test" => "test"}
|
||||
)
|
||||
|
||||
insert(:frontend_setting_profile,
|
||||
user: user,
|
||||
frontend_name: frontend_name,
|
||||
profile_name: "profileB",
|
||||
settings: %{"test" => "test"}
|
||||
)
|
||||
|
||||
assert [%FrontendSettingProfile{profile_name: "profileA"}, %{profile_name: "profileB"}] =
|
||||
FrontendSettingProfile.get_all_by_user_and_frontend_name(user, frontend_name)
|
||||
end
|
||||
end
|
||||
|
||||
describe "get_by_user_and_frontend_name_and_profile_name/3" do
|
||||
test "it should return a record" do
|
||||
user = insert(:user)
|
||||
frontend_name = "test"
|
||||
profile_name = "profileA"
|
||||
|
||||
insert(:frontend_setting_profile,
|
||||
user: user,
|
||||
frontend_name: frontend_name,
|
||||
profile_name: profile_name,
|
||||
settings: %{"test" => "test"}
|
||||
)
|
||||
|
||||
assert %FrontendSettingProfile{profile_name: "profileA"} =
|
||||
FrontendSettingProfile.get_by_user_and_frontend_name_and_profile_name(
|
||||
user,
|
||||
frontend_name,
|
||||
profile_name
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue