mirror of
https://git.pleroma.social/pleroma/relay.git
synced 2024-11-23 23:17:58 +00:00
Add software check on follow/unfollow
This commit is contained in:
parent
3d60ae2bbc
commit
6a0c1fe726
|
@ -87,10 +87,15 @@ class HttpClient:
|
||||||
headers.update(self.database.signer.sign_headers(
|
headers.update(self.database.signer.sign_headers(
|
||||||
'GET', url, algorithm='original'))
|
'GET', url, algorithm='original'))
|
||||||
|
|
||||||
|
async with ClientSession(
|
||||||
|
connector=TCPConnector(
|
||||||
|
limit=self.limit, ttl_dns_cache=300),
|
||||||
|
headers=HEADERS,
|
||||||
|
connector_owner=True,
|
||||||
|
timeout=ClientTimeout(total=self.timeout)) as session:
|
||||||
try:
|
try:
|
||||||
logging.verbose(f'Fetching resource: {url}')
|
logging.verbose(f'Fetching resource: {url}')
|
||||||
|
async with session.get(url, headers=headers) as resp:
|
||||||
async with self._session.get(url, headers=headers) as resp:
|
|
||||||
# Not expecting a response with 202s, so just return
|
# Not expecting a response with 202s, so just return
|
||||||
if resp.status == 202:
|
if resp.status == 202:
|
||||||
return
|
return
|
||||||
|
@ -131,11 +136,11 @@ class HttpClient:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
async def post(self, url, message):
|
async def post(self, url, message, software=None):
|
||||||
instance = self.database.get_inbox(url)
|
instance = self.database.get_inbox(url)
|
||||||
|
|
||||||
# Using the old algo by default is probably a better idea right now
|
# Using the old algo by default is probably a better idea right now
|
||||||
if instance and instance.get('software') in {'mastodon'}:
|
if (instance and instance.get('software') in {'mastodon'}) or (software and software in {'mastodon'}):
|
||||||
algorithm = 'hs2019'
|
algorithm = 'hs2019'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -145,42 +150,18 @@ class HttpClient:
|
||||||
headers.update(self.database.signer.sign_headers(
|
headers.update(self.database.signer.sign_headers(
|
||||||
'POST', url, message, algorithm=algorithm))
|
'POST', url, message, algorithm=algorithm))
|
||||||
|
|
||||||
try:
|
|
||||||
logging.verbose(f'Sending "{message.type}" to {url}')
|
|
||||||
logging.verbose(
|
|
||||||
f'url: {url}\nheaders: {headers}\ndata: {message.to_json()}')
|
|
||||||
|
|
||||||
# The following does not work and throws exception on 'relay inbox follow':
|
|
||||||
# Traceback (most recent call last):
|
|
||||||
# File "/home/vriess/Dokumente/Repos/relay-1/relay/http_client.py", line 153, in post
|
|
||||||
# async with self._session.post(url, headers=headers, data=message.to_json()) as resp:
|
|
||||||
# File "/home/vriess/Dokumente/Repos/relay-1/.venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
|
|
||||||
# self._resp = await self._coro
|
|
||||||
# File "/home/vriess/Dokumente/Repos/relay-1/.venv/lib/python3.10/site-packages/aiohttp/client.py", line 448, in _request
|
|
||||||
# handle = tm.start()
|
|
||||||
# File "/home/vriess/Dokumente/Repos/relay-1/.venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 651, in start
|
|
||||||
# return self._loop.call_at(when, self.__call__)
|
|
||||||
# File "/usr/lib/python3.10/asyncio/base_events.py", line 732, in call_at
|
|
||||||
# self._check_closed()
|
|
||||||
# File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
|
|
||||||
# raise RuntimeError('Event loop is closed')
|
|
||||||
# RuntimeError: Event loop is closed
|
|
||||||
# ↓↓↓↓
|
|
||||||
# async with self._session.post(url, headers=headers, data=message.to_json()) as resp:
|
|
||||||
# ## Not expecting a response, so just return
|
|
||||||
# if resp.status in {200, 202}:
|
|
||||||
# return logging.info(f'Successfully sent "{message.type}" to {url}')
|
|
||||||
|
|
||||||
# logging.info(f'Received error when pushing to {url}: {resp.status}')
|
|
||||||
# return logging.info(await resp.read()) # change this to debug
|
|
||||||
|
|
||||||
# Creating a session here works for some reason and does not throw an error
|
|
||||||
async with ClientSession(
|
async with ClientSession(
|
||||||
connector=TCPConnector(
|
connector=TCPConnector(
|
||||||
limit=self.limit, ttl_dns_cache=300),
|
limit=self.limit, ttl_dns_cache=300),
|
||||||
headers=HEADERS,
|
headers=HEADERS,
|
||||||
connector_owner=True,
|
connector_owner=True,
|
||||||
timeout=ClientTimeout(total=self.timeout)) as session:
|
timeout=ClientTimeout(total=self.timeout)) as session:
|
||||||
|
|
||||||
|
try:
|
||||||
|
logging.verbose(f'Sending "{message.type}" to {url}')
|
||||||
|
logging.verbose(
|
||||||
|
f'url: {url}\nheaders: {headers}\ndata: {message.to_json()}')
|
||||||
|
|
||||||
async with session.post(url, headers=headers, data=message.to_json()) as resp:
|
async with session.post(url, headers=headers, data=message.to_json()) as resp:
|
||||||
# Not expecting a response, so just return
|
# Not expecting a response, so just return
|
||||||
if resp.status in {200, 202}:
|
if resp.status in {200, 202}:
|
||||||
|
|
|
@ -160,7 +160,11 @@ def cli_inbox_follow(actor):
|
||||||
actor = actor
|
actor = actor
|
||||||
)
|
)
|
||||||
|
|
||||||
asyncio.run(app.client.post(inbox, message))
|
# Fetch software to decide on algorithm
|
||||||
|
nodeinfo = asyncio.run(app.client.fetch_nodeinfo(domain))
|
||||||
|
software = nodeinfo.sw_name if nodeinfo else None
|
||||||
|
|
||||||
|
asyncio.run(app.client.post(inbox, message, software))
|
||||||
click.echo(f'Sent follow message to actor: {actor}')
|
click.echo(f'Sent follow message to actor: {actor}')
|
||||||
|
|
||||||
|
|
||||||
|
@ -198,7 +202,11 @@ def cli_inbox_unfollow(actor):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
asyncio.run(app.client.post(inbox, message))
|
# Fetch software to decide on algorithm
|
||||||
|
nodeinfo = asyncio.run(app.client.fetch_nodeinfo(domain))
|
||||||
|
software = nodeinfo.sw_name if nodeinfo else None
|
||||||
|
|
||||||
|
asyncio.run(app.client.post(inbox, message, software))
|
||||||
click.echo(f'Sent unfollow message to: {actor}')
|
click.echo(f'Sent unfollow message to: {actor}')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue