Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support for Links in Bluesky Posts #346

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
Loading