mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2025-01-05 06:48:41 +00:00
App orphans should only be removed if they are older than 15 mins
This commit is contained in:
parent
53744bf146
commit
1797f5958a
2 changed files with 15 additions and 5 deletions
|
@ -168,8 +168,13 @@ defmodule Pleroma.Web.OAuth.App do
|
||||||
|
|
||||||
@spec remove_orphans(pos_integer()) :: :ok
|
@spec remove_orphans(pos_integer()) :: :ok
|
||||||
def remove_orphans(limit \\ 100) do
|
def remove_orphans(limit \\ 100) do
|
||||||
|
fifteen_mins_ago = DateTime.add(DateTime.utc_now(), -900, :second)
|
||||||
|
|
||||||
Repo.transaction(fn ->
|
Repo.transaction(fn ->
|
||||||
from(a in __MODULE__, where: is_nil(a.user_id), limit: ^limit)
|
from(a in __MODULE__,
|
||||||
|
where: is_nil(a.user_id) and a.inserted_at < ^fifteen_mins_ago,
|
||||||
|
limit: ^limit
|
||||||
|
)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Enum.each(&Repo.delete(&1))
|
|> Enum.each(&Repo.delete(&1))
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -56,13 +56,18 @@ defmodule Pleroma.Web.OAuth.AppTest do
|
||||||
|
|
||||||
test "removes orphaned apps" do
|
test "removes orphaned apps" do
|
||||||
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
||||||
{:ok, %App{} = app} = App.get_or_make(attrs, ["write"])
|
{:ok, %App{} = old_app} = App.get_or_make(attrs, ["write"])
|
||||||
assert app.scopes == ["write"]
|
|
||||||
|
|
||||||
assert app == Pleroma.Repo.get_by(App, %{id: app.id})
|
attrs = %{client_name: "PleromaFE", redirect_uris: "."}
|
||||||
|
{:ok, %App{} = app} = App.get_or_make(attrs, ["write"])
|
||||||
|
|
||||||
|
# backdate the old app so it's within the threshold for being cleaned up
|
||||||
|
{:ok, _} =
|
||||||
|
"UPDATE apps SET inserted_at = now() - interval '1 hour' WHERE id = #{old_app.id}"
|
||||||
|
|> Pleroma.Repo.query()
|
||||||
|
|
||||||
App.remove_orphans()
|
App.remove_orphans()
|
||||||
|
|
||||||
assert nil == Pleroma.Repo.get_by(App, %{id: app.id})
|
assert [app] == Pleroma.Repo.all(App)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue