Skip to content

Commit ad2d5cf

Browse files
committed
Update: include description + test case
1 parent bd8eeee commit ad2d5cf

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ toolchain go1.24.1
66

77
require (
88
github.com/go-redis/redis/v8 v8.11.5
9+
github.com/stretchr/testify v1.9.0
910
github.com/tailscale/go-bluesky v0.0.0-20241115170709-693553a07285
1011
)
1112

1213
require (
1314
github.com/carlmjohnson/versioninfo v0.22.5 // indirect
1415
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1516
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
1618
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1719
github.com/dustin/go-humanize v1.0.1 // indirect
1820
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -26,6 +28,7 @@ require (
2628
github.com/klauspost/compress v1.18.0 // indirect
2729
github.com/minio/crc64nvme v1.0.1 // indirect
2830
github.com/minio/md5-simd v1.1.2 // indirect
31+
github.com/pmezard/go-difflib v1.0.0 // indirect
2932
github.com/rs/xid v1.6.0 // indirect
3033
github.com/russross/blackfriday/v2 v2.1.0 // indirect
3134
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
@@ -38,6 +41,7 @@ require (
3841
golang.org/x/text v0.23.0 // indirect
3942
google.golang.org/appengine v1.6.7 // indirect
4043
google.golang.org/protobuf v1.33.0 // indirect
44+
gopkg.in/yaml.v3 v3.0.1 // indirect
4145
)
4246

4347
require (

internal/bluesky/bluesky.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Client struct {
2222

2323
// PostRecord constructs a post record with a facet. To get there, it will find the
2424
// position of the URL inside the text and attaches it to the post.
25-
func PostRecord(title, url, author, stargazers, hashtags string) *bsky.FeedPost {
25+
func PostRecord(title, description, url, author, stargazers, hashtags string) *bsky.FeedPost {
2626
text := title
2727

2828
var startAuthor int64 = -1
@@ -36,6 +36,17 @@ func PostRecord(title, url, author, stargazers, hashtags string) *bsky.FeedPost
3636
text += fmt.Sprintf(" (%s)", stargazers)
3737
}
3838

39+
// 300 = limit + '#go'
40+
if len(text) < (300-3) && len(description) > 0 {
41+
// poor version of normalize
42+
description = strings.Join(strings.Fields(description), " ")
43+
if len(description) > 150 {
44+
text += "\n\n" + description[0:147] + "..."
45+
} else {
46+
text += "\n\n" + description
47+
}
48+
}
49+
3950
if len(hashtags) > 0 {
4051
text += "\n\n" + hashtags
4152
}
@@ -57,7 +68,7 @@ func PostRecord(title, url, author, stargazers, hashtags string) *bsky.FeedPost
5768
}
5869

5970
if len(hashtags) > 0 {
60-
allTags := strings.Split(hashtags, " ")
71+
allTags := strings.Fields(hashtags)
6172

6273
startHashTag := int64(strings.Index(text, hashtags))
6374

@@ -172,13 +183,16 @@ func handleError(ctx context.Context, err error) error {
172183
var netErr *net.OpError
173184
if errors.As(err, &netErr) {
174185
if netErr.Temporary() {
186+
slog.InfoContext(ctx, "temporary error: "+netErr.Error())
175187
return nil
176188
}
177189

178190
if netErr.Timeout() {
191+
slog.InfoContext(ctx, "timeout error: "+netErr.Error())
179192
return nil
180193
}
181194

195+
// unhandled net error
182196
slog.ErrorContext(ctx, netErr.Error())
183197
return err
184198
}

internal/bluesky/bluesky_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package bluesky_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/till/golangoss-bluesky/internal/bluesky"
8+
)
9+
10+
type testCase struct {
11+
Title string
12+
Description string
13+
URL string
14+
Author string
15+
Stargazers string
16+
Tag string
17+
ExpectedFacets int
18+
}
19+
20+
func TestPostRecord(t *testing.T) {
21+
testCases := []testCase{
22+
{
23+
Title: "simple",
24+
Description: "description",
25+
URL: "https://github.com/user/repo",
26+
Author: "@user",
27+
Stargazers: "1 ⭐️",
28+
Tag: "#go",
29+
ExpectedFacets: 3,
30+
},
31+
{
32+
Title: "extra-long-description",
33+
Description: "description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description, description",
34+
URL: "https://github.com/org/repo",
35+
Author: "",
36+
Stargazers: "1 ⭐️",
37+
Tag: "#go",
38+
ExpectedFacets: 2,
39+
},
40+
{
41+
Title: "Short",
42+
URL: "https://github.com/s/s",
43+
Author: "",
44+
Stargazers: "0 ⭐️",
45+
Tag: "",
46+
ExpectedFacets: 1,
47+
},
48+
}
49+
50+
for _, tc := range testCases {
51+
t.Run(tc.Title, func(t *testing.T) {
52+
record := bluesky.PostRecord(tc.Title, tc.Description, tc.URL, tc.Author, tc.Stargazers, tc.Tag)
53+
assert.NotNil(t, record)
54+
55+
assert.NotEmpty(t, record.CreatedAt)
56+
assert.Len(t, record.Facets, tc.ExpectedFacets)
57+
58+
assert.True(t, (len(record.Text) <= 300))
59+
})
60+
}
61+
}

internal/content/content.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ func Do(ctx context.Context, c bluesky.Client) error {
5555
stargazers = e
5656
continue
5757
}
58-
5958
}
6059
}
6160

62-
return c.Post(ctx, bluesky.PostRecord(*p.Title, *p.URL, author, stargazers, hashTags))
61+
return c.Post(ctx, bluesky.PostRecord(*p.Title, *p.Subtitle, *p.URL, author, stargazers, hashTags))
6362
}

0 commit comments

Comments
 (0)