2022-11-07 05:30:07 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-11-06 00:51:54 +01:00
|
|
|
from django.db import models
|
|
|
|
|
2022-11-09 07:06:29 +01:00
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
2022-11-07 05:30:07 +01:00
|
|
|
|
2022-11-06 00:51:54 +01:00
|
|
|
|
2022-11-09 07:06:29 +01:00
|
|
|
class FollowStates(StateGraph):
|
|
|
|
pending = State(try_interval=3600)
|
|
|
|
requested = State()
|
|
|
|
accepted = State()
|
|
|
|
|
|
|
|
@pending.add_transition(requested)
|
|
|
|
async def try_request(cls, instance):
|
|
|
|
print("Would have tried to follow")
|
|
|
|
return False
|
|
|
|
|
|
|
|
requested.add_manual_transition(accepted)
|
|
|
|
|
|
|
|
|
|
|
|
class Follow(StatorModel):
|
2022-11-06 00:51:54 +01:00
|
|
|
"""
|
2022-11-06 21:48:04 +01:00
|
|
|
When one user (the source) follows other (the target)
|
2022-11-06 00:51:54 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
source = models.ForeignKey(
|
|
|
|
"users.Identity",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="outbound_follows",
|
|
|
|
)
|
|
|
|
target = models.ForeignKey(
|
|
|
|
"users.Identity",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="inbound_follows",
|
|
|
|
)
|
|
|
|
|
2022-11-07 05:30:07 +01:00
|
|
|
uri = models.CharField(blank=True, null=True, max_length=500)
|
2022-11-06 00:51:54 +01:00
|
|
|
note = models.TextField(blank=True, null=True)
|
|
|
|
|
2022-11-09 07:06:29 +01:00
|
|
|
state = StateField(FollowStates)
|
2022-11-07 05:30:07 +01:00
|
|
|
|
2022-11-06 00:51:54 +01:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
2022-11-07 05:30:07 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = [("source", "target")]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def maybe_get(cls, source, target) -> Optional["Follow"]:
|
|
|
|
"""
|
|
|
|
Returns a follow if it exists between source and target
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return Follow.objects.get(source=source, target=target)
|
|
|
|
except Follow.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_local(cls, source, target):
|
|
|
|
"""
|
|
|
|
Creates a Follow from a local Identity to the target
|
|
|
|
(which can be local or remote).
|
|
|
|
"""
|
|
|
|
if not source.local:
|
2022-11-09 07:06:29 +01:00
|
|
|
raise ValueError("You cannot initiate follows from a remote Identity")
|
2022-11-07 05:30:07 +01:00
|
|
|
try:
|
|
|
|
follow = Follow.objects.get(source=source, target=target)
|
|
|
|
except Follow.DoesNotExist:
|
|
|
|
follow = Follow.objects.create(source=source, target=target, uri="")
|
|
|
|
follow.uri = source.actor_uri + f"follow/{follow.pk}/"
|
2022-11-09 07:06:29 +01:00
|
|
|
# TODO: Local follow approvals
|
2022-11-07 05:30:07 +01:00
|
|
|
if target.local:
|
2022-11-09 07:06:29 +01:00
|
|
|
follow.state = FollowStates.accepted
|
2022-11-07 05:30:07 +01:00
|
|
|
follow.save()
|
|
|
|
return follow
|
|
|
|
|
|
|
|
def undo(self):
|
|
|
|
"""
|
|
|
|
Undoes this follow
|
|
|
|
"""
|
|
|
|
if not self.target.local:
|
|
|
|
Task.submit("follow_undo", str(self.pk))
|
|
|
|
self.delete()
|