Skip to content

Commit

Permalink
Merge pull request #346 from systemli/Add-support-for-Links-in-Bluesk…
Browse files Browse the repository at this point in the history
…y-Posts

✨ Add support for Links in Bluesky Posts
  • Loading branch information
0x46616c6b authored Jan 2, 2025
2 parents 8fcb144 + 233a01c commit ff60663
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
22 changes: 22 additions & 0 deletions internal/bridge/bluesky.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/systemli/ticker/internal/bluesky"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
"github.com/systemli/ticker/internal/util"
)

type BlueskyBridge struct {
Expand All @@ -40,6 +41,26 @@ func (bb *BlueskyBridge) Send(ticker storage.Ticker, message *storage.Message) e
post := &bsky.FeedPost{
Text: message.Text,
CreatedAt: time.Now().Local().Format(time.RFC3339),
Facets: []*bsky.RichtextFacet{},
}

links := util.ExtractURLs(message.Text)
for _, link := range links {
startIndex := strings.Index(message.Text, link)
endIndex := startIndex + len(link)
post.Facets = append(post.Facets, &bsky.RichtextFacet{
Features: []*bsky.RichtextFacet_Features_Elem{
{
RichtextFacet_Link: &bsky.RichtextFacet_Link{
Uri: link,
},
},
},
Index: &bsky.RichtextFacet_ByteSlice{
ByteStart: int64(startIndex),
ByteEnd: int64(endIndex),
},
})
}

if len(message.Attachments) > 0 {
Expand Down Expand Up @@ -76,6 +97,7 @@ func (bb *BlueskyBridge) Send(ticker storage.Ticker, message *storage.Message) e
if post.Embed == nil {
post.Embed = &bsky.FeedPost_Embed{}
}

post.Embed.EmbedImages = &bsky.EmbedImages{
Images: images,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *BridgeTestSuite) SetupTest() {
},
}
messageWithoutBridges = storage.Message{
Text: "Hello World",
Text: "Hello World https://example.com",
Attachments: []storage.Attachment{
{
UUID: "123",
Expand Down
9 changes: 9 additions & 0 deletions internal/util/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package util

import "regexp"

// ExtractURLs extracts URLs from a text.
func ExtractURLs(text string) []string {
urlRegex := regexp.MustCompile(`(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)`)
return urlRegex.FindAllString(text, -1)
}
39 changes: 39 additions & 0 deletions internal/util/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtractURL(t *testing.T) {
text := "This is a text with a URL https://example.com"
urls := ExtractURLs(text)

assert.Equal(t, 1, len(urls))
assert.Equal(t, "https://example.com", urls[0])

text = "This is a text with a URL https://example.com and another URL http://example.org"
urls = ExtractURLs(text)

assert.Equal(t, 2, len(urls))
assert.Equal(t, "https://example.com", urls[0])
assert.Equal(t, "http://example.org", urls[1])

text = "This is a text without a URL"
urls = ExtractURLs(text)

assert.Equal(t, 0, len(urls))

text = "This is a text with a URL https://www.systemli.org/en/contact/"
urls = ExtractURLs(text)

assert.Equal(t, 1, len(urls))
assert.Equal(t, "https://www.systemli.org/en/contact/", urls[0])

text = "This is a text with a URL https://www.systemli.org/en/contact/?key=value"
urls = ExtractURLs(text)

assert.Equal(t, 1, len(urls))
assert.Equal(t, "https://www.systemli.org/en/contact/?key=value", urls[0])
}

0 comments on commit ff60663

Please sign in to comment.