sedi-relay/relay/database.py

44 lines
1.1 KiB
Python
Raw Normal View History

2018-08-10 19:59:46 +00:00
import asyncio
import logging
import urllib.parse
2018-08-10 19:59:46 +00:00
import simplejson as json
from sys import exit
2018-08-10 19:59:46 +00:00
from . import CONFIG
AP_CONFIG = CONFIG['ap']
2018-08-10 19:59:46 +00:00
try:
with open(CONFIG['db']) as f:
DATABASE = json.load(f)
except FileNotFoundError:
2018-08-10 19:59:46 +00:00
logging.info('No database was found, making a new one.')
DATABASE = {}
except json.decoder.JSONDecodeError:
logging.info('Invalid JSON in db. Exiting...')
exit(1)
2018-10-31 19:29:30 +00:00
following = DATABASE.get('relay-list', [])
for inbox in following:
2019-05-21 16:29:55 +00:00
if urllib.parse.urlsplit(inbox).hostname in AP_CONFIG['blocked_instances']:
following.remove(inbox)
DATABASE['relay-list'] = following
elif AP_CONFIG['whitelist_enabled'] is True and urllib.parse.urlsplit(inbox).hostname not in AP_CONFIG['whitelist']:
2018-10-31 19:29:30 +00:00
following.remove(inbox)
DATABASE['relay-list'] = following
2018-08-10 19:59:46 +00:00
if 'actors' in DATABASE:
DATABASE.pop('actors')
2018-08-10 19:59:46 +00:00
async def database_save():
while True:
with open(CONFIG['db'], 'w') as f:
json.dump(DATABASE, f)
await asyncio.sleep(30)
asyncio.ensure_future(database_save())