Skip to content

Commit a255df4

Browse files
author
Jonny Johannes
committed
link facet. and required adoption_url and img
1 parent acb509d commit a255df4

3 files changed

Lines changed: 39 additions & 26 deletions

File tree

abstractions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class AdoptablePet:
1616
species: str # "dog" or "cat"
1717
breed: str
1818
location: str
19+
adoption_url: str
20+
image_url: str
1921
description: str = ""
20-
adoption_url: str | None = None
21-
image_url: str | None = None
2222
age_string: str | None = None
2323
sex: str | None = None
2424
size_group: str | None = None
@@ -50,8 +50,8 @@ class Post:
5050
"""Represents a social media post about an adoptable pet."""
5151

5252
text: str
53-
image_url: str | None = None
54-
link: str | None = None
53+
image_url: str
54+
link: str
5555
alt_text: str | None = None # For image accessibility
5656
tags: list[str] = field(default_factory=list)
5757

social_posters/bluesky.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
from typing import Optional
32
import os
43

54
import requests
@@ -147,36 +146,34 @@ def format_post(self, pet):
147146
alt_text=f"Photo of {name}, a {pet.breed} available for adoption",
148147
tags=tags,
149148
)
149+
150150
def _build_text_and_facets(self, post: Post) -> tuple[str, list]:
151151
body = post.text
152152
facets: list = []
153153
separator = "\n\n"
154154
limit = 300
155155

156-
tag_strings = [f"#{tag}" for tag in (post.tags or []) if tag]
157-
if tag_strings:
158-
tags_section = " ".join(tag_strings)
159-
# Truncate body so the full text (body + separator + tags) fits in limit chars.
160-
max_body = limit - len(separator) - len(tags_section)
161-
full_text = f"{body[:max_body]}{separator}{tags_section}"
162-
else:
163-
full_text = body[:limit]
156+
tag_strings = [f"#{tag}" for tag in (post.tags) if tag]
157+
tags_section = " ".join(tag_strings)
158+
link_section = post.link
159+
# Truncate body so the full text (body + link + tags + separators) fits in limit chars.
160+
max_body = limit - len(separator) - len(link_section) - len(separator) - len(tags_section)
161+
full_text = f"{body[:max_body]}{separator}{link_section}{separator}{tags_section}"
164162

165163
encoded = full_text.encode("utf-8")
166164

167-
if post.link:
168-
link_bytes = post.link.encode("utf-8")
169-
link_idx = encoded.find(link_bytes)
170-
if link_idx != -1:
171-
facets.append({
172-
"index": {
173-
"byteStart": link_idx,
174-
"byteEnd": link_idx + len(link_bytes),
175-
},
176-
"features": [
177-
{"$type": "app.bsky.richtext.facet#link", "uri": post.link}
178-
],
179-
})
165+
link_bytes = post.link.encode("utf-8")
166+
link_idx = encoded.find(link_bytes)
167+
if link_idx != -1:
168+
facets.append({
169+
"index": {
170+
"byteStart": link_idx,
171+
"byteEnd": link_idx + len(link_bytes),
172+
},
173+
"features": [
174+
{"$type": "app.bsky.richtext.facet#link", "uri": post.link}
175+
],
176+
})
180177

181178
search_from = 0
182179
for tag_str in tag_strings:

tests/test_bluesky.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ def test_link_and_tags_facets_sorted_by_byte_start(self):
4141
for facet in facets[1:]:
4242
assert facet["features"][0]["$type"] == "app.bsky.richtext.facet#tag"
4343

44+
def test_long_body_still_includes_full_url_and_link_facet_with_tags(self):
45+
"""Without preserving the URL suffix, body[:max_body] would drop the link entirely."""
46+
url = "https://www.rescuegroups.org/html/sa_details.html?id=12345"
47+
tags = ["AdoptDontShop", "Boston", "DogsOfBluesky"]
48+
tags_section = " ".join(f"#{t}" for t in tags)
49+
filler = "x" * 400
50+
post = Post(text=f"{filler}\n\n{url}", tags=tags, link=url)
51+
text, facets = self.poster._build_text_and_facets(post)
52+
53+
assert url in text
54+
assert text.endswith(f"\n\n{tags_section}")
55+
assert len(text) <= 300
56+
link_facets = [f for f in facets if f["features"][0]["$type"] == "app.bsky.richtext.facet#link"]
57+
assert len(link_facets) == 1
58+
assert link_facets[0]["features"][0]["uri"] == url
59+
4460
def test_tags_produce_facets_with_correct_byte_offsets(self):
4561
post = Post(text="Adopt me!", tags=["AdoptDontShop", "Boston", "DogsOfBluesky"])
4662
text, facets = self.poster._build_text_and_facets(post)

0 commit comments

Comments
 (0)