Skip to content

Commit 510ab60

Browse files
committed
feat: (very basic) searching
1 parent aa36d24 commit 510ab60

5 files changed

Lines changed: 76 additions & 11 deletions

File tree

bluesky/blueskyapi.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ type ItemByWithDate struct {
246246
Actor User `json:"actor"`
247247
}
248248

249+
type PostSearchResult struct {
250+
Posts []Post `json:"posts"`
251+
HitsTotal int `json:"hitsTotal"`
252+
Cursor string `json:"cursor"`
253+
}
254+
249255
type UserSearchResult struct {
250256
Actors []User `json:"actors"`
251257
}
@@ -1209,6 +1215,35 @@ func UserSearch(pds string, token string, query string) ([]User, error) {
12091215
return users.Actors, nil
12101216
}
12111217

1218+
func PostSearch(pds string, token string, query string) ([]Post, error) {
1219+
url := pds + "/xrpc/app.bsky.feed.searchPosts?q=" + url.QueryEscape(query)
1220+
1221+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
1222+
if err != nil {
1223+
return nil, err
1224+
}
1225+
defer resp.Body.Close()
1226+
1227+
// // Print the response body
1228+
// bodyBytes, _ := io.ReadAll(resp.Body)
1229+
// bodyString := string(bodyBytes)
1230+
// fmt.Println("Response Body:", bodyString)
1231+
1232+
if resp.StatusCode != http.StatusOK {
1233+
bodyBytes, _ := io.ReadAll(resp.Body)
1234+
bodyString := string(bodyBytes)
1235+
fmt.Println("Response Status:", resp.StatusCode)
1236+
fmt.Println("Response Body:", bodyString)
1237+
return nil, errors.New("failed to fetch search results")
1238+
}
1239+
1240+
posts := PostSearchResult{}
1241+
if err := json.NewDecoder(resp.Body).Decode(&posts); err != nil {
1242+
return nil, err
1243+
}
1244+
return posts.Posts, nil
1245+
}
1246+
12121247
// thank you https://docs.bsky.app/blog/create-post#replies
12131248
func GetReplyRefs(pds string, token string, parentURI string) (*ReplySubject, error) {
12141249
// Get the parent post

bridge/bridge.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ type TwitterRecommendation struct {
290290
Token string `json:"token"`
291291
}
292292

293+
type InternalSearchResult struct {
294+
Statuses []Tweet `json:"statuses"`
295+
}
296+
293297
// Bluesky's API returns a letter ID for each user,
294298
// While twitter uses a numeric ID, meaning we
295299
// need to convert between the two

twitterv1/connect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,6 @@ func GetMyActivity(c *fiber.Ctx) error {
188188

189189
position++ // Increment position
190190
}
191+
191192
return c.JSON(twitterNotifications)
192193
}

twitterv1/discover.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,38 @@ import (
1717
// 2. Too common of a function to find
1818
// 3. Has a "non internal" version that is documented, but isn't this request.
1919

20-
// TODO: Implement this
21-
func Search(c *fiber.Ctx) error {
20+
func InternalSearch(c *fiber.Ctx) error {
2221
q := c.Query("q")
2322
fmt.Println("Search query:", q)
24-
return c.SendStatus(fiber.StatusNotImplemented)
23+
24+
_, pds, _, oauthToken, err := GetAuthFromReq(c)
25+
if err != nil {
26+
return c.Status(fiber.StatusUnauthorized).SendString("OAuth token not found in Authorization header")
27+
}
28+
29+
bskySearch, err := blueskyapi.PostSearch(*pds, *oauthToken, q)
30+
31+
if err != nil {
32+
fmt.Println("Error:", err)
33+
return c.Status(fiber.StatusInternalServerError).SendString("Failed to search")
34+
}
35+
36+
// Optimization: Get all users at once so we don't have to do it in chunks
37+
var dids []string
38+
for _, search := range bskySearch {
39+
dids = append(dids, search.Author.DID)
40+
}
41+
blueskyapi.GetUsersInfo(*pds, *oauthToken, dids, false)
42+
43+
// Translate to twitter
44+
tweets := []bridge.Tweet{}
45+
for _, search := range bskySearch {
46+
tweets = append(tweets, TranslatePostToTweet(search, "", "", nil, nil, *oauthToken, *pds))
47+
}
48+
49+
return c.JSON(bridge.InternalSearchResult{
50+
Statuses: tweets,
51+
})
2552
}
2653

2754
// https://web.archive.org/web/20120313235613/https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid

twitterv1/twitterv1.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package twitterv1
22

33
import (
4-
"fmt"
5-
64
blueskyapi "github.com/Preloading/MastodonTwitterAPI/bluesky"
75
"github.com/Preloading/MastodonTwitterAPI/config"
86
"github.com/gofiber/fiber/v2"
@@ -23,11 +21,11 @@ func InitServer(config *config.Config) {
2321

2422
// Custom middleware to log request details
2523
app.Use(func(c *fiber.Ctx) error {
26-
fmt.Println("Request Method:", c.Method())
27-
fmt.Println("Request URL:", c.OriginalURL())
28-
fmt.Println("Post Body:", string(c.Body()))
29-
fmt.Println("Headers:", string(c.Request().Header.Header()))
30-
fmt.Println()
24+
// fmt.Println("Request Method:", c.Method())
25+
// fmt.Println("Request URL:", c.OriginalURL())
26+
// fmt.Println("Post Body:", string(c.Body()))
27+
// fmt.Println("Headers:", string(c.Request().Header.Header()))
28+
// fmt.Println()
3129
return c.Next()
3230
})
3331

@@ -81,7 +79,7 @@ func InitServer(config *config.Config) {
8179

8280
// Discover
8381
app.Get("/1/trends/:woeid.json", trends_woeid)
84-
app.Get("/i/search.json", Search)
82+
app.Get("/i/search.json", InternalSearch)
8583

8684
// Setings
8785
app.Get("/1/account/settings.xml", GetSettings)

0 commit comments

Comments
 (0)