abstracting protocols: start to use send()

#388
pull/448/head
Ryan Barrett 2023-03-08 20:50:55 -08:00
rodzic ffd8810b44
commit 31645618dc
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 16 dodań i 15 usunięć

Wyświetl plik

@ -51,7 +51,7 @@ class ActivityPub(Protocol):
LABEL = 'activitypub' LABEL = 'activitypub'
@classmethod @classmethod
def send(cls, url, activity, *, user=None): def send(cls, url, activity, *, user=None, log_data=True):
"""Sends an outgoing activity. """Sends an outgoing activity.
To be implemented by subclasses. To be implemented by subclasses.
@ -60,11 +60,12 @@ class ActivityPub(Protocol):
url: str, destination URL to send to url: str, destination URL to send to
activity: dict, AS1 activity to send activity: dict, AS1 activity to send
user: :class:`User` this is on behalf of user: :class:`User` this is on behalf of
log_data: boolean, whether to log full data object
Raises: Raises:
:class:`werkzeug.HTTPException` if the request fails :class:`werkzeug.HTTPException` if the request fails
""" """
raise NotImplementedError() return signed_post(url, user=user, log_data=True, data=activity)
@classmethod @classmethod
def fetch(cls, id, obj, *, user=None): def fetch(cls, id, obj, *, user=None):
@ -234,26 +235,27 @@ class ActivityPub(Protocol):
'object': followee_actor_url, '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): def signed_get(url, *, user=None, **kwargs):
return signed_request(util.requests_get, url, user, **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 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. """Wraps requests.* and adds HTTP Signature.
Args: Args:
fn: :func:`util.requests_get` or :func:`util.requests_get` fn: :func:`util.requests_get` or :func:`util.requests_get`
url: str url: str
user: :class:`User` to sign request with user: optional :class:`User` to sign request with
data: optional AS2 object data: optional AS2 object
log_data: boolean, whether to log full data object log_data: boolean, whether to log full data object
kwargs: passed through to requests kwargs: passed through to requests

Wyświetl plik

@ -176,7 +176,7 @@ class FollowCallback(indieauth.Callback):
'actor': common.host_url(domain), 'actor': common.host_url(domain),
'to': [as2.PUBLIC_AUDIENCE], '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', Follower.get_or_create(dest=id, src=domain, status='active',
last_follow=follow_as2) last_follow=follow_as2)
@ -253,7 +253,7 @@ class UnfollowCallback(indieauth.Callback):
'actor': common.host_url(domain), 'actor': common.host_url(domain),
'object': follower.last_follow, '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.status = 'inactive'
follower.put() follower.put()

Wyświetl plik

@ -217,9 +217,8 @@ class Webmention(View):
last_follow=self.source_as2) last_follow=self.source_as2)
try: try:
last = activitypub.signed_post(inbox, user=self.user, last = activitypub.ActivityPub.send(
data=self.source_as2, inbox, self.source_as2, user=self.user, log_data=log_data)
log_data=log_data)
obj.delivered.append(target) obj.delivered.append(target)
last_success = last last_success = last
except BaseException as e: except BaseException as e: