Make sure suspended actors are not in the AP cache anymore

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-06 15:49:42 +02:00
parent 501e0d12e4
commit da2254089c
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 40 additions and 3 deletions

View file

@ -29,9 +29,12 @@ defmodule Mobilizon.Web.Cache.ActivityPub do
@spec do_get_actor(String.t()) :: {:commit, Actor.t()} | {:ignore, nil} @spec do_get_actor(String.t()) :: {:commit, Actor.t()} | {:ignore, nil}
defp do_get_actor("actor_" <> name) do defp do_get_actor("actor_" <> name) do
case Actor.find_or_make_actor_from_nickname(name) do case Actor.find_or_make_actor_from_nickname(name) do
{:ok, %ActorModel{} = actor} -> {:ok, %ActorModel{suspended: false} = actor} ->
{:commit, actor} {:commit, actor}
{:ok, %ActorModel{}} ->
{:ignore, nil}
{:error, _err} -> {:error, _err} ->
{:ignore, nil} {:ignore, nil}
end end
@ -45,9 +48,12 @@ defmodule Mobilizon.Web.Cache.ActivityPub do
def get_local_actor_by_name(name) do def get_local_actor_by_name(name) do
Cachex.fetch(@cache, "local_actor_" <> name, fn "local_actor_" <> name -> Cachex.fetch(@cache, "local_actor_" <> name, fn "local_actor_" <> name ->
case Actors.get_local_actor_by_name(name) do case Actors.get_local_actor_by_name(name) do
%ActorModel{} = actor -> %ActorModel{suspended: false} = actor ->
{:commit, actor} {:commit, actor}
{:ok, %ActorModel{}} ->
{:ignore, nil}
nil -> nil ->
{:ignore, nil} {:ignore, nil}
end end

View file

@ -14,6 +14,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Service.ActorSuspension
alias Mobilizon.Service.HTTP.ActivityPub.Mock alias Mobilizon.Service.HTTP.ActivityPub.Mock
alias Mobilizon.Web.ActivityPub.ActorView alias Mobilizon.Web.ActivityPub.ActorView
alias Mobilizon.Web.{Endpoint, PageView} alias Mobilizon.Web.{Endpoint, PageView}
@ -44,6 +45,24 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
|> Jason.encode!() |> Jason.encode!()
|> Jason.decode!() |> Jason.decode!()
end end
test "it returns nothing if the actor is suspended", %{conn: conn} do
suspended = insert(:actor)
conn = get(conn, Actor.build_url(suspended.preferred_username, :page))
assert json_response(conn, 200)
assert {:ok, true} ==
Cachex.exists?(:activity_pub, "actor_" <> suspended.preferred_username)
ActorSuspension.suspend_actor(suspended)
assert {:ok, false} ==
Cachex.exists?(:activity_pub, "actor_" <> suspended.preferred_username)
conn = get(conn, Actor.build_url(suspended.preferred_username, :page))
assert json_response(conn, 404)
end
end end
describe "/events/:uuid" do describe "/events/:uuid" do

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.Web.PageControllerTest do
import Mobilizon.Factory import Mobilizon.Factory
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActorSuspension
alias Mobilizon.Web.Endpoint alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes alias Mobilizon.Web.Router.Helpers, as: Routes
@ -37,6 +37,18 @@ defmodule Mobilizon.Web.PageControllerTest do
conn = get(conn, Actor.build_url("not_existing", :page)) conn = get(conn, Actor.build_url("not_existing", :page))
assert html_response(conn, 404) assert html_response(conn, 404)
end end
test "GET /@actor when suspended", %{conn: conn} do
suspended = insert(:actor)
conn = get(conn, Actor.build_url(suspended.preferred_username, :page))
assert html_response(conn, 200)
ActorSuspension.suspend_actor(suspended)
conn = get(conn, Actor.build_url(suspended.preferred_username, :page))
assert html_response(conn, 404)
end
end end
test "GET /events/:uuid", %{conn: conn} do test "GET /events/:uuid", %{conn: conn} do