diff options
author | Haelwenn <contact+git.pleroma.social@hacktivis.me> | 2019-09-28 19:02:05 +0000 |
---|---|---|
committer | Haelwenn <contact+git.pleroma.social@hacktivis.me> | 2019-09-28 19:02:05 +0000 |
commit | b35a0f0ce4cd077300ac987449cccf057a9a216f (patch) | |
tree | 3d613f7c6129f667ea16aedfe46259d9ca6bde29 /lib/pleroma/web/oauth | |
parent | 50ab06435353144582f6afbf37402aef13c2b3f1 (diff) | |
parent | 1053319cd64a0eab40fab6dc9ce3a1b78711069b (diff) | |
download | pleroma-b35a0f0ce4cd077300ac987449cccf057a9a216f.tar.gz |
Merge branch 'tests/mastodon_api_controller.ex' into 'develop'
tests for mastodon_api_controller.ex
See merge request pleroma/pleroma!1639
Diffstat (limited to 'lib/pleroma/web/oauth')
-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 |