From 2c3a1299709f2612e96c37e4e121c83ad4df7a56 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 9 Nov 2022 23:48:31 -0700 Subject: Profile fetching now working on state machine --- users/models/follow.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'users/models/follow.py') diff --git a/users/models/follow.py b/users/models/follow.py index 3325a0b..6f62481 100644 --- a/users/models/follow.py +++ b/users/models/follow.py @@ -6,16 +6,20 @@ from stator.models import State, StateField, StateGraph, StatorModel class FollowStates(StateGraph): - pending = State(try_interval=30) - requested = State() + unrequested = State(try_interval=30) + requested = State(try_interval=24 * 60 * 60) accepted = State() - @pending.add_transition(requested) - async def try_request(instance: "Follow"): # type:ignore + unrequested.transitions_to(requested) + requested.transitions_to(accepted) + + @classmethod + async def handle_unrequested(cls, instance: "Follow"): print("Would have tried to follow on", instance) - return False - requested.add_manual_transition(accepted) + @classmethod + async def handle_requested(cls, instance: "Follow"): + print("Would have tried to requested on", instance) class Follow(StatorModel): @@ -73,3 +77,17 @@ class Follow(StatorModel): follow.state = FollowStates.accepted follow.save() return follow + + @classmethod + def remote_created(cls, source, target, uri): + follow = cls.maybe_get(source=source, target=target) + if follow is None: + follow = Follow.objects.create(source=source, target=target, uri=uri) + if follow.state == FollowStates.fresh: + follow.transition_perform(FollowStates.requested) + + @classmethod + def remote_accepted(cls, source, target): + follow = cls.maybe_get(source=source, target=target) + if follow and follow.state == FollowStates.requested: + follow.transition_perform(FollowStates.accepted) -- cgit v1.2.3