changeset 78:932aa9922d98

Add hashtag facets to Bluesky silo
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 28 Aug 2024 09:38:33 -0700
parents 5381655e7f6d
children 5c4c57aaf7b5
files silorider/silos/bluesky.py
diffstat 1 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/silorider/silos/bluesky.py	Wed Aug 28 08:40:52 2024 -0700
+++ b/silorider/silos/bluesky.py	Wed Aug 28 09:38:33 2024 -0700
@@ -22,6 +22,8 @@
 
 logger = logging.getLogger(__name__)
 
+re_hashtags = re.compile(r'#[\w\d]+')
+
 
 class _BlueskyClient(atproto.Client):
     def __init__(self, *args, **kwargs):
@@ -128,11 +130,10 @@
 
         # Grab any URLs detected by our URL flattener and add them as
         # facets on the atproto record.
-        facets = None
+        facets = []
         first_url = None
         url_flattener = entry_card.__bsky_url_flattener
         if url_flattener.urls:
-            facets = []
             for url_info in url_flattener.urls:
                 # atproto requires an http or https scheme.
                 start, end, url = url_info
@@ -142,13 +143,31 @@
                 facet = atprotomodels.AppBskyRichtextFacet.Main(
                     features=[atprotomodels.AppBskyRichtextFacet.Link(uri=url)],
                     index=atprotomodels.AppBskyRichtextFacet.ByteSlice(
-                        byteStart=start, byteEnd=end),
+                        byteStart=start, byteEnd=end)
                     )
                 facets.append(facet)
 
                 if first_url is None:
                     first_url = url
 
+        # Look for hashtags and turn them into facets too.
+        for htm in re_hashtags.finditer(entry_card.text):
+            start = htm.start()
+            end = htm.end()
+            tagname = htm.group()[1:]  # skip the hashtag character
+
+            facet = atprotomodels.AppBskyRichtextFacet.Main(
+                features=[atprotomodels.AppBskyRichtextFacet.Tag(tag=tagname)],
+                index=atprotomodels.AppBskyRichtextFacet.ByteSlice(
+                    byteStart=start, byteEnd=end)
+                )
+            facets.append(facet)
+
+        # A bit cleaner to pass None instead of an empty array if we don't
+        # have any facets.
+        if len(facets) == 0:
+            facets = None
+
         # Make a link embed for the first link if we didn't have an embed already.
         if embed is None and first_url is not None:
             embed = self._makeUrlEmbed(first_url)