From 3b079526a2ea78b68555094ca498faea31022759 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 27 Nov 2022 17:05:31 -0700 Subject: User fetching and inbox message cleaning --- users/models/identity.py | 20 ++++++++++++++++---- users/models/inbox_message.py | 10 +++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'users/models') diff --git a/users/models/identity.py b/users/models/identity.py index 805755a..6957526 100644 --- a/users/models/identity.py +++ b/users/models/identity.py @@ -23,19 +23,31 @@ from users.models.system_actor import SystemActor class IdentityStates(StateGraph): - outdated = State(try_interval=3600) - updated = State() + """ + There are only two states in a cycle. + Identities sit in "updated" for up to system.identity_max_age, and then + go back to "outdated" for refetching. + """ + + outdated = State(try_interval=3600, force_initial=True) + updated = State(try_interval=86400, attempt_immediately=False) outdated.transitions_to(updated) + updated.transitions_to(outdated) @classmethod async def handle_outdated(cls, identity: "Identity"): # Local identities never need fetching if identity.local: - return "updated" + return cls.updated # Run the actor fetch and progress to updated if it succeeds if await identity.fetch_actor(): - return "updated" + return cls.updated + + @classmethod + async def handle_updated(cls, instance: "Identity"): + if instance.state_age > Config.system.identity_max_age: + return cls.outdated class Identity(StatorModel): diff --git a/users/models/inbox_message.py b/users/models/inbox_message.py index 589f933..079c572 100644 --- a/users/models/inbox_message.py +++ b/users/models/inbox_message.py @@ -1,14 +1,17 @@ from asgiref.sync import sync_to_async from django.db import models +from core.models import Config from stator.models import State, StateField, StateGraph, StatorModel class InboxMessageStates(StateGraph): received = State(try_interval=300) - processed = State() + processed = State(try_interval=86400, attempt_immediately=False) + purged = State() # This is actually deletion, it will never get here received.transitions_to(processed) + processed.transitions_to(purged) @classmethod async def handle_received(cls, instance: "InboxMessage"): @@ -80,6 +83,11 @@ class InboxMessageStates(StateGraph): raise ValueError(f"Cannot handle activity of type {unknown}") return cls.processed + @classmethod + async def handle_processed(cls, instance: "InboxMessage"): + if instance.state_age > Config.system.inbox_message_purge_after: + await InboxMessage.objects.filter(pk=instance.pk).adelete() + class InboxMessage(StatorModel): """ -- cgit v1.2.3