From da029c1566c0aaf1b3e49aa1dd12d96db18773bf Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 8 Jul 2018 12:24:49 +0200 Subject: [PATCH] Fix collection parsing --- activitypub.py | 45 ++++++++++++++++++++++++++++----------------- app.py | 5 ++--- config.py | 4 ++++ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/activitypub.py b/activitypub.py index a62b35c..355d1f9 100644 --- a/activitypub.py +++ b/activitypub.py @@ -24,7 +24,6 @@ from little_boxes import activitypub as ap from little_boxes import strtobool from little_boxes.activitypub import _to_list from little_boxes.backend import Backend -from little_boxes.collection import parse_collection as ap_parse_collection from little_boxes.errors import Error from utils.media import Kind @@ -105,6 +104,28 @@ class MicroblogPubBackend(Backend): def outbox_new(self, as_actor: ap.Person, activity: ap.BaseActivity) -> None: self.save(Box.OUTBOX, activity) + def parse_collection( + payload: Optional[Dict[str, Any]] = None, url: Optional[str] = None + ) -> List[str]: + """Resolve/fetch a `Collection`/`OrderedCollection`.""" + # Resolve internal collections via MongoDB directly + if url == ID + "/followers": + q = { + "box": Box.INBOX.value, + "type": ap.ActivityType.FOLLOW.value, + "meta.undo": False, + } + return [doc["activity"]["actor"] for doc in DB.activities.find(q)] + elif url == ID + "/following": + q = { + "box": Box.OUTBOX.value, + "type": ap.ActivityType.FOLLOW.value, + "meta.undo": False, + } + return [doc["activity"]["object"] for doc in DB.activities.find(q)] + + return super().parse_collection(payload, url) + @ensure_it_is_me def outbox_is_blocked(self, as_actor: ap.Person, actor_id: str) -> bool: return bool( @@ -160,11 +181,15 @@ class MicroblogPubBackend(Backend): @ensure_it_is_me def undo_new_follower(self, as_actor: ap.Person, follow: ap.Follow) -> None: - DB.activities.update_one({"remote_id": follow.id}, {"$set": {"meta.undo": True}}) + DB.activities.update_one( + {"remote_id": follow.id}, {"$set": {"meta.undo": True}} + ) @ensure_it_is_me def undo_new_following(self, as_actor: ap.Person, follow: ap.Follow) -> None: - DB.activities.update_one({"remote_id": follow.id}, {"$set": {"meta.undo": True}}) + DB.activities.update_one( + {"remote_id": follow.id}, {"$set": {"meta.undo": True}} + ) @ensure_it_is_me def new_following(self, as_actor: ap.Person, follow: ap.Follow) -> None: @@ -498,20 +523,6 @@ def build_inbox_json_feed( return resp -def parse_collection( - payload: Optional[Dict[str, Any]] = None, url: Optional[str] = None -) -> List[str]: - """Resolve/fetch a `Collection`/`OrderedCollection`.""" - # Resolve internal collections via MongoDB directly - if url == ID + "/followers": - return [doc["remote_actor"] for doc in DB.followers.find()] - elif url == ID + "/following": - return [doc["remote_actor"] for doc in DB.following.find()] - - # Go through all the pages - return ap_parse_collection(payload, url) - - def embed_collection(total_items, first_page_id): """Helper creating a root OrderedCollection with a link to the first page.""" return { diff --git a/app.py b/app.py index e5b4938..b4748c8 100644 --- a/app.py +++ b/app.py @@ -41,6 +41,7 @@ import activitypub import config from activitypub import Box from activitypub import embed_collection +from config import ACTOR_SERVICE from config import ADMIN_API_KEY from config import BASE_URL from config import DB @@ -53,6 +54,7 @@ from config import JWT from config import KEY from config import ME from config import MEDIA_CACHE +from config import OBJECT_SERVICE from config import PASS from config import USERNAME from config import VERSION @@ -73,9 +75,6 @@ from little_boxes.webfinger import get_actor_url from little_boxes.webfinger import get_remote_follow_template from utils.key import get_secret_key from utils.media import Kind -from utils.object_service import ObjectService - -OBJECT_SERVICE = ACTOR_SERVICE = ObjectService() back = activitypub.MicroblogPubBackend() ap.use_backend(back) diff --git a/config.py b/config.py index 9e7b844..abb2187 100644 --- a/config.py +++ b/config.py @@ -15,6 +15,7 @@ from utils.key import KEY_DIR from utils.key import get_key from utils.key import get_secret_key from utils.media import MediaCache +from utils.object_service import ObjectService class ThemeStyle(Enum): @@ -147,3 +148,6 @@ ME = { }, "publicKey": KEY.to_dict(), } + + +OBJECT_SERVICE = ACTOR_SERVICE = ObjectService()