Skip to content

Commit

Permalink
abstracting protocols: start to use send()
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 9, 2023
1 parent bdb03c3 commit 7080335
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
22 changes: 12 additions & 10 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions webmention.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7080335

Please sign in to comment.