From e2b86f3cafdb8e6d605eec838b01ab20acfe6950 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 26 Nov 2023 23:21:04 +1300 Subject: [PATCH] keep activitypub log small --- app/admin/routes.py | 6 ++++++ app/auth/routes.py | 6 +++++- app/cli.py | 12 +++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/admin/routes.py b/app/admin/routes.py index b6bb382c..9fad1391 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -1,3 +1,5 @@ +from datetime import datetime, timedelta + from flask import request, flash from flask_login import login_required, current_user from flask_babel import _ @@ -46,5 +48,9 @@ def admin_home(): @login_required @permission_required('change instance settings') def admin_activities(): + db.session.query(ActivityPubLog).filter( + ActivityPubLog.created_at < datetime.utcnow() - timedelta(days=3)).delete() + db.session.commit() + return render_template('admin/activities.html', title=_('ActivityPub Log'), activities=ActivityPubLog.query.order_by(desc(ActivityPubLog.created_at)).all()) \ No newline at end of file diff --git a/app/auth/routes.py b/app/auth/routes.py index 092d62ae..8aa2b0e2 100644 --- a/app/auth/routes.py +++ b/app/auth/routes.py @@ -71,6 +71,7 @@ def register(): flash(_('Sorry, you cannot use that email address'), 'error') else: verification_token = random_token(16) + form.user_name.data = form.user_name.data.strip() user = User(user_name=form.user_name.data, email=form.real_email.data, verification_token=verification_token) user.set_password(form.password.data) @@ -146,8 +147,11 @@ def verify_email(token): private_key, public_key = RsaKeys.generate_keypair() user.private_key = private_key user.public_key = public_key + user.ap_profile_id = f"https://{current_app.config['SERVER_NAME']}/u/{user.user_name}" + user.ap_public_url = f"https://{current_app.config['SERVER_NAME']}/u/{user.user_name}" + user.ap_inbox_url = f"https://{current_app.config['SERVER_NAME']}/u/{user.user_name}/inbox" db.session.commit() - flash(_('Thank you for verifying your email address. You can now post content and vote.')) + flash(_('Thank you for verifying your email address.')) else: flash(_('Email address validation failed.'), 'error') return redirect(url_for('main.index')) diff --git a/app/cli.py b/app/cli.py index 7a3d960b..4ae195dc 100644 --- a/app/cli.py +++ b/app/cli.py @@ -1,5 +1,7 @@ # if commands in this file are not working (e.g. 'flask translate') make sure you set the FLASK_APP environment variable. # e.g. export FLASK_APP=pyfedi.py +from datetime import datetime, timedelta + from flask import json from app import db @@ -8,7 +10,7 @@ import os from app.auth.email import send_verification_email from app.auth.util import random_token -from app.models import Settings, BannedInstances, Interest, Role, User, RolePermission, Domain +from app.models import Settings, BannedInstances, Interest, Role, User, RolePermission, Domain, ActivityPubLog from app.utils import file_get_contents, retrieve_block_list @@ -117,6 +119,14 @@ def register(app): db.session.commit() print("Initial setup is finished.") + @app.cli.command('daily-maintenance') + def daily_maintenance(): + with app.app_context(): + """Remove activity older than 3 days""" + db.session.query(ActivityPubLog).filter( + ActivityPubLog.created_at < datetime.utcnow() - timedelta(days=3)).delete() + db.session.commit() + def parse_communities(interests_source, segment): lines = interests_source.split("\n")