From d005ff8f48e3ae581e4b363b10ebd9c982365a5b Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 6 May 2022 18:08:55 -0400 Subject: [PATCH] add/remove inbox on cli inbox (un)follow --- relay/database.py | 10 ++++++---- relay/manage.py | 24 ++++++++++++++++++------ relay/misc.py | 2 -- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/relay/database.py b/relay/database.py index 3582360..6e71f56 100644 --- a/relay/database.py +++ b/relay/database.py @@ -109,13 +109,15 @@ class RelayDatabase: def add_inbox(self, inbox): assert inbox.startswith('https') - assert inbox not in self.inboxes + assert not self.get_inbox(inbox) self.data['relay-list'].append(inbox) - def del_inbox(self, inbox): - if inbox not in self.inboxes: - raise KeyError(inbox) + def del_inbox(self, inbox_url): + inbox = self.get_inbox(inbox_url) + + if not inbox: + raise KeyError(inbox_url) self.data['relay-list'].remove(inbox) diff --git a/relay/manage.py b/relay/manage.py index e72a852..4aae7c7 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -9,10 +9,9 @@ import platform from aiohttp.web import AppRunner, TCPSite from cachetools import LRUCache -from . import app, views, __version__ +from . import app, misc, views, __version__ from .config import DotDict, RelayConfig, relay_software_names from .database import RelayDatabase -from .misc import check_open_port, follow_remote_actor, unfollow_remote_actor @click.group('cli', context_settings={'show_default': True}, invoke_without_command=True) @@ -77,8 +76,18 @@ def cli_inbox_follow(actor): if database.get_inbox(actor): return click.echo(f'Error: Already following actor: {actor}') - run_in_loop(follow_remote_actor, actor) - click.echo(f'Sent follow message to: {actor}') + actor_data = run_in_loop(misc.request, actor, sign_headers=True) + + if not actor_data: + return click.echo(f'Error: Failed to fetch actor: {actor}') + + inbox = misc.get_actor_inbox(actor_data) + + database.add_inbox(inbox) + database.save() + + run_in_loop(misc.follow_remote_actor, actor) + click.echo(f'Sent follow message to actor: {actor}') @cli_inbox.command('unfollow') @@ -94,7 +103,10 @@ def cli_inbox_unfollow(actor): if not database.get_inbox(actor): return click.echo(f'Error: Not following actor: {actor}') - run_in_loop(unfollow_remote_actor, actor) + database.del_inbox(actor) + database.save() + + run_in_loop(misc.unfollow_remote_actor, actor) click.echo(f'Sent unfollow message to: {actor}') @@ -370,7 +382,7 @@ def relay_run(): click.echo('Warning: PyCrypto is old and should be replaced with pycryptodome') return click.echo(pip_command) - if not check_open_port(config.listen, config.port): + if not misc.check_open_port(config.listen, config.port): return click.echo(f'Error: A server is already running on port {config.port}') # web pages diff --git a/relay/misc.py b/relay/misc.py index 184f577..91f6f3f 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -160,7 +160,6 @@ async def fetch_nodeinfo(domain): async def follow_remote_actor(actor_uri): config = app['config'] - database = app['database'] actor = await request(actor_uri) inbox = get_actor_inbox(actor) @@ -185,7 +184,6 @@ async def follow_remote_actor(actor_uri): async def unfollow_remote_actor(actor_uri): config = app['config'] - database = app['database'] actor = await request(actor_uri)