add manage command to convert pleroma relay config and data
This commit is contained in:
parent
ee900b5282
commit
14af3349a7
|
@ -5,3 +5,4 @@
|
|||
-e git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-sql&subdirectory=sql
|
||||
|
||||
envbash==1.2.0
|
||||
pyyaml==5.4.1
|
||||
|
|
|
@ -1,110 +1,13 @@
|
|||
import json
|
||||
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
from izzylib import DotDict, boolean, logging
|
||||
from izzylib import logging
|
||||
from izzylib.http_requests_client.signature import generate_rsa_key
|
||||
from izzylib.sql import SqlDatabase, SqlSession
|
||||
from izzylib.sql import SqlColumn as Column
|
||||
from izzylib.sql import SqlDatabase
|
||||
from izzylib.sql.generic import OperationalError
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from . import get, put, delete
|
||||
from .base import Session, tables
|
||||
|
||||
from ..config import config, dbconfig
|
||||
|
||||
|
||||
tables = {
|
||||
'config': [
|
||||
Column('id'),
|
||||
Column('key', 'text', nullable=False),
|
||||
Column('value', 'text')
|
||||
],
|
||||
'inbox': [
|
||||
Column('id'),
|
||||
Column('domain', 'text', nullable=False),
|
||||
Column('inbox', 'text', nullable=False, unique=True),
|
||||
Column('actor', 'text', nullable=False, unique=True),
|
||||
Column('followid', 'text'),
|
||||
Column('timestamp')
|
||||
],
|
||||
'retry': [
|
||||
Column('id'),
|
||||
Column('msgid', 'text', nullable=False),
|
||||
Column('inboxid', 'integer', nullable=False, fkey='inbox.id'),
|
||||
Column('data', 'json', nullable=False),
|
||||
Column('headers', 'json')
|
||||
],
|
||||
'user': [
|
||||
Column('id'),
|
||||
Column('handle', 'text', nullable=False, unique=True),
|
||||
Column('username', 'text'),
|
||||
Column('hash', 'text'),
|
||||
Column('level', 'integer', nullable=False, default=0),
|
||||
Column('timestamp')
|
||||
],
|
||||
'token': [
|
||||
Column('id'),
|
||||
Column('userid', 'integer', fkey='user.id'),
|
||||
Column('code', 'text', nullable=False, unique=True),
|
||||
Column('timestamp')
|
||||
],
|
||||
'whitelist': [
|
||||
Column('id'),
|
||||
Column('domain', 'text', nullable=False, unique=True)
|
||||
],
|
||||
'ban': [
|
||||
Column('id'),
|
||||
Column('handle', 'text'),
|
||||
Column('domain', 'text'),
|
||||
Column('reason', 'text')
|
||||
],
|
||||
'actor_cache': [
|
||||
Column('id'),
|
||||
Column('url', 'text', nullable=False, unique=True),
|
||||
Column('data', 'json', nullable=False),
|
||||
Column('timestamp')
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
class Session(SqlSession):
|
||||
#get = get
|
||||
#put = put
|
||||
#delete = delete
|
||||
|
||||
config_defaults = dict(
|
||||
version = (0, int),
|
||||
pubkey = (None, str),
|
||||
privkey = (None, str),
|
||||
name = ('Uncia Relay', str),
|
||||
description = ('Small and fast ActivityPub relay', str),
|
||||
rules = (["No homophobia, transphobia, or racism"], json.loads),
|
||||
require_approval = (True, boolean),
|
||||
email = (None, str),
|
||||
show_domain_bans = (False, boolean),
|
||||
show_user_bans = (False, boolean)
|
||||
)
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.get = DotDict()
|
||||
self.put = DotDict()
|
||||
self.delete = DotDict()
|
||||
|
||||
self._set_commands('get', get)
|
||||
self._set_commands('put', put)
|
||||
self._set_commands('delete', delete)
|
||||
|
||||
|
||||
def _set_commands(self, name, mod):
|
||||
for method in dir(mod):
|
||||
if method.startswith('cmd_'):
|
||||
getattr(self, name)[method[4:]] = partial(getattr(mod, method), self)
|
||||
|
||||
|
||||
db = SqlDatabase(
|
||||
config.dbtype,
|
||||
tables = tables,
|
||||
|
|
|
@ -5,6 +5,26 @@ from izzylib import DotDict, logging
|
|||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
def cmd_ban(self, handle=None, domain=None, reason=None):
|
||||
row = self.get.ban(handle, domain)
|
||||
|
||||
if row:
|
||||
if reason:
|
||||
self.update(row=row, reason=reason)
|
||||
|
||||
else:
|
||||
logging.verbose('Banned user or instance already exists')
|
||||
return
|
||||
|
||||
else:
|
||||
self.insert('ban',
|
||||
handle = handle,
|
||||
domain = domain,
|
||||
reason = reason,
|
||||
timestamp = datetime.now()
|
||||
)
|
||||
|
||||
|
||||
def cmd_actor(self, url, actor):
|
||||
row = self.get.actor(url)
|
||||
|
||||
|
@ -65,3 +85,16 @@ def cmd_instance(self, inbox, actor, followid=None):
|
|||
)
|
||||
|
||||
return row
|
||||
|
||||
|
||||
def cmd_whitelist(self, domain):
|
||||
row = self.fetch('whitelist', domain=domain)
|
||||
|
||||
if row:
|
||||
logging.verbose(f'Domain already in the whitelist: {domain}')
|
||||
return
|
||||
|
||||
self.insert('whitelist',
|
||||
domain = domain,
|
||||
timestamp = datetime.now()
|
||||
)
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import sys
|
||||
import json, sys, yaml
|
||||
|
||||
from izzylib import logging
|
||||
from izzylib import DotDict, Path, boolean, logging, prompt
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from . import __version__
|
||||
from .config import dbconfig, config, write_config
|
||||
from .database import db
|
||||
from .functions import fetch_actor, get_inbox
|
||||
from .messages import Message
|
||||
|
@ -239,6 +240,78 @@ python3 -m uncia.manage rules remove <rule number>:
|
|||
return f'Removed rule: {rule_text}'
|
||||
|
||||
|
||||
def cmd_convert(self, relay='uncia'):
|
||||
if relay.lower() == 'uncia':
|
||||
return self.convert_uncia()
|
||||
|
||||
if relay.lower() == 'pleroma':
|
||||
return self.convert_pleroma()
|
||||
|
||||
|
||||
def convert_uncia(self):
|
||||
pass
|
||||
|
||||
|
||||
def convert_pleroma(self):
|
||||
try:
|
||||
pl_cfg = load_pleroma_relay_config()
|
||||
pl_db = DotDict()
|
||||
pl_db.load_json(pl_cfg.db)
|
||||
|
||||
except FileNotFoundError as e:
|
||||
return f'Error when loading config or database: {e}'
|
||||
|
||||
config.listen = pl_cfg.listen
|
||||
config.port = pl_cfg.port
|
||||
config.host = pl_cfg.host
|
||||
|
||||
write_config()
|
||||
|
||||
for instance in pl_db['relay-list']:
|
||||
self.cmd_add(urlparse(instance).netloc)
|
||||
|
||||
with db.session as s:
|
||||
s.put.config('privkey', pl_db.actorKeys.privateKey)
|
||||
s.put.config('pubkey', pl_db.actorKeys.publicKey)
|
||||
s.put.config('whitelist', pl_cfg.whitelist_enabled)
|
||||
s.put.config('description', pl_cfg.note)
|
||||
s.put.config('blocked_software', pl_cfg.blocked_software)
|
||||
|
||||
for domain in pl_cfg.blocked_instances:
|
||||
s.put.ban(domain=domain)
|
||||
|
||||
for domain in pl_cfg.whitelist:
|
||||
s.put.whitelist(domain)
|
||||
|
||||
delete_old = prompt('Config and database successfully converted. Delete old files?', default='no', valtype=boolean, options=['yes', 'no'])
|
||||
|
||||
if delete_old:
|
||||
Path('relay.yaml').delete()
|
||||
pl_cfg.db.delete()
|
||||
|
||||
|
||||
def load_pleroma_relay_config():
|
||||
with open('relay.yaml') as f:
|
||||
yaml_file = yaml.load(f, Loader=yaml.FullLoader)
|
||||
|
||||
if not yaml_file.get('ap'):
|
||||
raise ValueError('Missing AP section in Pleroma Relay config')
|
||||
|
||||
config = DotDict({
|
||||
'db': Path(yaml_file.get('db', 'relay.jsonld')).resolve,
|
||||
'listen': yaml_file.get('listen', '127.0.0.1'),
|
||||
'port': int(yaml_file.get('port', 3621)),
|
||||
'note': yaml_file.get('note'),
|
||||
'blocked_software': [v.lower() for v in yaml_file['ap'].get('blocked_software', [])],
|
||||
'blocked_instances': yaml_file['ap'].get('blocked_instances', []),
|
||||
'host': yaml_file['ap'].get('host', 'localhost'),
|
||||
'whitelist': yaml_file['ap'].get('whitelist', []),
|
||||
'whitelist_enabled': yaml_file['ap'].get('whitelist_enabled', False)
|
||||
})
|
||||
|
||||
return config
|
||||
|
||||
|
||||
class InvalidCommandError(Exception):
|
||||
pass
|
||||
|
||||
|
|
|
@ -142,6 +142,8 @@ class UnciaNodeinfo(View):
|
|||
instances = s.get.instance_list('domain')
|
||||
domainbans = [row.domain for row in s.get.ban_list('domain')]
|
||||
userbans = [f"{row.handle}@{row.domain}" for row in s.get.ban_list('user')]
|
||||
whitelist = [row.domain for row in s.search('whitelist')]
|
||||
wl_enabled = s.get.config('whitelist')
|
||||
|
||||
data = {
|
||||
'openRegistrations': True,
|
||||
|
@ -157,7 +159,7 @@ class UnciaNodeinfo(View):
|
|||
'usage': {
|
||||
'localPosts': 0,
|
||||
'users': {
|
||||
'total': len(instances)
|
||||
'total': 1
|
||||
}
|
||||
},
|
||||
'version': '2.0',
|
||||
|
@ -167,7 +169,8 @@ class UnciaNodeinfo(View):
|
|||
'email': s.get.config('email'),
|
||||
'federation': {
|
||||
'instance_blocks': False if not s.get.config('show_domain_bans') else domainbans,
|
||||
'user_blocks': False if not s.get.config('show_user_bans') else userbans
|
||||
'user_blocks': False if not s.get.config('show_user_bans') else userbans,
|
||||
'whitelist': whitelist if s.get.config('show_whitelist') and wl_enabled else wl_enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue