Skip to content

Commit f2efcc5

Browse files
committed
feat: implement searching for users
1 parent d2d00db commit f2efcc5

2 files changed

Lines changed: 74 additions & 13 deletions

File tree

bluesky/blueskyapi.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type AuthRequest struct {
2525
Password string `json:"password"`
2626
}
2727

28-
type Author struct {
28+
type User struct {
2929
DID string `json:"did"`
3030
Handle string `json:"handle"`
3131
DisplayName string `json:"displayName"`
@@ -58,7 +58,7 @@ type PostRecord struct {
5858
// Specifically for reposts
5959
type PostReason struct {
6060
Type string `json:"$type"`
61-
By Author `json:"by"`
61+
By User `json:"by"`
6262
IndexedAt time.Time `json:"indexedAt"`
6363
}
6464

@@ -118,7 +118,7 @@ type PostViewer struct {
118118
}
119119
type Post struct {
120120
Subject
121-
Author Author `json:"author"`
121+
Author User `json:"author"`
122122
Record PostRecord `json:"record"`
123123
// Embed Embed `json:"embed"`
124124
ReplyCount int `json:"replyCount"`
@@ -193,8 +193,8 @@ type CreateRecordResult struct {
193193

194194
type RepostedBy struct {
195195
Subject
196-
Cursor string `json:"cursor"`
197-
RepostedBy []Author `json:"repostedBy"`
196+
Cursor string `json:"cursor"`
197+
RepostedBy []User `json:"repostedBy"`
198198
}
199199
type Likes struct {
200200
Subject
@@ -205,7 +205,11 @@ type Likes struct {
205205
type ItemByWithDate struct {
206206
IndexedAt time.Time `json:"indexedAt"`
207207
CreatedAt time.Time `json:"createdAt"`
208-
Actor Author `json:"actor"`
208+
Actor User `json:"actor"`
209+
}
210+
211+
type UserSearchResult struct {
212+
Actors []User `json:"actors"`
209213
}
210214

211215
func SendRequest(token *string, method string, url string, body io.Reader) (*http.Response, error) {
@@ -303,7 +307,7 @@ func GetUserInfo(token string, screen_name string) (*bridge.TwitterUser, error)
303307
return nil, errors.New("failed to fetch user info")
304308
}
305309

306-
author := Author{}
310+
author := User{}
307311
if err := json.NewDecoder(resp.Body).Decode(&author); err != nil {
308312
return nil, err
309313
}
@@ -328,7 +332,7 @@ func GetUsersInfo(token string, items []string) ([]*bridge.TwitterUser, error) {
328332
}
329333

330334
var authors struct {
331-
Profiles []Author `json:"profiles"`
335+
Profiles []User `json:"profiles"`
332336
}
333337
if err := json.NewDecoder(resp.Body).Decode(&authors); err != nil {
334338
return nil, err
@@ -342,7 +346,7 @@ func GetUsersInfo(token string, items []string) ([]*bridge.TwitterUser, error) {
342346
return users, nil
343347
}
344348

345-
func AuthorTTB(author Author) *bridge.TwitterUser {
349+
func AuthorTTB(author User) *bridge.TwitterUser {
346350
return &bridge.TwitterUser{
347351
ProfileSidebarFillColor: "e0ff92",
348352
Name: func() string {
@@ -666,6 +670,31 @@ func GetRetweetAuthors(token string, uri string, limit int) (*RepostedBy, error)
666670
return &retweetAuthors, nil
667671
}
668672

669-
// func UserSearch(token string, query string) (bridge.TwitterUser, error) {
673+
func UserSearch(token string, query string) ([]User, error) {
674+
url := "https://public.bsky.social/xrpc/app.bsky.actor.searchActors?q=" + query
675+
676+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
677+
if err != nil {
678+
return nil, err
679+
}
680+
defer resp.Body.Close()
670681

671-
// }
682+
// // Print the response body
683+
// bodyBytes, _ := io.ReadAll(resp.Body)
684+
// bodyString := string(bodyBytes)
685+
// fmt.Println("Response Body:", bodyString)
686+
687+
if resp.StatusCode != http.StatusOK {
688+
bodyBytes, _ := io.ReadAll(resp.Body)
689+
bodyString := string(bodyBytes)
690+
fmt.Println("Response Status:", resp.StatusCode)
691+
fmt.Println("Response Body:", bodyString)
692+
return nil, errors.New("failed to fetch search results")
693+
}
694+
695+
users := UserSearchResult{}
696+
if err := json.NewDecoder(resp.Body).Decode(&users); err != nil {
697+
return nil, err
698+
}
699+
return users.Actors, nil
700+
}

twitterv1/connect.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
package twitterv1
22

3-
import "github.com/gofiber/fiber/v2"
3+
import (
4+
"fmt"
5+
6+
blueskyapi "github.com/Preloading/MastodonTwitterAPI/bluesky"
7+
"github.com/gofiber/fiber/v2"
8+
)
49

510
func UserSearch(c *fiber.Ctx) error {
6-
return c.SendStatus(fiber.StatusNotImplemented)
11+
searchQuery := c.Query("q")
12+
_, _, oauthToken, err := GetAuthFromReq(c)
13+
14+
if err != nil {
15+
return c.Status(fiber.StatusUnauthorized).SendString("OAuth token not found in Authorization header")
16+
}
17+
// Search for users
18+
bskyUsers, err := blueskyapi.UserSearch(*oauthToken, searchQuery)
19+
20+
if err != nil {
21+
fmt.Println("Error:", err)
22+
return c.Status(fiber.StatusInternalServerError).SendString("Failed to search")
23+
}
24+
// Get complete user info.
25+
// We must do this as the search API only returns a subset of the user info, and twitter wants all of it.
26+
27+
// Extract the dids into a string array
28+
var dids []string
29+
for _, user := range bskyUsers {
30+
dids = append(dids, user.DID)
31+
}
32+
users, err := LookupUsers(dids, oauthToken)
33+
if err != nil {
34+
fmt.Println("Error:", err)
35+
return c.Status(fiber.StatusInternalServerError).SendString("Failed to get user info")
36+
}
37+
38+
return c.JSON(users)
739
}

0 commit comments

Comments
 (0)