From 7746abbbb7700fa918450101bbc6d29ed9b4b608 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 9 Nov 2022 22:29:33 -0700 Subject: Most of the way through the stator refactor --- users/models/__init__.py | 4 ++-- users/models/domain.py | 2 +- users/models/follow.py | 14 +++----------- users/models/identity.py | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 15 deletions(-) (limited to 'users/models') diff --git a/users/models/__init__.py b/users/models/__init__.py index e1877bc..d46003f 100644 --- a/users/models/__init__.py +++ b/users/models/__init__.py @@ -1,6 +1,6 @@ from .block import Block # noqa from .domain import Domain # noqa -from .follow import Follow # noqa -from .identity import Identity # noqa +from .follow import Follow, FollowStates # noqa +from .identity import Identity, IdentityStates # noqa from .user import User # noqa from .user_event import UserEvent # noqa diff --git a/users/models/domain.py b/users/models/domain.py index 8467ac3..4ac6ee9 100644 --- a/users/models/domain.py +++ b/users/models/domain.py @@ -55,7 +55,7 @@ class Domain(models.Model): return cls.objects.create(domain=domain, local=False) @classmethod - def get_local_domain(cls, domain: str) -> Optional["Domain"]: + def get_domain(cls, domain: str) -> Optional["Domain"]: try: return cls.objects.get( models.Q(domain=domain) | models.Q(service_domain=domain) diff --git a/users/models/follow.py b/users/models/follow.py index 04f90ee..3325a0b 100644 --- a/users/models/follow.py +++ b/users/models/follow.py @@ -6,13 +6,13 @@ from stator.models import State, StateField, StateGraph, StatorModel class FollowStates(StateGraph): - pending = State(try_interval=3600) + pending = State(try_interval=30) requested = State() accepted = State() @pending.add_transition(requested) - async def try_request(cls, instance): - print("Would have tried to follow") + async def try_request(instance: "Follow"): # type:ignore + print("Would have tried to follow on", instance) return False requested.add_manual_transition(accepted) @@ -73,11 +73,3 @@ class Follow(StatorModel): follow.state = FollowStates.accepted follow.save() return follow - - def undo(self): - """ - Undoes this follow - """ - if not self.target.local: - Task.submit("follow_undo", str(self.pk)) - self.delete() diff --git a/users/models/identity.py b/users/models/identity.py index 98262bc..5e2cd06 100644 --- a/users/models/identity.py +++ b/users/models/identity.py @@ -14,9 +14,21 @@ from django.utils import timezone from OpenSSL import crypto from core.ld import canonicalise +from stator.models import State, StateField, StateGraph, StatorModel from users.models.domain import Domain +class IdentityStates(StateGraph): + outdated = State(try_interval=3600) + updated = State() + + @outdated.add_transition(updated) + async def fetch_identity(identity: "Identity"): # type:ignore + if identity.local: + return True + return await identity.fetch_actor() + + def upload_namer(prefix, instance, filename): """ Names uploaded images etc. @@ -26,7 +38,7 @@ def upload_namer(prefix, instance, filename): return f"{prefix}/{now.year}/{now.month}/{now.day}/{filename}" -class Identity(models.Model): +class Identity(StatorModel): """ Represents both local and remote Fediverse identities (actors) """ @@ -35,6 +47,8 @@ class Identity(models.Model): # one around as well for making nice URLs etc. actor_uri = models.CharField(max_length=500, unique=True) + state = StateField(IdentityStates) + local = models.BooleanField() users = models.ManyToManyField("users.User", related_name="identities") -- cgit v1.2.3