Merge branch 'refactor/object-cache-deletion' into 'develop'
refactor object cache deletion See merge request pleroma/pleroma!408
This commit is contained in:
commit
68a1405643
|
@ -1,6 +1,6 @@
|
||||||
defmodule Pleroma.Object do
|
defmodule Pleroma.Object do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
alias Pleroma.{Repo, Object}
|
alias Pleroma.{Repo, Object, Activity}
|
||||||
import Ecto.{Query, Changeset}
|
import Ecto.{Query, Changeset}
|
||||||
|
|
||||||
schema "objects" do
|
schema "objects" do
|
||||||
|
@ -52,4 +52,12 @@ def get_cached_by_ap_id(ap_id) do
|
||||||
def context_mapping(context) do
|
def context_mapping(context) do
|
||||||
Object.change(%Object{}, %{data: %{"id" => context}})
|
Object.change(%Object{}, %{data: %{"id" => context}})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def delete(%Object{data: %{"id" => id}} = object) do
|
||||||
|
with Repo.delete(object),
|
||||||
|
Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
|
||||||
|
{:ok, true} <- Cachex.del(:user_cache, "object:#{id}") do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -273,8 +273,7 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ tru
|
||||||
"to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
|
"to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
|
||||||
}
|
}
|
||||||
|
|
||||||
with Repo.delete(object),
|
with Object.delete(object),
|
||||||
Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
|
|
||||||
{:ok, activity} <- insert(data, local),
|
{:ok, activity} <- insert(data, local),
|
||||||
:ok <- maybe_federate(activity),
|
:ok <- maybe_federate(activity),
|
||||||
{:ok, _actor} <- User.decrease_note_count(user) do
|
{:ok, _actor} <- User.decrease_note_count(user) do
|
||||||
|
|
|
@ -9,8 +9,7 @@ def delete(activity_id, user) do
|
||||||
with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
|
with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
|
||||||
%Object{} = object <- Object.normalize(object_id),
|
%Object{} = object <- Object.normalize(object_id),
|
||||||
true <- user.info["is_moderator"] || user.ap_id == object.data["actor"],
|
true <- user.info["is_moderator"] || user.ap_id == object.data["actor"],
|
||||||
{:ok, delete} <- ActivityPub.delete(object),
|
{:ok, delete} <- ActivityPub.delete(object) do
|
||||||
{:ok, true} <- Cachex.del(:user_cache, "object:#{object_id}") do
|
|
||||||
{:ok, delete}
|
{:ok, delete}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,4 +19,34 @@ test "it ensures uniqueness of the id" do
|
||||||
{:error, _result} = Repo.insert(cs)
|
{:error, _result} = Repo.insert(cs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "deletion function" do
|
||||||
|
test "deletes an object" do
|
||||||
|
object = insert(:note)
|
||||||
|
found_object = Object.get_by_ap_id(object.data["id"])
|
||||||
|
|
||||||
|
assert object == found_object
|
||||||
|
|
||||||
|
Object.delete(found_object)
|
||||||
|
|
||||||
|
found_object = Object.get_by_ap_id(object.data["id"])
|
||||||
|
|
||||||
|
refute object == found_object
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ensures cache is cleared for the object" do
|
||||||
|
object = insert(:note)
|
||||||
|
cached_object = Object.get_cached_by_ap_id(object.data["id"])
|
||||||
|
|
||||||
|
assert object == cached_object
|
||||||
|
|
||||||
|
Object.delete(cached_object)
|
||||||
|
|
||||||
|
{:ok, nil} = Cachex.get(:user_cache, "object:#{object.data["id"]}")
|
||||||
|
|
||||||
|
cached_object = Object.get_cached_by_ap_id(object.data["id"])
|
||||||
|
|
||||||
|
refute object == cached_object
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue