add/remove inbox on cli inbox (un)follow

This commit is contained in:
Izalia Mae 2022-05-06 18:08:55 -04:00
parent 1c3b1b39e6
commit d005ff8f48
3 changed files with 24 additions and 12 deletions

View file

@ -109,13 +109,15 @@ class RelayDatabase:
def add_inbox(self, inbox): def add_inbox(self, inbox):
assert inbox.startswith('https') assert inbox.startswith('https')
assert inbox not in self.inboxes assert not self.get_inbox(inbox)
self.data['relay-list'].append(inbox) self.data['relay-list'].append(inbox)
def del_inbox(self, inbox): def del_inbox(self, inbox_url):
if inbox not in self.inboxes: inbox = self.get_inbox(inbox_url)
raise KeyError(inbox)
if not inbox:
raise KeyError(inbox_url)
self.data['relay-list'].remove(inbox) self.data['relay-list'].remove(inbox)

View file

@ -9,10 +9,9 @@ import platform
from aiohttp.web import AppRunner, TCPSite from aiohttp.web import AppRunner, TCPSite
from cachetools import LRUCache from cachetools import LRUCache
from . import app, views, __version__ from . import app, misc, views, __version__
from .config import DotDict, RelayConfig, relay_software_names from .config import DotDict, RelayConfig, relay_software_names
from .database import RelayDatabase 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) @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): if database.get_inbox(actor):
return click.echo(f'Error: Already following actor: {actor}') return click.echo(f'Error: Already following actor: {actor}')
run_in_loop(follow_remote_actor, actor) actor_data = run_in_loop(misc.request, actor, sign_headers=True)
click.echo(f'Sent follow message to: {actor}')
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') @cli_inbox.command('unfollow')
@ -94,7 +103,10 @@ def cli_inbox_unfollow(actor):
if not database.get_inbox(actor): if not database.get_inbox(actor):
return click.echo(f'Error: Not following actor: {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}') 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') click.echo('Warning: PyCrypto is old and should be replaced with pycryptodome')
return click.echo(pip_command) 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}') return click.echo(f'Error: A server is already running on port {config.port}')
# web pages # web pages

View file

@ -160,7 +160,6 @@ async def fetch_nodeinfo(domain):
async def follow_remote_actor(actor_uri): async def follow_remote_actor(actor_uri):
config = app['config'] config = app['config']
database = app['database']
actor = await request(actor_uri) actor = await request(actor_uri)
inbox = get_actor_inbox(actor) inbox = get_actor_inbox(actor)
@ -185,7 +184,6 @@ async def follow_remote_actor(actor_uri):
async def unfollow_remote_actor(actor_uri): async def unfollow_remote_actor(actor_uri):
config = app['config'] config = app['config']
database = app['database']
actor = await request(actor_uri) actor = await request(actor_uri)