takahe/users/models/follow.py

434 lines
14 KiB
Python
Raw Normal View History

import logging
from typing import Optional
2022-12-17 19:29:48 +00:00
import httpx
2022-11-18 01:52:00 +00:00
from django.db import models, transaction
2022-11-05 23:51:54 +00:00
from core.ld import canonicalise, get_str_or_id
from core.snowflake import Snowflake
from stator.models import State, StateField, StateGraph, StatorModel
2023-08-18 06:19:45 +00:00
from users.models.block import Block
2022-11-12 05:02:43 +00:00
from users.models.identity import Identity
2023-08-18 06:19:45 +00:00
from users.models.inbox_message import InboxMessage
logger = logging.getLogger(__name__)
2022-11-05 23:51:54 +00:00
class FollowStates(StateGraph):
2022-12-20 07:10:31 +00:00
unrequested = State(try_interval=600)
2023-08-18 06:19:45 +00:00
pending_approval = State(externally_progressed=True)
accepting = State(try_interval=24 * 60 * 60)
rejecting = State(try_interval=24 * 60 * 60)
accepted = State(externally_progressed=True)
2023-08-18 06:19:45 +00:00
undone = State(try_interval=24 * 60 * 60)
pending_removal = State(try_interval=60 * 60)
removed = State(delete_after=1)
unrequested.transitions_to(pending_approval)
unrequested.transitions_to(accepting)
unrequested.transitions_to(rejecting)
unrequested.times_out_to(removed, seconds=24 * 60 * 60)
pending_approval.transitions_to(accepting)
pending_approval.transitions_to(rejecting)
pending_approval.transitions_to(pending_removal)
accepting.transitions_to(accepted)
accepting.times_out_to(accepted, seconds=7 * 24 * 60 * 60)
rejecting.transitions_to(pending_removal)
rejecting.times_out_to(pending_removal, seconds=24 * 60 * 60)
accepted.transitions_to(rejecting)
2022-11-17 05:23:32 +00:00
accepted.transitions_to(undone)
2023-08-18 06:19:45 +00:00
undone.transitions_to(pending_removal)
pending_removal.transitions_to(removed)
2022-11-18 03:04:01 +00:00
@classmethod
def group_active(cls):
2022-11-12 05:02:43 +00:00
"""
2023-08-18 06:19:45 +00:00
Follows that are active means they are being handled and no need to re-request
2022-11-12 05:02:43 +00:00
"""
2023-08-18 06:19:45 +00:00
return [cls.unrequested, cls.pending_approval, cls.accepting, cls.accepted]
@classmethod
2023-08-18 06:19:45 +00:00
def group_accepted(cls):
"""
Follows that are accepting/accepted means they should be consider accepted when deliver to followers
"""
return [cls.accepting, cls.accepted]
@classmethod
2023-08-18 06:19:45 +00:00
def handle_unrequested(cls, instance: "Follow"):
2022-11-12 05:02:43 +00:00
"""
2023-08-18 06:19:45 +00:00
Follows start unrequested as their initial state regardless of local/remote
2022-11-12 05:02:43 +00:00
"""
2023-08-18 06:19:45 +00:00
if Block.maybe_get(
source=instance.target, target=instance.source, require_active=True
):
return cls.rejecting
if not instance.target.local:
if not instance.source.local:
# remote follow remote, invalid case
return cls.removed
# local follow remote, send Follow to target server
# Don't try if the other identity didn't fetch yet
if not instance.target.inbox_uri:
return
# Sign it and send it
try:
instance.source.signed_request(
method="post",
uri=instance.target.inbox_uri,
body=canonicalise(instance.to_ap()),
)
except httpx.RequestError:
return
return cls.pending_approval
# local/remote follow local, check manually_approve
if instance.target.manually_approves_followers:
from activities.models import TimelineEvent
TimelineEvent.add_follow_request(instance.target, instance.source)
return cls.pending_approval
return cls.accepting
@classmethod
def handle_accepting(cls, instance: "Follow"):
if not instance.source.local:
# send an Accept object to the source server
try:
instance.target.signed_request(
method="post",
uri=instance.source.inbox_uri,
body=canonicalise(instance.to_accept_ap()),
)
except httpx.RequestError:
return
from activities.models import TimelineEvent
TimelineEvent.add_follow(instance.target, instance.source)
return cls.accepted
2023-08-18 06:19:45 +00:00
@classmethod
def handle_rejecting(cls, instance: "Follow"):
if not instance.source.local:
# send a Reject object to the source server
try:
instance.target.signed_request(
method="post",
uri=instance.source.inbox_uri,
body=canonicalise(instance.to_reject_ap()),
)
except httpx.RequestError:
return
return cls.pending_removal
@classmethod
def handle_undone(cls, instance: "Follow"):
2022-11-12 05:02:43 +00:00
"""
Delivers the Undo object to the target server
"""
2022-12-17 19:29:48 +00:00
try:
2023-08-18 06:19:45 +00:00
if not instance.target.local:
instance.source.signed_request(
method="post",
uri=instance.target.inbox_uri,
body=canonicalise(instance.to_undo_ap()),
)
2022-12-17 19:29:48 +00:00
except httpx.RequestError:
return
2023-08-18 06:19:45 +00:00
return cls.pending_removal
@classmethod
def handle_pending_removal(cls, instance: "Follow"):
if instance.target.local:
from activities.models import TimelineEvent
TimelineEvent.delete_follow(instance.target, instance.source)
return cls.removed
2023-01-15 20:35:45 +00:00
class FollowQuerySet(models.QuerySet):
def active(self):
query = self.filter(state__in=FollowStates.group_active())
return query
2023-08-18 06:19:45 +00:00
def accepted(self):
query = self.filter(state__in=FollowStates.group_accepted())
return query
2023-01-15 20:35:45 +00:00
class FollowManager(models.Manager):
def get_queryset(self):
return FollowQuerySet(self.model, using=self._db)
def active(self):
return self.get_queryset().active()
2023-08-18 06:19:45 +00:00
def accepted(self):
return self.get_queryset().accepted()
2023-01-15 20:35:45 +00:00
class Follow(StatorModel):
2022-11-05 23:51:54 +00:00
"""
When one user (the source) follows other (the target)
2022-11-05 23:51:54 +00:00
"""
id = models.BigIntegerField(primary_key=True, default=Snowflake.generate_follow)
2022-11-05 23:51:54 +00: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",
)
boosts = models.BooleanField(
default=True, help_text="Also follow boosts from this user"
)
uri = models.CharField(blank=True, null=True, max_length=500)
2022-11-05 23:51:54 +00:00
note = models.TextField(blank=True, null=True)
state = StateField(FollowStates)
2022-11-05 23:51:54 +00:00
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
2023-01-15 20:35:45 +00:00
objects = FollowManager()
class Meta:
unique_together = [("source", "target")]
indexes: list = [] # We need this so Stator can add its own
2022-11-12 05:02:43 +00:00
def __str__(self):
return f"#{self.id}: {self.source}{self.target}"
### Alternate fetchers/constructors ###
@classmethod
2023-01-15 20:35:45 +00:00
def maybe_get(cls, source, target, require_active=False) -> Optional["Follow"]:
"""
Returns a follow if it exists between source and target
"""
try:
2023-01-15 20:35:45 +00:00
if require_active:
return Follow.objects.active().get(source=source, target=target)
else:
return Follow.objects.get(source=source, target=target)
except Follow.DoesNotExist:
return None
@classmethod
def create_local(cls, source, target, boosts=True):
"""
Creates a Follow from a local Identity to the target
(which can be local or remote).
"""
if not source.local:
raise ValueError("You cannot initiate follows from a remote Identity")
try:
follow = Follow.objects.get(source=source, target=target)
2023-01-15 20:35:45 +00:00
if not follow.active:
2023-08-18 06:19:45 +00:00
follow.state = FollowStates.unrequested
2023-01-15 20:35:45 +00:00
follow.boosts = boosts
follow.save()
2023-01-15 20:35:45 +00:00
except Follow.DoesNotExist:
with transaction.atomic():
follow = Follow.objects.create(
source=source,
target=target,
boosts=boosts,
uri="",
2023-08-18 06:19:45 +00:00
state=FollowStates.unrequested,
2023-01-15 20:35:45 +00:00
)
follow.uri = source.actor_uri + f"follow/{follow.pk}/"
follow.save()
return follow
2023-01-15 20:35:45 +00:00
### Properties ###
2022-12-22 05:54:01 +00:00
2023-01-15 20:35:45 +00:00
@property
def active(self):
return self.state in FollowStates.group_active()
2023-08-18 06:19:45 +00:00
@property
def accepted(self):
return self.state in FollowStates.group_accepted()
2022-11-12 05:02:43 +00:00
### ActivityPub (outbound) ###
def to_ap(self):
"""
Returns the AP JSON for this object
"""
return {
"type": "Follow",
"id": self.uri,
"actor": self.source.actor_uri,
"object": self.target.actor_uri,
}
def to_accept_ap(self):
"""
Returns the AP JSON for this objects' accept.
"""
return {
"type": "Accept",
"id": self.uri + "#accept",
2022-11-12 05:02:43 +00:00
"actor": self.target.actor_uri,
"object": self.to_ap(),
}
2023-08-18 06:19:45 +00:00
def to_reject_ap(self):
"""
Returns the AP JSON for this objects' rejection.
"""
return {
"type": "Reject",
"id": self.uri + "#reject",
"actor": self.target.actor_uri,
"object": self.to_ap(),
}
2022-11-12 05:02:43 +00:00
def to_undo_ap(self):
"""
Returns the AP JSON for this objects' undo.
"""
return {
"type": "Undo",
"id": self.uri + "#undo",
"actor": self.source.actor_uri,
"object": self.to_ap(),
}
### ActivityPub (inbound) ###
@classmethod
2023-01-15 20:35:45 +00:00
def by_ap(cls, data: str | dict, create=False) -> "Follow":
2022-11-12 05:02:43 +00:00
"""
2023-01-15 20:35:45 +00:00
Retrieves a Follow instance by its ActivityPub JSON object or its URI.
2022-11-12 05:02:43 +00:00
Optionally creates one if it's not present.
2023-01-15 20:35:45 +00:00
Raises DoesNotExist if it's not found and create is False.
2022-11-12 05:02:43 +00:00
"""
2023-01-15 20:35:45 +00:00
# If it's a string, do the reference resolve
if isinstance(data, str):
bits = data.strip("/").split("/")
if bits[-2] != "follow":
raise ValueError(f"Unknown Follow object URI: {data}")
return Follow.objects.get(pk=bits[-1])
# Otherwise, do object resolve
2022-11-12 05:02:43 +00:00
else:
2023-01-15 20:35:45 +00:00
# Resolve source and target and see if a Follow exists
source = Identity.by_actor_uri(data["actor"], create=create)
target = Identity.by_actor_uri(get_str_or_id(data["object"]))
follow = cls.maybe_get(source=source, target=target)
2023-08-18 06:19:45 +00:00
# If it doesn't exist, create one in the unrequested state
2023-01-15 20:35:45 +00:00
if follow is None:
if create:
return cls.objects.create(
source=source,
target=target,
uri=data["id"],
2023-08-18 06:19:45 +00:00
state=FollowStates.unrequested,
2023-01-15 20:35:45 +00:00
)
else:
raise cls.DoesNotExist(
f"No follow with source {source} and target {target}", data
)
else:
return follow
@classmethod
2022-11-12 05:02:43 +00:00
def handle_request_ap(cls, data):
"""
Handles an incoming follow request
"""
2022-11-18 01:52:00 +00:00
with transaction.atomic():
try:
follow = cls.by_ap(data, create=True)
except Identity.DoesNotExist:
logger.info(
"Identity not found for incoming Follow", extra={"data": data}
)
return
2023-08-18 06:19:45 +00:00
if follow.state == FollowStates.accepted:
# Likely the source server missed the Accept, send it back again
follow.transition_perform(FollowStates.accepting)
2022-11-12 05:02:43 +00:00
@classmethod
def handle_accept_ap(cls, data):
"""
Handles an incoming Follow Accept for one of our follows
"""
# Resolve source and target and see if a Follow exists (it really should)
try:
follow = cls.by_ap(data["object"])
except (cls.DoesNotExist, Identity.DoesNotExist):
logger.info(
"Follow or Identity not found for incoming Accept",
extra={"data": data},
)
return
2023-01-15 20:35:45 +00:00
# Ensure the Accept actor is the Follow's target
if data["actor"] != follow.target.actor_uri:
raise ValueError("Accept actor does not match its Follow object", data)
2022-11-12 05:02:43 +00:00
# If the follow was waiting to be accepted, transition it
2023-08-18 06:19:45 +00:00
if follow and follow.state == FollowStates.pending_approval:
follow.transition_perform(FollowStates.accepting)
2022-11-12 05:02:43 +00:00
@classmethod
2023-01-15 20:35:45 +00:00
def handle_reject_ap(cls, data):
"""
2023-01-15 20:35:45 +00:00
Handles an incoming Follow Reject for one of our follows
"""
2023-01-15 20:35:45 +00:00
# Resolve source and target and see if a Follow exists (it really should)
try:
follow = cls.by_ap(data["object"])
except (cls.DoesNotExist, Identity.DoesNotExist):
logger.info(
"Follow or Identity not found for incoming Reject",
extra={"data": data},
)
return
2023-01-15 20:35:45 +00:00
# Ensure the Accept actor is the Follow's target
if data["actor"] != follow.target.actor_uri:
2023-01-15 20:35:45 +00:00
raise ValueError("Reject actor does not match its Follow object", data)
2023-08-18 06:19:45 +00:00
# Clear timeline if remote target remove local source from their previously accepted follows
if follow.accepted:
InboxMessage.create_internal(
{
"type": "ClearTimeline",
"object": follow.target.pk,
"actor": follow.source.pk,
}
)
2023-01-15 20:35:45 +00:00
# Mark the follow rejected
2023-08-18 06:19:45 +00:00
follow.transition_perform(FollowStates.rejecting)
2022-11-12 05:02:43 +00:00
@classmethod
def handle_undo_ap(cls, data):
"""
Handles an incoming Follow Undo for one of our follows
"""
# Resolve source and target and see if a Follow exists (it hopefully does)
try:
follow = cls.by_ap(data["object"])
except (cls.DoesNotExist, Identity.DoesNotExist):
logger.info(
"Follow or Identity not found for incoming Undo", extra={"data": data}
)
return
2023-01-15 20:35:45 +00:00
# Ensure the Undo actor is the Follow's source
if data["actor"] != follow.source.actor_uri:
raise ValueError("Accept actor does not match its Follow object", data)
2022-11-12 05:02:43 +00:00
# Delete the follow
2023-08-18 06:19:45 +00:00
follow.transition_perform(FollowStates.pending_removal)