summaryrefslogtreecommitdiffstats
path: root/activities/models
diff options
context:
space:
mode:
authorAndrew Godwin2022-11-27 12:09:08 -0700
committerAndrew Godwin2022-11-27 12:09:08 -0700
commit8e9e3ecf6935db84bbc731252f592795675de685 (patch)
treeeb3f690cdc7504b3f8fc2da716c1bbfc4bacc36c /activities/models
parent6c7ddedd342553b53dd98c8de9cbe9e8e2e8cd7c (diff)
downloadtakahe-8e9e3ecf6935db84bbc731252f592795675de685.tar.gz
takahe-8e9e3ecf6935db84bbc731252f592795675de685.tar.bz2
takahe-8e9e3ecf6935db84bbc731252f592795675de685.zip
Some cleanup around editing
Diffstat (limited to 'activities/models')
-rw-r--r--activities/models/fan_out.py20
-rw-r--r--activities/models/post.py61
2 files changed, 53 insertions, 28 deletions
diff --git a/activities/models/fan_out.py b/activities/models/fan_out.py
index a86e30a..14f52a4 100644
--- a/activities/models/fan_out.py
+++ b/activities/models/fan_out.py
@@ -17,14 +17,12 @@ class FanOutStates(StateGraph):
"""
Sends the fan-out to the right inbox.
"""
- LOCAL_IDENTITY = True
- REMOTE_IDENTITY = False
fan_out = await instance.afetch_full()
match (fan_out.type, fan_out.identity.local):
# Handle creating/updating local posts
- case (FanOut.Types.post | FanOut.Types.post_edited, LOCAL_IDENTITY):
+ case ((FanOut.Types.post | FanOut.Types.post_edited), True):
post = await fan_out.subject_post.afetch_full()
# Make a timeline event directly
# If it's a reply, we only add it if we follow at least one
@@ -50,7 +48,7 @@ class FanOutStates(StateGraph):
)
# Handle sending remote posts create
- case (FanOut.Types.post, REMOTE_IDENTITY):
+ case (FanOut.Types.post, False):
post = await fan_out.subject_post.afetch_full()
# Sign it and send it
await post.author.signed_request(
@@ -60,7 +58,7 @@ class FanOutStates(StateGraph):
)
# Handle sending remote posts update
- case (FanOut.Types.post_edited, REMOTE_IDENTITY):
+ case (FanOut.Types.post_edited, False):
post = await fan_out.subject_post.afetch_full()
# Sign it and send it
await post.author.signed_request(
@@ -70,7 +68,7 @@ class FanOutStates(StateGraph):
)
# Handle deleting local posts
- case (FanOut.Types.post_deleted, LOCAL_IDENTITY):
+ case (FanOut.Types.post_deleted, True):
post = await fan_out.subject_post.afetch_full()
if fan_out.identity.local:
# Remove all timeline events mentioning it
@@ -80,7 +78,7 @@ class FanOutStates(StateGraph):
).adelete()
# Handle sending remote post deletes
- case (FanOut.Types.post_deleted, REMOTE_IDENTITY):
+ case (FanOut.Types.post_deleted, False):
post = await fan_out.subject_post.afetch_full()
# Send it to the remote inbox
await post.author.signed_request(
@@ -90,7 +88,7 @@ class FanOutStates(StateGraph):
)
# Handle local boosts/likes
- case (FanOut.Types.interaction, LOCAL_IDENTITY):
+ case (FanOut.Types.interaction, True):
interaction = await fan_out.subject_post_interaction.afetch_full()
# Make a timeline event directly
await sync_to_async(TimelineEvent.add_post_interaction)(
@@ -99,7 +97,7 @@ class FanOutStates(StateGraph):
)
# Handle sending remote boosts/likes
- case (FanOut.Types.interaction, REMOTE_IDENTITY):
+ case (FanOut.Types.interaction, False):
interaction = await fan_out.subject_post_interaction.afetch_full()
# Send it to the remote inbox
await interaction.identity.signed_request(
@@ -109,7 +107,7 @@ class FanOutStates(StateGraph):
)
# Handle undoing local boosts/likes
- case (FanOut.Types.undo_interaction, LOCAL_IDENTITY): # noqa:F841
+ case (FanOut.Types.undo_interaction, True): # noqa:F841
interaction = await fan_out.subject_post_interaction.afetch_full()
# Delete any local timeline events
@@ -119,7 +117,7 @@ class FanOutStates(StateGraph):
)
# Handle sending remote undoing boosts/likes
- case (FanOut.Types.undo_interaction, REMOTE_IDENTITY): # noqa:F841
+ case (FanOut.Types.undo_interaction, False): # noqa:F841
interaction = await fan_out.subject_post_interaction.afetch_full()
# Send an undo to the remote inbox
await interaction.identity.signed_request(
diff --git a/activities/models/post.py b/activities/models/post.py
index 23194b3..f504fcb 100644
--- a/activities/models/post.py
+++ b/activities/models/post.py
@@ -1,5 +1,5 @@
import re
-from typing import Dict, Iterable, Optional
+from typing import Dict, Iterable, Optional, Set
import httpx
import urlman
@@ -244,6 +244,12 @@ class Post(StatorModel):
"""
return self.linkify_mentions(sanitize_post(self.content))
+ def safe_content_plain(self):
+ """
+ Returns the content formatted as plain text
+ """
+ return self.linkify_mentions(sanitize_post(self.content))
+
### Async helpers ###
async def afetch_full(self):
@@ -256,7 +262,7 @@ class Post(StatorModel):
.aget(pk=self.pk)
)
- ### Local creation ###
+ ### Local creation/editing ###
@classmethod
def create_local(
@@ -269,21 +275,7 @@ class Post(StatorModel):
) -> "Post":
with transaction.atomic():
# Find mentions in this post
- mention_hits = cls.mention_regex.findall(content)
- mentions = set()
- for precursor, handle in mention_hits:
- if "@" in handle:
- username, domain = handle.split("@", 1)
- else:
- username = handle
- domain = author.domain_id
- identity = Identity.by_username_and_domain(
- username=username,
- domain=domain,
- fetch=True,
- )
- if identity is not None:
- mentions.add(identity)
+ mentions = cls.mentions_from_content(content, author)
if reply_to:
mentions.add(reply_to.author)
# Maintain local-only for replies
@@ -307,6 +299,41 @@ class Post(StatorModel):
post.save()
return post
+ def edit_local(
+ self,
+ content: str,
+ summary: Optional[str] = None,
+ visibility: int = Visibilities.public,
+ ):
+ with transaction.atomic():
+ # Strip all HTML and apply linebreaks filter
+ self.content = linebreaks_filter(strip_html(content))
+ self.summary = summary or None
+ self.sensitive = bool(summary)
+ self.visibility = visibility
+ self.edited = timezone.now()
+ self.mentions.set(self.mentions_from_content(content, self.author))
+ self.save()
+
+ @classmethod
+ def mentions_from_content(cls, content, author) -> Set[Identity]:
+ mention_hits = cls.mention_regex.findall(content)
+ mentions = set()
+ for precursor, handle in mention_hits:
+ if "@" in handle:
+ username, domain = handle.split("@", 1)
+ else:
+ username = handle
+ domain = author.domain_id
+ identity = Identity.by_username_and_domain(
+ username=username,
+ domain=domain,
+ fetch=True,
+ )
+ if identity is not None:
+ mentions.add(identity)
+ return mentions
+
### ActivityPub (outbound) ###
def to_ap(self) -> Dict: