summaryrefslogtreecommitdiffstats
path: root/activities/models
diff options
context:
space:
mode:
Diffstat (limited to 'activities/models')
-rw-r--r--activities/models/__init__.py1
-rw-r--r--activities/models/fan_out.py53
-rw-r--r--activities/models/post.py33
-rw-r--r--activities/models/post_interaction.py191
-rw-r--r--activities/models/timeline_event.py55
5 files changed, 297 insertions, 36 deletions
diff --git a/activities/models/__init__.py b/activities/models/__init__.py
index b0ed474..a0680ad 100644
--- a/activities/models/__init__.py
+++ b/activities/models/__init__.py
@@ -1,3 +1,4 @@
from .fan_out import FanOut # noqa
from .post import Post # noqa
+from .post_interaction import PostInteraction # noqa
from .timeline_event import TimelineEvent # noqa
diff --git a/activities/models/fan_out.py b/activities/models/fan_out.py
index 958fbe2..dbe86c0 100644
--- a/activities/models/fan_out.py
+++ b/activities/models/fan_out.py
@@ -19,23 +19,27 @@ class FanOutStates(StateGraph):
Sends the fan-out to the right inbox.
"""
fan_out = await instance.afetch_full()
- if fan_out.identity.local:
- # Make a timeline event directly
- await sync_to_async(TimelineEvent.add_post)(
- identity=fan_out.identity,
- post=fan_out.subject_post,
- )
+ # Handle Posts
+ if fan_out.type == FanOut.Types.post:
+ if fan_out.identity.local:
+ # Make a timeline event directly
+ await sync_to_async(TimelineEvent.add_post)(
+ identity=fan_out.identity,
+ post=fan_out.subject_post,
+ )
+ else:
+ # Send it to the remote inbox
+ post = await fan_out.subject_post.afetch_full()
+ # Sign it and send it
+ await HttpSignature.signed_request(
+ uri=fan_out.identity.inbox_uri,
+ body=canonicalise(post.to_create_ap()),
+ private_key=post.author.private_key,
+ key_id=post.author.public_key_id,
+ )
+ return cls.sent
else:
- # Send it to the remote inbox
- post = await fan_out.subject_post.afetch_full()
- # Sign it and send it
- await HttpSignature.signed_request(
- uri=fan_out.identity.inbox_uri,
- body=canonicalise(post.to_create_ap()),
- private_key=post.author.private_key,
- key_id=post.author.public_key_id,
- )
- return cls.sent
+ raise ValueError(f"Cannot fan out with type {fan_out.type}")
class FanOut(StatorModel):
@@ -45,7 +49,7 @@ class FanOut(StatorModel):
class Types(models.TextChoices):
post = "post"
- boost = "boost"
+ interaction = "interaction"
state = StateField(FanOutStates)
@@ -67,6 +71,13 @@ class FanOut(StatorModel):
null=True,
related_name="fan_outs",
)
+ subject_post_interaction = models.ForeignKey(
+ "activities.PostInteraction",
+ on_delete=models.CASCADE,
+ blank=True,
+ null=True,
+ related_name="fan_outs",
+ )
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
@@ -77,6 +88,8 @@ class FanOut(StatorModel):
"""
Returns a version of the object with all relations pre-loaded
"""
- return await FanOut.objects.select_related("identity", "subject_post").aget(
- pk=self.pk
- )
+ return await FanOut.objects.select_related(
+ "identity",
+ "subject_post",
+ "subject_post_interaction",
+ ).aget(pk=self.pk)
diff --git a/activities/models/post.py b/activities/models/post.py
index 75a4388..d847307 100644
--- a/activities/models/post.py
+++ b/activities/models/post.py
@@ -1,5 +1,6 @@
from typing import Dict, Optional
+import httpx
import urlman
from django.db import models
from django.utils import timezone
@@ -7,7 +8,7 @@ from django.utils import timezone
from activities.models.fan_out import FanOut
from activities.models.timeline_event import TimelineEvent
from core.html import sanitize_post
-from core.ld import format_ld_date, parse_ld_date
+from core.ld import canonicalise, format_ld_date, parse_ld_date
from stator.models import State, StateField, StateGraph, StatorModel
from users.models.follow import Follow
from users.models.identity import Identity
@@ -91,7 +92,7 @@ class Post(StatorModel):
)
# When the post was originally created (as opposed to when we received it)
- authored = models.DateTimeField(default=timezone.now)
+ published = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
@@ -173,7 +174,7 @@ class Post(StatorModel):
value = {
"type": "Note",
"id": self.object_uri,
- "published": format_ld_date(self.created),
+ "published": format_ld_date(self.published),
"attributedTo": self.author.actor_uri,
"content": self.safe_content,
"to": "as:Public",
@@ -227,7 +228,7 @@ class Post(StatorModel):
post.summary = data.get("summary", None)
post.sensitive = data.get("as:sensitive", False)
post.url = data.get("url", None)
- post.authored = parse_ld_date(data.get("published", None))
+ post.published = parse_ld_date(data.get("published", None))
# TODO: to
# TODO: mentions
# TODO: visibility
@@ -235,6 +236,30 @@ class Post(StatorModel):
return post
@classmethod
+ def by_object_uri(cls, object_uri, fetch=False):
+ """
+ Gets the post by URI - either looking up locally, or fetching
+ from the other end if it's not here.
+ """
+ try:
+ return cls.objects.get(object_uri=object_uri)
+ except cls.DoesNotExist:
+ if fetch:
+ # Go grab the data from the URI
+ response = httpx.get(
+ object_uri,
+ headers={"Accept": "application/json"},
+ follow_redirects=True,
+ )
+ if 200 <= response.status_code < 300:
+ return cls.by_ap(
+ canonicalise(response.json(), include_security=True),
+ create=True,
+ update=True,
+ )
+ raise ValueError(f"Cannot find Post with URI {object_uri}")
+
+ @classmethod
def handle_create_ap(cls, data):
"""
Handles an incoming create request
diff --git a/activities/models/post_interaction.py b/activities/models/post_interaction.py
new file mode 100644
index 0000000..151ab45
--- /dev/null
+++ b/activities/models/post_interaction.py
@@ -0,0 +1,191 @@
+from typing import Dict
+
+from django.db import models
+from django.utils import timezone
+
+from activities.models.fan_out import FanOut
+from activities.models.post import Post
+from activities.models.timeline_event import TimelineEvent
+from core.ld import format_ld_date, parse_ld_date
+from stator.models import State, StateField, StateGraph, StatorModel
+from users.models.follow import Follow
+from users.models.identity import Identity
+
+
+class PostInteractionStates(StateGraph):
+ new = State(try_interval=300)
+ fanned_out = State()
+
+ new.transitions_to(fanned_out)
+
+ @classmethod
+ async def handle_new(cls, instance: "PostInteraction"):
+ """
+ Creates all needed fan-out objects for a new PostInteraction.
+ """
+ interaction = await instance.afetch_full()
+ # Boost: send a copy to all people who follow this user
+ if interaction.type == interaction.Types.boost:
+ async for follow in interaction.identity.inbound_follows.select_related(
+ "source", "target"
+ ):
+ if follow.source.local or follow.target.local:
+ await FanOut.objects.acreate(
+ identity_id=follow.source_id,
+ type=FanOut.Types.interaction,
+ subject_post=interaction,
+ )
+ # Like: send a copy to the original post author only
+ elif interaction.type == interaction.Types.like:
+ await FanOut.objects.acreate(
+ identity_id=interaction.post.author_id,
+ type=FanOut.Types.interaction,
+ subject_post=interaction,
+ )
+ else:
+ raise ValueError("Cannot fan out unknown type")
+ # And one for themselves if they're local
+ if interaction.identity.local:
+ await FanOut.objects.acreate(
+ identity_id=interaction.identity_id,
+ type=FanOut.Types.interaction,
+ subject_post=interaction,
+ )
+
+
+class PostInteraction(StatorModel):
+ """
+ Handles both boosts and likes
+ """
+
+ class Types(models.TextChoices):
+ like = "like"
+ boost = "boost"
+
+ # The state the boost is in
+ state = StateField(PostInteractionStates)
+
+ # The canonical object ID
+ object_uri = models.CharField(max_length=500, blank=True, null=True, unique=True)
+
+ # What type of interaction it is
+ type = models.CharField(max_length=100, choices=Types.choices)
+
+ # The user who boosted/liked/etc.
+ identity = models.ForeignKey(
+ "users.Identity",
+ on_delete=models.CASCADE,
+ related_name="interactions",
+ )
+
+ # The post that was boosted/liked/etc
+ post = models.ForeignKey(
+ "activities.Post",
+ on_delete=models.CASCADE,
+ related_name="interactions",
+ )
+
+ # When the activity was originally created (as opposed to when we received it)
+ # Mastodon only seems to send this for boosts, not likes
+ published = models.DateTimeField(default=timezone.now)
+
+ created = models.DateTimeField(auto_now_add=True)
+ updated = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ index_together = [["type", "identity", "post"]]
+
+ ### Async helpers ###
+
+ async def afetch_full(self):
+ """
+ Returns a version of the object with all relations pre-loaded
+ """
+ return await PostInteraction.objects.select_related("identity", "post").aget(
+ pk=self.pk
+ )
+
+ ### ActivityPub (outbound) ###
+
+ def to_ap(self) -> Dict:
+ """
+ Returns the AP JSON for this object
+ """
+ if self.type == self.Types.boost:
+ value = {
+ "type": "Announce",
+ "id": self.object_uri,
+ "published": format_ld_date(self.published),
+ "actor": self.identity.actor_uri,
+ "object": self.post.object_uri,
+ "to": "as:Public",
+ }
+ elif self.type == self.Types.like:
+ value = {
+ "type": "Like",
+ "id": self.object_uri,
+ "published": format_ld_date(self.published),
+ "actor": self.identity.actor_uri,
+ "object": self.post.object_uri,
+ }
+ else:
+ raise ValueError("Cannot turn into AP")
+ return value
+
+ ### ActivityPub (inbound) ###
+
+ @classmethod
+ def by_ap(cls, data, create=False) -> "PostInteraction":
+ """
+ Retrieves a PostInteraction instance by its ActivityPub JSON object.
+
+ Optionally creates one if it's not present.
+ Raises KeyError if it's not found and create is False.
+ """
+ # Do we have one with the right ID?
+ try:
+ boost = cls.objects.get(object_uri=data["id"])
+ except cls.DoesNotExist:
+ if create:
+ # Resolve the author
+ identity = Identity.by_actor_uri(data["actor"], create=True)
+ # Resolve the post
+ post = Post.by_object_uri(data["object"], fetch=True)
+ # Get the right type
+ if data["type"].lower() == "like":
+ type = cls.Types.like
+ elif data["type"].lower() == "announce":
+ type = cls.Types.boost
+ else:
+ raise ValueError(f"Cannot handle AP type {data['type']}")
+ # Make the actual interaction
+ boost = cls.objects.create(
+ object_uri=data["id"],
+ identity=identity,
+ post=post,
+ published=parse_ld_date(data.get("published", None))
+ or timezone.now(),
+ type=type,
+ )
+ else:
+ raise KeyError(f"No post with ID {data['id']}", data)
+ return boost
+
+ @classmethod
+ def handle_ap(cls, data):
+ """
+ Handles an incoming announce/like
+ """
+ # Create it
+ interaction = cls.by_ap(data, create=True)
+ # Boosts (announces) go to everyone who follows locally
+ if interaction.type == cls.Types.boost:
+ for follow in Follow.objects.filter(
+ target=interaction.identity, source__local=True
+ ):
+ TimelineEvent.add_post_interaction(follow.source, interaction)
+ # Likes go to just the author of the post
+ elif interaction.type == cls.Types.like:
+ TimelineEvent.add_post_interaction(interaction.post.author, interaction)
+ # Force it into fanned_out as it's not ours
+ interaction.transition_perform(PostInteractionStates.fanned_out)
diff --git a/activities/models/timeline_event.py b/activities/models/timeline_event.py
index 43fc458..6dba32c 100644
--- a/activities/models/timeline_event.py
+++ b/activities/models/timeline_event.py
@@ -9,10 +9,11 @@ class TimelineEvent(models.Model):
class Types(models.TextChoices):
post = "post"
- mention = "mention"
- like = "like"
- follow = "follow"
- boost = "boost"
+ boost = "boost" # A boost from someone (post substitude)
+ mentioned = "mentioned"
+ liked = "liked" # Someone liking one of our posts
+ followed = "followed"
+ boosted = "boosted" # Someone boosting one of our posts
# The user this event is for
identity = models.ForeignKey(
@@ -30,7 +31,14 @@ class TimelineEvent(models.Model):
on_delete=models.CASCADE,
blank=True,
null=True,
- related_name="timeline_events_about_us",
+ related_name="timeline_events",
+ )
+ subject_post_interaction = models.ForeignKey(
+ "activities.PostInteraction",
+ on_delete=models.CASCADE,
+ blank=True,
+ null=True,
+ related_name="timeline_events",
)
subject_identity = models.ForeignKey(
"users.Identity",
@@ -74,12 +82,35 @@ class TimelineEvent(models.Model):
)[0]
@classmethod
- def add_like(cls, identity, post):
+ def add_post_interaction(cls, identity, interaction):
"""
- Adds a like to the timeline if it's not there already
+ Adds a boost/like to the timeline if it's not there already.
+
+ For boosts, may make two objects - one "boost" and one "boosted".
+ It'll return the "boost" in that case.
"""
- return cls.objects.get_or_create(
- identity=identity,
- type=cls.Types.like,
- subject_post=post,
- )[0]
+ if interaction.type == interaction.Types.like:
+ return cls.objects.get_or_create(
+ identity=identity,
+ type=cls.Types.liked,
+ subject_post_id=interaction.post_id,
+ subject_identity_id=interaction.identity_id,
+ subject_post_interaction=interaction,
+ )[0]
+ elif interaction.type == interaction.Types.boost:
+ # If the boost is on one of our posts, then that's a boosted too
+ if interaction.post.author_id == identity.id:
+ return cls.objects.get_or_create(
+ identity=identity,
+ type=cls.Types.boosted,
+ subject_post_id=interaction.post_id,
+ subject_identity_id=interaction.identity_id,
+ subject_post_interaction=interaction,
+ )[0]
+ return cls.objects.get_or_create(
+ identity=identity,
+ type=cls.Types.boost,
+ subject_post_id=interaction.post_id,
+ subject_identity_id=interaction.identity_id,
+ subject_post_interaction=interaction,
+ )[0]