From 31645618dc2273477e48508ab34b6afafe348878 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 8 Mar 2023 20:50:55 -0800 Subject: [PATCH] abstracting protocols: start to use send() #388 --- activitypub.py | 22 ++++++++++++---------- follow.py | 4 ++-- webmention.py | 5 ++--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/activitypub.py b/activitypub.py index 31a8d0b..ba9b10c 100644 --- a/activitypub.py +++ b/activitypub.py @@ -51,7 +51,7 @@ class ActivityPub(Protocol): LABEL = 'activitypub' @classmethod - def send(cls, url, activity, *, user=None): + def send(cls, url, activity, *, user=None, log_data=True): """Sends an outgoing activity. To be implemented by subclasses. @@ -60,11 +60,12 @@ class ActivityPub(Protocol): url: str, destination URL to send to activity: dict, AS1 activity to send user: :class:`User` this is on behalf of + log_data: boolean, whether to log full data object Raises: :class:`werkzeug.HTTPException` if the request fails """ - raise NotImplementedError() + return signed_post(url, user=user, log_data=True, data=activity) @classmethod def fetch(cls, id, obj, *, user=None): @@ -234,26 +235,27 @@ class ActivityPub(Protocol): 'object': followee_actor_url, } } - # TODO: generic send() - return signed_post(inbox, data=accept, user=user) + + return cls.send(inbox, accept, user=user) -def signed_get(url, user, **kwargs): - return signed_request(util.requests_get, url, user, **kwargs) +def signed_get(url, *, user=None, **kwargs): + return signed_request(util.requests_get, url, user=user, **kwargs) -def signed_post(url, user, **kwargs): +def signed_post(url, *, user=None, **kwargs): assert user - return signed_request(util.requests_post, url, user, **kwargs) + return signed_request(util.requests_post, url, user=user, **kwargs) -def signed_request(fn, url, user, data=None, log_data=True, headers=None, **kwargs): +def signed_request(fn, url, *, user=None, data=None, log_data=True, + headers=None, **kwargs): """Wraps requests.* and adds HTTP Signature. Args: fn: :func:`util.requests_get` or :func:`util.requests_get` url: str - user: :class:`User` to sign request with + user: optional :class:`User` to sign request with data: optional AS2 object log_data: boolean, whether to log full data object kwargs: passed through to requests diff --git a/follow.py b/follow.py index 2d3b895..b779ba2 100644 --- a/follow.py +++ b/follow.py @@ -176,7 +176,7 @@ class FollowCallback(indieauth.Callback): 'actor': common.host_url(domain), 'to': [as2.PUBLIC_AUDIENCE], } - activitypub.signed_post(inbox, user=user, data=follow_as2) + activitypub.ActivityPub.send(inbox, follow_as2, user=user) Follower.get_or_create(dest=id, src=domain, status='active', last_follow=follow_as2) @@ -253,7 +253,7 @@ class UnfollowCallback(indieauth.Callback): 'actor': common.host_url(domain), 'object': follower.last_follow, } - activitypub.signed_post(inbox, user=user, data=unfollow_as2) + activitypub.ActivityPub.send(inbox, unfollow_as2, user=user) follower.status = 'inactive' follower.put() diff --git a/webmention.py b/webmention.py index e6b3489..f2cc342 100644 --- a/webmention.py +++ b/webmention.py @@ -217,9 +217,8 @@ class Webmention(View): last_follow=self.source_as2) try: - last = activitypub.signed_post(inbox, user=self.user, - data=self.source_as2, - log_data=log_data) + last = activitypub.ActivityPub.send( + inbox, self.source_as2, user=self.user, log_data=log_data) obj.delivered.append(target) last_success = last except BaseException as e: