2023-11-26 23:21:04 +13:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2023-11-03 21:59:48 +13:00
|
|
|
from flask import request, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_babel import _
|
2023-11-24 22:32:46 +13:00
|
|
|
from sqlalchemy import text, desc
|
2023-11-03 21:59:48 +13:00
|
|
|
|
|
|
|
from app import db
|
|
|
|
from app.admin.forms import AdminForm
|
2023-11-24 22:32:46 +13:00
|
|
|
from app.models import AllowedInstances, BannedInstances, ActivityPubLog
|
2023-11-03 21:59:48 +13:00
|
|
|
from app.utils import render_template, permission_required, set_setting, get_setting
|
|
|
|
from app.admin import bp
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@permission_required('change instance settings')
|
|
|
|
def admin_home():
|
|
|
|
form = AdminForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if form.use_allowlist.data:
|
|
|
|
set_setting('use_allowlist', True)
|
|
|
|
db.session.execute(text('DELETE FROM allowed_instances'))
|
|
|
|
for allow in form.allowlist.data.split('\n'):
|
|
|
|
if allow.strip():
|
|
|
|
db.session.add(AllowedInstances(domain=allow.strip()))
|
|
|
|
if form.use_blocklist.data:
|
|
|
|
set_setting('use_allowlist', False)
|
|
|
|
db.session.execute(text('DELETE FROM banned_instances'))
|
|
|
|
for banned in form.blocklist.data.split('\n'):
|
|
|
|
if banned.strip():
|
|
|
|
db.session.add(BannedInstances(domain=banned.strip()))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Admin settings saved'))
|
|
|
|
|
|
|
|
elif request.method == 'GET':
|
|
|
|
form.use_allowlist.data = get_setting('use_allowlist', False)
|
|
|
|
form.use_blocklist.data = not form.use_allowlist.data
|
|
|
|
instances = BannedInstances.query.all()
|
|
|
|
form.blocklist.data = '\n'.join([instance.domain for instance in instances])
|
|
|
|
instances = AllowedInstances.query.all()
|
|
|
|
form.allowlist.data = '\n'.join([instance.domain for instance in instances])
|
|
|
|
|
|
|
|
return render_template('admin/home.html', title=_('Admin settings'), form=form)
|
|
|
|
|
2023-11-24 22:32:46 +13:00
|
|
|
|
|
|
|
@bp.route('/activities', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@permission_required('change instance settings')
|
|
|
|
def admin_activities():
|
2023-11-26 23:21:04 +13:00
|
|
|
db.session.query(ActivityPubLog).filter(
|
|
|
|
ActivityPubLog.created_at < datetime.utcnow() - timedelta(days=3)).delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-11-24 22:32:46 +13:00
|
|
|
return render_template('admin/activities.html', title=_('ActivityPub Log'),
|
|
|
|
activities=ActivityPubLog.query.order_by(desc(ActivityPubLog.created_at)).all())
|