Skip to content

Commit 782f617

Browse files
committed
max replies allowed is 5
1 parent 4d2fe6b commit 782f617

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os, sys
2+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
3+
4+
from social_posters.mastodon import PosterMastodon
5+
from mastodon_manual_test import post_exceed_500_chars_limit_with_adoption_link
6+
7+
poster = PosterMastodon.__new__(PosterMastodon)
8+
9+
pet = post_exceed_500_chars_limit_with_adoption_link()
10+
post = poster.format_post(pet)
11+
12+
main_caption, replies = poster._format_caption_thread(post)
13+
14+
print("\n" + "=" * 60)
15+
print("MAIN POST")
16+
print("=" * 60)
17+
print(main_caption)
18+
print(f"\nLength: {len(main_caption)}")
19+
20+
for i, reply in enumerate(replies, start=1):
21+
print("\n" + "=" * 60)
22+
print(f"REPLY {i}")
23+
print("=" * 60)
24+
print(reply)
25+
print(f"\nLength: {len(reply)}")

social_posters/mastodon.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
THREAD_SUFFIX = "\n\nMore details below ⬇️"
1212
MASTODON_CHARACTER_LIMIT = 500
1313
TRUNCATION_SUFFIX = "..."
14-
14+
MAX_REPLIES = 5
1515

1616
class PosterMastodon(SocialPoster):
1717
def __init__(self):
@@ -126,12 +126,21 @@ def _split_reply_chunks(self, text: str) -> list[str]:
126126
chunks = []
127127
remaining = text.strip()
128128

129-
while remaining:
129+
while remaining and len(chunks) < MAX_REPLIES:
130130
chunk, remaining = self._safe_truncate(
131131
remaining,
132132
MASTODON_CHARACTER_LIMIT
133133
)
134134
chunks.append(chunk)
135+
136+
if remaining and chunks:
137+
last_chunk = chunks[-1]
138+
139+
cutoff = MASTODON_CHARACTER_LIMIT - len(TRUNCATION_SUFFIX)
140+
141+
last_chunk, _ = self._safe_truncate(last_chunk, cutoff)
142+
143+
chunks[-1] = f"{last_chunk}{TRUNCATION_SUFFIX}"
135144

136145
return chunks
137146

tests/test_mastodon.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abstractions import Post
2-
from social_posters.mastodon import PosterMastodon, MASTODON_CHARACTER_LIMIT
2+
from social_posters.mastodon import PosterMastodon, MASTODON_CHARACTER_LIMIT, MAX_REPLIES
33

44

55
class TestMastodonCaption:
@@ -16,6 +16,43 @@ def reconstruct_text(self, main_caption: str, replies: list[str]) -> str:
1616
)
1717

1818
return " ".join([main_without_suffix] + replies).strip()
19+
20+
def test_reply_count_is_capped(self):
21+
post = Post(text="hello " * 5000)
22+
23+
_, replies = self.poster._format_caption_thread(post)
24+
25+
assert len(replies) <= MAX_REPLIES
26+
27+
def test_last_reply_has_truncation_suffix_when_capped(self):
28+
post = Post(text="hello " * 5000)
29+
30+
_, replies = self.poster._format_caption_thread(post)
31+
32+
assert len(replies) == MAX_REPLIES
33+
assert replies[-1].endswith("...")
34+
assert len(replies[-1]) <= MASTODON_CHARACTER_LIMIT
35+
36+
def test_last_reply_has_no_truncation_suffix_when_not_capped(self):
37+
post = Post(text="hello " * 300)
38+
39+
_, replies = self.poster._format_caption_thread(post)
40+
41+
assert replies
42+
assert len(replies) < MAX_REPLIES
43+
assert not replies[-1].endswith("...")
44+
45+
def test_capped_thread_does_not_preserve_all_original_text(self):
46+
original_text = "hello " * 5000
47+
post = Post(text=original_text)
48+
49+
main_caption, replies = self.poster._format_caption_thread(post)
50+
51+
reconstructed = self.reconstruct_text(main_caption, replies)
52+
53+
assert len(replies) == MAX_REPLIES
54+
assert reconstructed != original_text
55+
assert replies[-1].endswith("...")
1956

2057
def test_thread_preserves_original_text_content(self):
2158
original_text = " ".join(f"word{i}" for i in range(300))

0 commit comments

Comments
 (0)