From 2c362e32f4c95bcde5ee8898524a815f068545a8 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:27:46 +1200 Subject: [PATCH] remove content from communities according to retention setting #254 --- app/cli.py | 12 +++++++++++- app/models.py | 3 --- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/cli.py b/app/cli.py index f089c2e3..a7ce8d52 100644 --- a/app/cli.py +++ b/app/cli.py @@ -23,7 +23,7 @@ from app.constants import NOTIF_COMMUNITY, NOTIF_POST, NOTIF_REPLY from app.email import send_verification_email, send_email from app.models import Settings, BannedInstances, Interest, Role, User, RolePermission, Domain, ActivityPubLog, \ utcnow, Site, Instance, File, Notification, Post, CommunityMember, NotificationSubscription, PostReply, Language, \ - Tag, InstanceRole + Tag, InstanceRole, Community from app.utils import file_get_contents, retrieve_block_list, blocked_domains, retrieve_peertube_block_list, \ shorten_string, get_request, html_to_text, blocked_communities @@ -171,6 +171,15 @@ def register(app): @app.cli.command('daily-maintenance') def daily_maintenance(): with app.app_context(): + # Remove old content from communities + communities = Community.query.filter(Community.content_retention > 0).all() + for community in communities: + cut_off = utcnow() - timedelta(days=community.content_retention) + db.session.execute(text('UPDATE "post" SET deleted = true WHERE posted_at < :cut_off AND community_id = :community_id'), { + 'cut_off': cut_off, + 'community_id': community.id + }) + # Remove activity older than 3 days db.session.query(ActivityPubLog).filter(ActivityPubLog.created_at < utcnow() - timedelta(days=3)).delete() db.session.commit() @@ -280,6 +289,7 @@ def register(app): db.session.delete(post_reply) for post in Post.query.filter(Post.deleted == True, Post.posted_at < utcnow() - timedelta(days=7)).all(): + post.delete_dependencies() db.session.delete(post) db.session.commit() diff --git a/app/models.py b/app/models.py index 89adca95..acf63bd3 100644 --- a/app/models.py +++ b/app/models.py @@ -343,8 +343,6 @@ class Topic(db.Model): return existing_notification is not None - - class Community(db.Model): query_class = FullTextSearchQuery id = db.Column(db.Integer, primary_key=True) @@ -573,7 +571,6 @@ class Community(db.Model): db.session.query(Report).filter(Report.suspect_community_id == self.id).delete() - user_role = db.Table('user_role', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('role_id', db.Integer, db.ForeignKey('role.id')),