remove content from communities according to retention setting #254

This commit is contained in:
rimu 2024-08-15 20:27:46 +12:00
parent 8d60f8f4cd
commit 2c362e32f4
2 changed files with 11 additions and 4 deletions

View file

@ -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()

View file

@ -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')),