diff --git a/activitypub.py b/activitypub.py index 31a8d0b1..ba9b10c9 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 @@ def send(cls, url, activity, *, user=None): 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 @@ def accept_follow(cls, obj, user): '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 2d3b8956..b779ba27 100644 --- a/follow.py +++ b/follow.py @@ -176,7 +176,7 @@ def finish(self, auth_entity, state=None): '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 @@ def finish(self, auth_entity, state=None): '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 e6b3489e..f2cc3423 100644 --- a/webmention.py +++ b/webmention.py @@ -217,9 +217,8 @@ def try_activitypub(self): 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: