2022-12-17 11:29:48 -08:00
|
|
|
import httpx
|
2022-11-11 22:04:43 -08:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from activities.models.timeline_event import TimelineEvent
|
|
|
|
from core.ld import canonicalise
|
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
2023-01-15 12:35:45 -08:00
|
|
|
from users.models import Block, FollowStates
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
class FanOutStates(StateGraph):
|
2022-12-19 22:23:50 -08:00
|
|
|
new = State(try_interval=600)
|
2023-01-08 12:51:30 -08:00
|
|
|
sent = State(delete_after=86400)
|
2023-01-15 12:35:45 -08:00
|
|
|
skipped = State(delete_after=86400)
|
2023-01-08 11:43:32 -08:00
|
|
|
failed = State(delete_after=86400)
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
new.transitions_to(sent)
|
2023-01-15 12:35:45 -08:00
|
|
|
new.transitions_to(skipped)
|
2022-12-19 22:23:50 -08:00
|
|
|
new.times_out_to(failed, seconds=86400 * 3)
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
@classmethod
|
2023-07-07 14:14:06 -07:00
|
|
|
def handle_new(cls, instance: "FanOut"):
|
2022-11-11 22:04:43 -08:00
|
|
|
"""
|
|
|
|
Sends the fan-out to the right inbox.
|
|
|
|
"""
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2022-12-19 23:01:30 -08:00
|
|
|
# Don't try to fan out to identities that are not fetched yet
|
2023-07-07 14:14:06 -07:00
|
|
|
if not (instance.identity.local or instance.identity.inbox_uri):
|
2022-12-19 23:01:30 -08:00
|
|
|
return
|
|
|
|
|
2023-07-07 14:14:06 -07:00
|
|
|
match (instance.type, instance.identity.local):
|
2022-11-27 10:09:46 -08:00
|
|
|
# Handle creating/updating local posts
|
2022-11-27 11:09:08 -08:00
|
|
|
case ((FanOut.Types.post | FanOut.Types.post_edited), True):
|
2023-07-07 14:14:06 -07:00
|
|
|
post = instance.subject_post
|
2023-01-15 12:35:45 -08:00
|
|
|
# If the author of the post is blocked or muted, skip out
|
|
|
|
if (
|
2023-07-07 14:14:06 -07:00
|
|
|
Block.objects.active()
|
|
|
|
.filter(source=instance.identity, target=post.author)
|
|
|
|
.exists()
|
2023-01-15 12:35:45 -08:00
|
|
|
):
|
|
|
|
return cls.skipped
|
2022-11-13 17:42:47 -08:00
|
|
|
# Make a timeline event directly
|
2022-11-25 17:20:59 -08:00
|
|
|
# If it's a reply, we only add it if we follow at least one
|
2022-12-20 06:20:11 -08:00
|
|
|
# of the people mentioned AND the author, or we're mentioned,
|
|
|
|
# or it's a reply to us or the author
|
2022-11-25 17:20:59 -08:00
|
|
|
add = True
|
|
|
|
mentioned = {identity.id for identity in post.mentions.all()}
|
|
|
|
if post.in_reply_to:
|
2023-07-07 14:14:06 -07:00
|
|
|
followed = set(
|
|
|
|
instance.identity.outbound_follows.filter(
|
2023-01-08 13:24:27 -08:00
|
|
|
state__in=FollowStates.group_active()
|
|
|
|
).values_list("target_id", flat=True)
|
|
|
|
)
|
2022-12-20 06:20:11 -08:00
|
|
|
interested_in = followed.union(
|
2023-07-07 14:14:06 -07:00
|
|
|
{post.author_id, instance.identity_id}
|
2022-12-20 06:20:11 -08:00
|
|
|
)
|
|
|
|
add = (post.author_id in followed) and (
|
|
|
|
bool(mentioned.intersection(interested_in))
|
2022-11-27 11:20:58 -08:00
|
|
|
)
|
2022-11-25 17:20:59 -08:00
|
|
|
if add:
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.add_post(
|
|
|
|
identity=instance.identity,
|
2022-11-25 17:20:59 -08:00
|
|
|
post=post,
|
|
|
|
)
|
2022-11-24 16:11:04 -08:00
|
|
|
# We might have been mentioned
|
2022-12-18 08:48:18 -08:00
|
|
|
if (
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.id in mentioned
|
|
|
|
and instance.identity_id != post.author_id
|
2022-12-18 08:48:18 -08:00
|
|
|
):
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.add_mentioned(
|
|
|
|
identity=instance.identity,
|
2022-11-24 16:11:04 -08:00
|
|
|
post=post,
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle sending remote posts create
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
post = instance.subject_post
|
2022-11-13 17:42:47 -08:00
|
|
|
# Sign it and send it
|
2022-12-17 11:29:48 -08:00
|
|
|
try:
|
2023-07-16 23:37:47 -07:00
|
|
|
post.author.signed_request(
|
2022-12-17 11:29:48 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2022-12-17 11:29:48 -08:00
|
|
|
body=canonicalise(post.to_create_ap()),
|
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle sending remote posts update
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post_edited, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
post = instance.subject_post
|
2022-11-27 10:09:46 -08:00
|
|
|
# Sign it and send it
|
2022-12-17 11:29:48 -08:00
|
|
|
try:
|
2023-07-16 23:37:47 -07:00
|
|
|
post.author.signed_request(
|
2022-12-17 11:29:48 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2022-12-17 11:29:48 -08:00
|
|
|
body=canonicalise(post.to_update_ap()),
|
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle deleting local posts
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post_deleted, True):
|
2023-07-07 14:14:06 -07:00
|
|
|
post = instance.subject_post
|
|
|
|
if instance.identity.local:
|
2022-11-27 10:09:46 -08:00
|
|
|
# Remove all timeline events mentioning it
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.objects.filter(
|
|
|
|
identity=instance.identity,
|
2022-11-27 10:09:46 -08:00
|
|
|
subject_post=post,
|
2023-07-07 14:14:06 -07:00
|
|
|
).delete()
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle sending remote post deletes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post_deleted, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
post = instance.subject_post
|
2022-11-24 16:11:04 -08:00
|
|
|
# Send it to the remote inbox
|
2022-12-17 11:29:48 -08:00
|
|
|
try:
|
2023-07-16 23:37:47 -07:00
|
|
|
post.author.signed_request(
|
2022-12-17 11:29:48 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2022-12-17 11:29:48 -08:00
|
|
|
body=canonicalise(post.to_delete_ap()),
|
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle local boosts/likes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.interaction, True):
|
2023-07-07 14:14:06 -07:00
|
|
|
interaction = instance.subject_post_interaction
|
2023-01-15 12:35:45 -08:00
|
|
|
# If the author of the interaction is blocked or their notifications
|
|
|
|
# are muted, skip out
|
|
|
|
if (
|
2023-07-07 14:14:06 -07:00
|
|
|
Block.objects.active()
|
2023-01-15 12:35:45 -08:00
|
|
|
.filter(
|
|
|
|
models.Q(mute=False) | models.Q(include_notifications=True),
|
2023-07-07 14:14:06 -07:00
|
|
|
source=instance.identity,
|
2023-01-15 12:35:45 -08:00
|
|
|
target=interaction.identity,
|
|
|
|
)
|
2023-07-07 14:14:06 -07:00
|
|
|
.exists()
|
2023-01-15 12:35:45 -08:00
|
|
|
):
|
|
|
|
return cls.skipped
|
|
|
|
# If blocked/muted the underlying post author, skip out
|
|
|
|
if (
|
2023-07-07 14:14:06 -07:00
|
|
|
Block.objects.active()
|
2023-01-15 12:35:45 -08:00
|
|
|
.filter(
|
2023-07-07 14:14:06 -07:00
|
|
|
source=instance.identity,
|
2023-01-15 12:35:45 -08:00
|
|
|
target_id=interaction.post.author_id,
|
|
|
|
)
|
2023-07-07 14:14:06 -07:00
|
|
|
.exists()
|
2023-01-15 12:35:45 -08:00
|
|
|
):
|
|
|
|
return cls.skipped
|
2022-11-15 17:30:30 -08:00
|
|
|
# Make a timeline event directly
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.add_post_interaction(
|
|
|
|
identity=instance.identity,
|
2022-11-15 17:30:30 -08:00
|
|
|
interaction=interaction,
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2023-05-13 09:01:27 -07:00
|
|
|
# Handle sending remote boosts/likes/votes/pins
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.interaction, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
interaction = instance.subject_post_interaction
|
2022-11-15 17:30:30 -08:00
|
|
|
# Send it to the remote inbox
|
2022-12-17 11:29:48 -08:00
|
|
|
try:
|
2023-05-13 09:01:27 -07:00
|
|
|
if interaction.type == interaction.Types.vote:
|
2023-08-07 08:15:13 -07:00
|
|
|
body = interaction.to_create_ap()
|
2023-05-13 09:01:27 -07:00
|
|
|
elif interaction.type == interaction.Types.pin:
|
|
|
|
body = interaction.to_add_ap()
|
|
|
|
else:
|
2023-08-07 08:15:13 -07:00
|
|
|
body = interaction.to_ap()
|
2023-07-16 23:37:47 -07:00
|
|
|
interaction.identity.signed_request(
|
2022-12-17 11:29:48 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2023-05-13 09:01:27 -07:00
|
|
|
body=canonicalise(body),
|
2022-12-17 11:29:48 -08:00
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle undoing local boosts/likes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.undo_interaction, True): # noqa:F841
|
2023-07-07 14:14:06 -07:00
|
|
|
interaction = instance.subject_post_interaction
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2022-11-15 17:30:30 -08:00
|
|
|
# Delete any local timeline events
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.delete_post_interaction(
|
|
|
|
identity=instance.identity,
|
2022-11-15 17:30:30 -08:00
|
|
|
interaction=interaction,
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2023-05-13 09:01:27 -07:00
|
|
|
# Handle sending remote undoing boosts/likes/pins
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.undo_interaction, False): # noqa:F841
|
2023-07-07 14:14:06 -07:00
|
|
|
interaction = instance.subject_post_interaction
|
2022-11-15 17:30:30 -08:00
|
|
|
# Send an undo to the remote inbox
|
2022-12-17 11:29:48 -08:00
|
|
|
try:
|
2023-05-13 09:01:27 -07:00
|
|
|
if interaction.type == interaction.Types.pin:
|
|
|
|
body = interaction.to_remove_ap()
|
|
|
|
else:
|
|
|
|
body = interaction.to_undo_ap()
|
2023-07-16 23:37:47 -07:00
|
|
|
interaction.identity.signed_request(
|
2022-12-17 11:29:48 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2023-05-13 09:01:27 -07:00
|
|
|
body=canonicalise(body),
|
2022-12-17 11:29:48 -08:00
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2022-12-21 09:13:39 -08:00
|
|
|
# Handle sending identity edited to remote
|
|
|
|
case (FanOut.Types.identity_edited, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
identity = instance.subject_identity
|
2022-12-21 09:13:39 -08:00
|
|
|
try:
|
2023-07-16 23:37:47 -07:00
|
|
|
identity.signed_request(
|
2022-12-21 09:13:39 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-28 10:13:06 -08:00
|
|
|
),
|
2023-07-07 14:14:06 -07:00
|
|
|
body=canonicalise(instance.subject_identity.to_update_ap()),
|
2022-12-21 09:13:39 -08:00
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Handle sending identity deleted to remote
|
|
|
|
case (FanOut.Types.identity_deleted, False):
|
2023-07-07 14:14:06 -07:00
|
|
|
identity = instance.subject_identity
|
2022-12-21 09:13:39 -08:00
|
|
|
try:
|
2023-07-16 23:37:47 -07:00
|
|
|
identity.signed_request(
|
2022-12-21 09:13:39 -08:00
|
|
|
method="post",
|
2023-01-08 12:46:40 -08:00
|
|
|
uri=(
|
2023-07-07 14:14:06 -07:00
|
|
|
instance.identity.shared_inbox_uri
|
|
|
|
or instance.identity.inbox_uri
|
2023-01-08 12:46:40 -08:00
|
|
|
),
|
2023-07-07 14:14:06 -07:00
|
|
|
body=canonicalise(instance.subject_identity.to_delete_ap()),
|
2022-12-21 09:13:39 -08:00
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
|
|
|
|
2023-08-08 07:54:59 -07:00
|
|
|
# Handle sending identity moved to remote
|
|
|
|
case (FanOut.Types.identity_moved, False):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-12-21 09:13:39 -08:00
|
|
|
# Sending identity edited/deleted to local is a no-op
|
|
|
|
case (FanOut.Types.identity_edited, True):
|
|
|
|
pass
|
|
|
|
case (FanOut.Types.identity_deleted, True):
|
|
|
|
pass
|
|
|
|
|
2023-01-15 13:48:17 -08:00
|
|
|
# Created identities make a timeline event
|
|
|
|
case (FanOut.Types.identity_created, True):
|
2023-07-07 14:14:06 -07:00
|
|
|
TimelineEvent.add_identity_created(
|
|
|
|
identity=instance.identity,
|
|
|
|
new_identity=instance.subject_identity,
|
2023-01-15 13:48:17 -08:00
|
|
|
)
|
|
|
|
|
2022-11-27 10:09:46 -08:00
|
|
|
case _:
|
|
|
|
raise ValueError(
|
2023-07-07 14:14:06 -07:00
|
|
|
f"Cannot fan out with type {instance.type} local={instance.identity.local}"
|
2022-11-27 10:09:46 -08:00
|
|
|
)
|
|
|
|
|
2022-11-17 17:52:00 -08:00
|
|
|
return cls.sent
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
class FanOut(StatorModel):
|
|
|
|
"""
|
|
|
|
An activity that needs to get to an inbox somewhere.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Types(models.TextChoices):
|
|
|
|
post = "post"
|
2022-11-24 16:11:04 -08:00
|
|
|
post_edited = "post_edited"
|
|
|
|
post_deleted = "post_deleted"
|
2022-11-13 17:42:47 -08:00
|
|
|
interaction = "interaction"
|
2022-11-15 17:30:30 -08:00
|
|
|
undo_interaction = "undo_interaction"
|
2022-12-21 09:13:39 -08:00
|
|
|
identity_edited = "identity_edited"
|
|
|
|
identity_deleted = "identity_deleted"
|
2023-01-15 13:48:17 -08:00
|
|
|
identity_created = "identity_created"
|
2023-08-08 07:54:59 -07:00
|
|
|
identity_moved = "identity_moved"
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
state = StateField(FanOutStates)
|
|
|
|
|
|
|
|
# The user this event is targeted at
|
2023-01-08 12:46:40 -08:00
|
|
|
# We always need this, but if there is a shared inbox URL on the user
|
|
|
|
# we'll deliver to that and won't have fanouts for anyone else with the
|
|
|
|
# same one.
|
2022-11-11 22:04:43 -08:00
|
|
|
identity = models.ForeignKey(
|
|
|
|
"users.Identity",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="fan_outs",
|
|
|
|
)
|
|
|
|
|
|
|
|
# What type of activity it is
|
|
|
|
type = models.CharField(max_length=100, choices=Types.choices)
|
|
|
|
|
|
|
|
# Links to the appropriate objects
|
|
|
|
subject_post = models.ForeignKey(
|
|
|
|
"activities.Post",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="fan_outs",
|
|
|
|
)
|
2022-11-13 17:42:47 -08:00
|
|
|
subject_post_interaction = models.ForeignKey(
|
|
|
|
"activities.PostInteraction",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="fan_outs",
|
|
|
|
)
|
2022-12-21 09:13:39 -08:00
|
|
|
subject_identity = models.ForeignKey(
|
|
|
|
"users.Identity",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="subject_fan_outs",
|
|
|
|
)
|
2022-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|