2022-11-11 22:04:43 -08:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class FanOutStates(StateGraph):
|
|
|
|
new = State(try_interval=300)
|
|
|
|
sent = State()
|
|
|
|
|
|
|
|
new.transitions_to(sent)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def handle_new(cls, instance: "FanOut"):
|
|
|
|
"""
|
|
|
|
Sends the fan-out to the right inbox.
|
|
|
|
"""
|
2022-11-27 10:09:46 -08:00
|
|
|
|
2022-11-11 22:04:43 -08:00
|
|
|
fan_out = await instance.afetch_full()
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
match (fan_out.type, fan_out.identity.local):
|
|
|
|
# Handle creating/updating local posts
|
2022-11-27 11:09:08 -08:00
|
|
|
case ((FanOut.Types.post | FanOut.Types.post_edited), True):
|
2022-11-27 10:09:46 -08:00
|
|
|
post = await fan_out.subject_post.afetch_full()
|
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-11-27 11:20:58 -08:00
|
|
|
# of the people mentioned AND the author
|
2022-11-25 17:20:59 -08:00
|
|
|
add = True
|
|
|
|
mentioned = {identity.id for identity in post.mentions.all()}
|
2022-11-27 11:20:58 -08:00
|
|
|
followed = await sync_to_async(set)(
|
|
|
|
fan_out.identity.outbound_follows.values_list("id", flat=True)
|
|
|
|
)
|
2022-11-25 17:20:59 -08:00
|
|
|
if post.in_reply_to:
|
2022-11-27 11:20:58 -08:00
|
|
|
add = (post.author_id in followed) and bool(
|
|
|
|
mentioned.intersection(followed)
|
|
|
|
)
|
2022-11-25 17:20:59 -08:00
|
|
|
if add:
|
|
|
|
await sync_to_async(TimelineEvent.add_post)(
|
|
|
|
identity=fan_out.identity,
|
|
|
|
post=post,
|
|
|
|
)
|
2022-11-24 16:11:04 -08:00
|
|
|
# We might have been mentioned
|
2022-11-25 17:20:59 -08:00
|
|
|
if fan_out.identity.id in mentioned:
|
2022-11-25 17:35:43 -08:00
|
|
|
await sync_to_async(TimelineEvent.add_mentioned)(
|
2022-11-24 16:11:04 -08:00
|
|
|
identity=fan_out.identity,
|
|
|
|
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):
|
2022-11-27 10:09:46 -08:00
|
|
|
post = await fan_out.subject_post.afetch_full()
|
2022-11-13 17:42:47 -08:00
|
|
|
# Sign it and send it
|
2022-11-20 17:32:55 -08:00
|
|
|
await post.author.signed_request(
|
|
|
|
method="post",
|
2022-11-13 17:42:47 -08:00
|
|
|
uri=fan_out.identity.inbox_uri,
|
|
|
|
body=canonicalise(post.to_create_ap()),
|
|
|
|
)
|
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):
|
2022-11-27 10:09:46 -08:00
|
|
|
post = await fan_out.subject_post.afetch_full()
|
|
|
|
# Sign it and send it
|
|
|
|
await post.author.signed_request(
|
|
|
|
method="post",
|
|
|
|
uri=fan_out.identity.inbox_uri,
|
|
|
|
body=canonicalise(post.to_update_ap()),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Handle deleting local posts
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post_deleted, True):
|
2022-11-27 10:09:46 -08:00
|
|
|
post = await fan_out.subject_post.afetch_full()
|
|
|
|
if fan_out.identity.local:
|
|
|
|
# Remove all timeline events mentioning it
|
|
|
|
await TimelineEvent.objects.filter(
|
|
|
|
identity=fan_out.identity,
|
|
|
|
subject_post=post,
|
|
|
|
).adelete()
|
|
|
|
|
|
|
|
# Handle sending remote post deletes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.post_deleted, False):
|
2022-11-27 10:09:46 -08:00
|
|
|
post = await fan_out.subject_post.afetch_full()
|
2022-11-24 16:11:04 -08:00
|
|
|
# Send it to the remote inbox
|
|
|
|
await post.author.signed_request(
|
|
|
|
method="post",
|
|
|
|
uri=fan_out.identity.inbox_uri,
|
|
|
|
body=canonicalise(post.to_delete_ap()),
|
|
|
|
)
|
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):
|
2022-11-27 10:09:46 -08:00
|
|
|
interaction = await fan_out.subject_post_interaction.afetch_full()
|
2022-11-15 17:30:30 -08:00
|
|
|
# Make a timeline event directly
|
|
|
|
await sync_to_async(TimelineEvent.add_post_interaction)(
|
|
|
|
identity=fan_out.identity,
|
|
|
|
interaction=interaction,
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle sending remote boosts/likes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.interaction, False):
|
2022-11-27 10:09:46 -08:00
|
|
|
interaction = await fan_out.subject_post_interaction.afetch_full()
|
2022-11-15 17:30:30 -08:00
|
|
|
# Send it to the remote inbox
|
2022-11-20 17:32:55 -08:00
|
|
|
await interaction.identity.signed_request(
|
|
|
|
method="post",
|
2022-11-15 17:30:30 -08:00
|
|
|
uri=fan_out.identity.inbox_uri,
|
|
|
|
body=canonicalise(interaction.to_ap()),
|
|
|
|
)
|
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
|
2022-11-27 10:09:46 -08:00
|
|
|
interaction = await fan_out.subject_post_interaction.afetch_full()
|
|
|
|
|
2022-11-15 17:30:30 -08:00
|
|
|
# Delete any local timeline events
|
|
|
|
await sync_to_async(TimelineEvent.delete_post_interaction)(
|
|
|
|
identity=fan_out.identity,
|
|
|
|
interaction=interaction,
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
# Handle sending remote undoing boosts/likes
|
2022-11-27 11:09:08 -08:00
|
|
|
case (FanOut.Types.undo_interaction, False): # noqa:F841
|
2022-11-27 10:09:46 -08:00
|
|
|
interaction = await fan_out.subject_post_interaction.afetch_full()
|
2022-11-15 17:30:30 -08:00
|
|
|
# Send an undo to the remote inbox
|
2022-11-20 17:32:55 -08:00
|
|
|
await interaction.identity.signed_request(
|
|
|
|
method="post",
|
2022-11-15 17:30:30 -08:00
|
|
|
uri=fan_out.identity.inbox_uri,
|
|
|
|
body=canonicalise(interaction.to_undo_ap()),
|
|
|
|
)
|
2022-11-27 10:09:46 -08:00
|
|
|
|
|
|
|
case _:
|
|
|
|
raise ValueError(
|
|
|
|
f"Cannot fan out with type {fan_out.type} local={fan_out.identity.local}"
|
|
|
|
)
|
|
|
|
|
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-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
state = StateField(FanOutStates)
|
|
|
|
|
|
|
|
# The user this event is targeted at
|
|
|
|
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-11-11 22:04:43 -08:00
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
### Async helpers ###
|
|
|
|
|
|
|
|
async def afetch_full(self):
|
|
|
|
"""
|
|
|
|
Returns a version of the object with all relations pre-loaded
|
|
|
|
"""
|
2022-12-14 23:50:54 -08:00
|
|
|
return (
|
|
|
|
await FanOut.objects.select_related(
|
|
|
|
"identity",
|
|
|
|
"subject_post",
|
|
|
|
"subject_post_interaction",
|
|
|
|
)
|
|
|
|
.prefetch_related(
|
|
|
|
"subject_post__emojis",
|
|
|
|
)
|
|
|
|
.aget(pk=self.pk)
|
|
|
|
)
|