add checks for backend sync
This commit is contained in:
parent
d93e521f4a
commit
fb7cc0d7cc
|
@ -27,7 +27,7 @@ defmodule Pleroma.Akkoma.FrontendSettingProfile do
|
||||||
|
|
||||||
def create_or_update(%User{} = user, frontend_name, profile_name, settings) do
|
def create_or_update(%User{} = user, frontend_name, profile_name, settings) do
|
||||||
struct =
|
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 ->
|
nil ->
|
||||||
%__MODULE__{}
|
%__MODULE__{}
|
||||||
|
|
||||||
|
@ -45,13 +45,17 @@ defmodule Pleroma.Akkoma.FrontendSettingProfile do
|
||||||
|> Repo.insert_or_update()
|
|> Repo.insert_or_update()
|
||||||
end
|
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(
|
Repo.all(
|
||||||
from(p in __MODULE__, where: p.user_id == ^user_id and p.frontend_name == ^frontend_name)
|
from(p in __MODULE__, where: p.user_id == ^user_id and p.frontend_name == ^frontend_name)
|
||||||
)
|
)
|
||||||
end
|
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(
|
Repo.one(
|
||||||
from(p in __MODULE__,
|
from(p in __MODULE__,
|
||||||
where:
|
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 @@ defmodule Pleroma.Repo.Migrations.AddUserFrontendProfiles do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create_if_not_exists(
|
create_if_not_exists(index(:user_frontend_setting_profiles, [:user_id, :frontend_name]))
|
||||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name])
|
|
||||||
)
|
|
||||||
|
|
||||||
create_if_not_exists(
|
create_if_not_exists(
|
||||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
||||||
|
@ -21,7 +19,7 @@ defmodule Pleroma.Repo.Migrations.AddUserFrontendProfiles do
|
||||||
|
|
||||||
def down do
|
def down do
|
||||||
drop_if_exists(table("user_frontend_setting_profiles"))
|
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(
|
drop_if_exists(
|
||||||
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
unique_index(:user_frontend_setting_profiles, [:user_id, :frontend_name, :profile_name])
|
||||||
|
|
|
@ -87,7 +87,12 @@ defmodule Pleroma.Akkoma.FrontendSettingProfileTest do
|
||||||
settings = %{"test" => "test"}
|
settings = %{"test" => "test"}
|
||||||
|
|
||||||
assert {:ok, %FrontendSettingProfile{}} =
|
assert {:ok, %FrontendSettingProfile{}} =
|
||||||
FrontendSettingProfile.create_or_update(user, frontend_name, profile_name, settings)
|
FrontendSettingProfile.create_or_update(
|
||||||
|
user,
|
||||||
|
frontend_name,
|
||||||
|
profile_name,
|
||||||
|
settings
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it should update a record" do
|
test "it should update a record" do
|
||||||
|
@ -105,7 +110,58 @@ defmodule Pleroma.Akkoma.FrontendSettingProfileTest do
|
||||||
settings = %{"test" => "test2"}
|
settings = %{"test" => "test2"}
|
||||||
|
|
||||||
assert {:ok, %FrontendSettingProfile{settings: ^settings}} =
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue