Lot of improvements/bugfixes
Better caching, and more on visibility too
This commit is contained in:
143
activitypub.py
143
activitypub.py
@@ -35,6 +35,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
ACTORS_CACHE = LRUCache(maxsize=256)
|
||||
MY_PERSON = ap.Person(**ME)
|
||||
|
||||
|
||||
def _actor_to_meta(actor: ap.BaseActivity, with_inbox=False) -> Dict[str, Any]:
|
||||
@@ -125,7 +126,7 @@ class MicroblogPubBackend(Backend):
|
||||
except ValueError:
|
||||
pass
|
||||
object_visibility = None
|
||||
if object_id:
|
||||
if activity.has_type([ap.ActivityType.CREATE, ap.ActivityType.ANNOUNCE]):
|
||||
object_visibility = ap.get_visibility(activity.get_object()).name
|
||||
|
||||
actor_id = activity.get_actor().id
|
||||
@@ -205,10 +206,40 @@ class MicroblogPubBackend(Backend):
|
||||
)
|
||||
)
|
||||
|
||||
def _fetch_iri(self, iri: str) -> ap.ObjectType:
|
||||
def _fetch_iri(self, iri: str) -> ap.ObjectType: # noqa: C901
|
||||
# Shortcut if the instance actor is fetched
|
||||
if iri == ME["id"]:
|
||||
return ME
|
||||
|
||||
# Internal collecitons handling
|
||||
# Followers
|
||||
if iri == MY_PERSON.followers:
|
||||
followers = []
|
||||
for data in DB.activities.find(
|
||||
{
|
||||
"box": Box.INBOX.value,
|
||||
"type": ap.ActivityType.FOLLOW.value,
|
||||
"meta.undo": False,
|
||||
}
|
||||
):
|
||||
followers.append(data["meta"]["actor_id"])
|
||||
return {"type": "Collection", "items": followers}
|
||||
|
||||
# Following
|
||||
if iri == MY_PERSON.following:
|
||||
following = []
|
||||
for data in DB.activities.find(
|
||||
{
|
||||
"box": Box.OUTBOX.value,
|
||||
"type": ap.ActivityType.FOLLOW.value,
|
||||
"meta.undo": False,
|
||||
}
|
||||
):
|
||||
following.append(data["meta"]["object_id"])
|
||||
return {"type": "Collection", "items": following}
|
||||
|
||||
# TODO(tsileo): handle the liked collection too
|
||||
|
||||
# Check if the activity is owned by this server
|
||||
if iri.startswith(BASE_URL):
|
||||
is_a_note = False
|
||||
@@ -229,40 +260,48 @@ class MicroblogPubBackend(Backend):
|
||||
if data["meta"]["deleted"]:
|
||||
raise ActivityGoneError(f"{iri} is gone")
|
||||
return data["activity"]
|
||||
obj = DB.activities.find_one({"meta.object_id": iri, "type": "Create"})
|
||||
if obj:
|
||||
if obj["meta"]["deleted"]:
|
||||
raise ActivityGoneError(f"{iri} is gone")
|
||||
return obj["meta"].get("object") or obj["activity"]["object"]
|
||||
|
||||
# Check if it's cached because it's a follower
|
||||
# Remove extra info (like the key hash if any)
|
||||
cleaned_iri = iri.split("#")[0]
|
||||
actor = DB.activities.find_one(
|
||||
{
|
||||
"meta.actor_id": cleaned_iri,
|
||||
"type": ap.ActivityType.FOLLOW.value,
|
||||
"meta.undo": False,
|
||||
}
|
||||
)
|
||||
if actor and actor["meta"].get("actor"):
|
||||
return actor["meta"]["actor"]
|
||||
|
||||
# Check if it's cached because it's a following
|
||||
actor2 = DB.activities.find_one(
|
||||
{
|
||||
"meta.object_id": cleaned_iri,
|
||||
"type": ap.ActivityType.FOLLOW.value,
|
||||
"meta.undo": False,
|
||||
}
|
||||
)
|
||||
if actor2 and actor2["meta"].get("object"):
|
||||
return actor2["meta"]["object"]
|
||||
|
||||
# Fetch the URL via HTTP
|
||||
logger.info(f"dereference {iri} via HTTP")
|
||||
return super().fetch_iri(iri)
|
||||
|
||||
def fetch_iri(self, iri: str, no_cache=False) -> ap.ObjectType:
|
||||
if iri == ME["id"]:
|
||||
return ME
|
||||
|
||||
if iri in ACTORS_CACHE:
|
||||
logger.info(f"{iri} found in cache")
|
||||
return ACTORS_CACHE[iri]
|
||||
|
||||
# data = DB.actors.find_one({"remote_id": iri})
|
||||
# if data:
|
||||
# if ap._has_type(data["type"], ap.ACTOR_TYPES):
|
||||
# logger.info(f"{iri} found in DB cache")
|
||||
# ACTORS_CACHE[iri] = data["data"]
|
||||
# return data["data"]
|
||||
if not no_cache:
|
||||
# Fetch the activity by checking the local DB first
|
||||
data = self._fetch_iri(iri)
|
||||
else:
|
||||
return super().fetch_iri(iri)
|
||||
data = super().fetch_iri(iri)
|
||||
|
||||
logger.debug(f"_fetch_iri({iri!r}) == {data!r}")
|
||||
if ap._has_type(data["type"], ap.ACTOR_TYPES):
|
||||
logger.debug(f"caching actor {iri}")
|
||||
# Cache the actor
|
||||
DB.actors.update_one(
|
||||
{"remote_id": iri},
|
||||
{"$set": {"remote_id": iri, "data": data}},
|
||||
upsert=True,
|
||||
)
|
||||
ACTORS_CACHE[iri] = data
|
||||
|
||||
return data
|
||||
|
||||
@@ -395,37 +434,36 @@ class MicroblogPubBackend(Backend):
|
||||
|
||||
@ensure_it_is_me
|
||||
def inbox_delete(self, as_actor: ap.Person, delete: ap.Delete) -> None:
|
||||
obj = delete.get_object()
|
||||
logger.debug("delete object={obj!r}")
|
||||
obj_id = delete.get_object_id()
|
||||
logger.debug("delete object={obj_id}")
|
||||
try:
|
||||
obj = delete.get_object()
|
||||
logger.info(f"inbox_delete handle_replies obj={obj!r}")
|
||||
in_reply_to = obj.get_in_reply_to() if obj.inReplyTo else None
|
||||
if obj.has_type(ap.CREATE_TYPES):
|
||||
in_reply_to = ap._get_id(
|
||||
DB.activities.find_one(
|
||||
{"meta.object_id": obj_id, "type": ap.ActivityType.CREATE.value}
|
||||
)["activity"]["object"].get("inReplyTo")
|
||||
)
|
||||
if in_reply_to:
|
||||
self._handle_replies_delete(as_actor, in_reply_to)
|
||||
except Exception:
|
||||
logger.exception(f"failed to handle delete replies for {obj_id}")
|
||||
|
||||
DB.activities.update_one(
|
||||
{"activity.object.id": obj.id}, {"$set": {"meta.deleted": True}}
|
||||
{"meta.object_id": obj_id, "type": "Create"},
|
||||
{"$set": {"meta.deleted": True}},
|
||||
)
|
||||
|
||||
logger.info(f"inbox_delete handle_replies obj={obj!r}")
|
||||
in_reply_to = obj.get_in_reply_to() if obj.inReplyTo else None
|
||||
if delete.get_object().ACTIVITY_TYPE != ap.ActivityType.NOTE:
|
||||
in_reply_to = ap._get_id(
|
||||
DB.activities.find_one(
|
||||
{
|
||||
"activity.object.id": delete.get_object().id,
|
||||
"type": ap.ActivityType.CREATE.value,
|
||||
}
|
||||
)["activity"]["object"].get("inReplyTo")
|
||||
)
|
||||
|
||||
# Fake a Undo so any related Like/Announce doesn't appear on the web UI
|
||||
DB.activities.update(
|
||||
{"meta.object.id": obj.id},
|
||||
{"$set": {"meta.undo": True, "meta.extra": "object deleted"}},
|
||||
)
|
||||
if in_reply_to:
|
||||
self._handle_replies_delete(as_actor, in_reply_to)
|
||||
# Foce undo other related activities
|
||||
DB.activities.update({"meta.object_id": obj_id}, {"$set": {"meta.undo": True}})
|
||||
|
||||
@ensure_it_is_me
|
||||
def outbox_delete(self, as_actor: ap.Person, delete: ap.Delete) -> None:
|
||||
DB.activities.update_one(
|
||||
{"activity.object.id": delete.get_object().id},
|
||||
{"$set": {"meta.deleted": True}},
|
||||
DB.activities.update(
|
||||
{"meta.object_id": delete.get_object_id()},
|
||||
{"$set": {"meta.deleted": True, "meta.undo": True}},
|
||||
)
|
||||
obj = delete.get_object()
|
||||
if delete.get_object().ACTIVITY_TYPE != ap.ActivityType.NOTE:
|
||||
@@ -438,11 +476,6 @@ class MicroblogPubBackend(Backend):
|
||||
)["activity"]
|
||||
).get_object()
|
||||
|
||||
DB.activities.update(
|
||||
{"meta.object.id": obj.id},
|
||||
{"$set": {"meta.undo": True, "meta.exta": "object deleted"}},
|
||||
)
|
||||
|
||||
self._handle_replies_delete(as_actor, obj.get_in_reply_to())
|
||||
|
||||
@ensure_it_is_me
|
||||
|
Reference in New Issue
Block a user