diff options
author | Maksim Pechnikov <parallel588@gmail.com> | 2019-09-06 21:50:00 +0300 |
---|---|---|
committer | Maksim Pechnikov <parallel588@gmail.com> | 2019-09-06 21:50:00 +0300 |
commit | ab2f21e470f349f783f895f26da3041afcc3d73e (patch) | |
tree | e086b2568f448aa497955f4bd66a20dee0f6ea93 /lib/pleroma/web/oauth/app.ex | |
parent | 130bc8e0d553eb918685edf2b1fdab7e69021446 (diff) | |
download | pleroma-ab2f21e470f349f783f895f26da3041afcc3d73e.tar.gz |
tests for mastodon_api_controller.ex
Diffstat (limited to 'lib/pleroma/web/oauth/app.ex')
-rw-r--r-- | lib/pleroma/web/oauth/app.ex | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/pleroma/web/oauth/app.ex b/lib/pleroma/web/oauth/app.ex index ddcdb1871..cc3fb1ce5 100644 --- a/lib/pleroma/web/oauth/app.ex +++ b/lib/pleroma/web/oauth/app.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.OAuth.App do use Ecto.Schema import Ecto.Changeset + alias Pleroma.Repo @type t :: %__MODULE__{} @@ -39,4 +40,29 @@ defmodule Pleroma.Web.OAuth.App do changeset end end + + @doc """ + Gets app by attrs or create new with attrs. + And updates the scopes if need. + """ + @spec get_or_make(map(), list(String.t())) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()} + def get_or_make(attrs, scopes) do + with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do + update_scopes(app, scopes) + else + _e -> + %__MODULE__{} + |> register_changeset(Map.put(attrs, :scopes, scopes)) + |> Repo.insert() + end + end + + defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app} + defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app} + + defp update_scopes(%__MODULE__{} = app, scopes) do + app + |> change(%{scopes: scopes}) + |> Repo.update() + end end |