takahe/users/management/commands/pruneidentities.py

54 lines
1.7 KiB
Python
Raw Normal View History

import sys
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.utils import timezone
from users.models import Identity
class Command(BaseCommand):
help = "Prunes identities that have no local interaction"
def add_arguments(self, parser):
parser.add_argument(
"--number",
"-n",
type=int,
2023-11-13 01:32:38 +00:00
default=500,
help="The maximum number of identities to prune at once",
)
def handle(self, number: int, *args, **options):
if not settings.SETUP.REMOTE_PRUNE_HORIZON:
print("Pruning has been disabled as REMOTE_PRUNE_HORIZON=0")
sys.exit(2)
# Find a set of identities that match the initial criteria
print(f"Running query to find up to {number} unused identities...")
identities = Identity.objects.filter(
local=False,
created__lt=timezone.now(),
).exclude(
Q(interactions__post__local=True)
| Q(posts__isnull=False)
2023-11-13 01:06:29 +00:00
| Q(posts_mentioning__isnull=False)
| Q(outbound_follows__isnull=False)
| Q(inbound_follows__isnull=False)
| Q(outbound_blocks__isnull=False)
| Q(inbound_blocks__isnull=False)
)[
:number
]
identity_ids = identities.values_list("id", flat=True)
print(f" found {len(identity_ids)}")
2023-11-13 01:06:29 +00:00
if not identity_ids:
sys.exit(1)
# Delete them
print("Deleting...")
number_deleted, deleted = Identity.objects.filter(id__in=identity_ids).delete()
print("Deleted:")
for model, model_deleted in deleted.items():
print(f" {model}: {model_deleted}")