Skip to content

Commit 0dea0d4

Browse files
committed
fix: some URL compatibility changes
1 parent ff5b788 commit 0dea0d4

3 files changed

Lines changed: 155 additions & 117 deletions

File tree

twitterv1/post_viewing.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,11 @@ func hot_post_timeline(c *fiber.Ctx) error {
3434
}
3535

3636
func user_timeline(c *fiber.Ctx) error {
37-
actor := c.Query("screen_name")
38-
if actor == "" {
39-
actor = c.Query("user_id")
40-
if actor == "" {
41-
return ReturnError(c, "No user provided", 195, fiber.StatusForbidden)
42-
}
43-
actorInt, err := strconv.ParseInt(actor, 10, 64)
44-
if err != nil {
45-
return ReturnError(c, "Invalid user_id provided", 195, fiber.StatusForbidden)
46-
}
47-
actorPtr, err := bridge.TwitterIDToBlueSky(&actorInt)
48-
if err != nil {
49-
return ReturnError(c, "Invalid user_id provided", 195, fiber.StatusForbidden)
50-
}
51-
actor = *actorPtr
37+
actorPtr, err := GetUserSpecifiedInRequest(c, nil)
38+
if err != nil {
39+
return err
5240
}
41+
actor := *actorPtr
5342
return convert_timeline(c, actor, false, blueskyapi.GetUserTimeline)
5443
}
5544

twitterv1/twitterv1.go

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"encoding/json"
66
"encoding/xml"
77
"fmt"
8+
"strconv"
89
"strings"
910

1011
blueskyapi "github.com/Preloading/TwitterAPIBridge/bluesky"
12+
"github.com/Preloading/TwitterAPIBridge/bridge"
1113
"github.com/Preloading/TwitterAPIBridge/config"
1214
"github.com/gofiber/fiber/v2"
1315
"github.com/gofiber/fiber/v2/middleware/logger"
@@ -106,7 +108,9 @@ func InitServer(config *config.Config) {
106108

107109
// Posts
108110
AddV1Path(app.Get, "/statuses/home_timeline.:filetype", home_timeline)
111+
AddV1Path(app.Get, "/statuses/friends_timeline.:filetype", home_timeline)
109112
AddV1Path(app.Get, "/statuses/user_timeline.:filetype", user_timeline)
113+
AddV1Path(app.Get, "/statuses/user_timeline/*", HandleFiletypeSplitter(user_timeline))
110114
AddV1Path(app.Get, "/statuses/mentions.:filetype", mentions_timeline)
111115
AddV1Path(app.Get, "/favorites/toptweets.:filetype", hot_post_timeline)
112116
AddV1Path(app.Get, "/tatuses/media_timeline.:filetype", media_timeline)
@@ -116,21 +120,7 @@ func InitServer(config *config.Config) {
116120

117121
// Users
118122
AddV1Path(app.Get, "/users/show.:filetype", user_info)
119-
AddV1Path(app.Get, "/users/show/*", func(c *fiber.Ctx) error {
120-
path := c.Params("*")
121-
lastDotIndex := strings.LastIndex(path, ".")
122-
123-
if lastDotIndex == -1 {
124-
// No file extension found
125-
c.Locals("handle", path)
126-
c.Locals("filetype", "json") // Default to JSON
127-
} else {
128-
c.Locals("handle", path[:lastDotIndex])
129-
c.Locals("filetype", path[lastDotIndex+1:])
130-
}
131-
132-
return user_info(c)
133-
})
123+
AddV1Path(app.Get, "/users/show/*", HandleFiletypeSplitter(user_info))
134124
AddV1Path(app.Get, "/users/lookup.:filetype", UsersLookup)
135125
AddV1Path(app.Post, "/users/lookup.:filetype", UsersLookup)
136126
AddV1Path(app.Get, "/friendships/lookup.:filetype", UserRelationships)
@@ -143,6 +133,10 @@ func InitServer(config *config.Config) {
143133
AddV1Path(app.Get, "/friends.:filetype", GetFollows)
144134
AddV1Path(app.Get, "/statuses/followers.:filetype", GetStatusesFollowers)
145135
AddV1Path(app.Get, "/statuses/friends.:filetype", GetStatusesFollows)
136+
AddV1Path(app.Get, "/friends/ids.:filetype", GetFollowingIds)
137+
AddV1Path(app.Get, "/friends/ids/*", HandleFiletypeSplitter(GetFollowingIds))
138+
AddV1Path(app.Get, "/followers/ids.:filetype", GetFollowersIds)
139+
AddV1Path(app.Get, "/followers/ids/*", HandleFiletypeSplitter(GetFollowersIds))
146140
app.Get("/i/device_following/ids.:filetype", GetFollowingIds)
147141

148142
AddV1Path(app.Get, "/users/recommendations.:filetype", GetSuggestedUsers)
@@ -206,11 +200,60 @@ func InitServer(config *config.Config) {
206200
app.Listen(fmt.Sprintf(":%d", config.ServerPort))
207201
}
208202

203+
func HandleFiletypeSplitter(handler fiber.Handler) fiber.Handler {
204+
return func(c *fiber.Ctx) error {
205+
path := c.Params("*")
206+
lastDotIndex := strings.LastIndex(path, ".")
207+
208+
if lastDotIndex == -1 {
209+
// No file extension found
210+
c.Locals("handle", path)
211+
c.Locals("filetype", "json") // Default to JSON
212+
} else {
213+
c.Locals("handle", path[:lastDotIndex])
214+
c.Locals("filetype", path[lastDotIndex+1:])
215+
}
216+
217+
return handler(c)
218+
}
219+
}
220+
209221
func AddV1Path(function func(string, ...fiber.Handler) fiber.Router, url string, handler fiber.Handler) {
210222
function(url, handler)
211223
function(fmt.Sprintf("/1%s", url), handler)
212224
}
213225

226+
func GetUserSpecifiedInRequest(c *fiber.Ctx, no_value_default *string) (*string, error) {
227+
actor := c.FormValue("user_id")
228+
if actor == "" {
229+
actor = c.FormValue("screen_name")
230+
if actor == "" {
231+
actor = c.Locals("handle").(string)
232+
}
233+
if actor == "" {
234+
if no_value_default != nil {
235+
actor = *no_value_default
236+
} else {
237+
return nil, ReturnError(c, "No user was specified", 195, 403)
238+
}
239+
}
240+
} else {
241+
id, err := strconv.ParseInt(actor, 10, 64)
242+
if err != nil {
243+
return nil, ReturnError(c, "Invalid ID format", 195, 403)
244+
}
245+
actorPtr, err := bridge.TwitterIDToBlueSky(&id)
246+
if err != nil {
247+
return nil, ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
248+
}
249+
if actorPtr == nil {
250+
return nil, ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
251+
}
252+
actor = *actorPtr
253+
}
254+
return &actor, nil
255+
}
256+
214257
// misc
215258
func MobileClientApiDecider(c *fiber.Ctx) error {
216259
return c.SendString("") // todo maybe?

twitterv1/user.go

Lines changed: 93 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -422,33 +422,17 @@ func UnfollowUserParams(c *fiber.Ctx) error {
422422
// At the moment we are not doing pagination, so this will only return the first ~50 followers.
423423
func GetStatusesFollowers(c *fiber.Ctx) error {
424424
// auth
425-
_, pds, _, oauthToken, err := GetAuthFromReq(c)
425+
userDID, pds, _, oauthToken, err := GetAuthFromReq(c)
426426
if err != nil {
427427
return MissingAuth(c, err)
428428
}
429429

430430
// lets go get our user data
431-
432-
actor := c.FormValue("user_id")
433-
if actor == "" {
434-
actor = c.FormValue("screen_name")
435-
if actor == "" {
436-
return ReturnError(c, "No user was specified", 195, 403)
437-
}
438-
} else {
439-
id, err := strconv.ParseInt(actor, 10, 64)
440-
if err != nil {
441-
return ReturnError(c, "Invalid ID format", 195, 403)
442-
}
443-
actorPtr, err := bridge.TwitterIDToBlueSky(&id)
444-
if err != nil {
445-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
446-
}
447-
if actorPtr == nil {
448-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
449-
}
450-
actor = *actorPtr
431+
actorPtr, err := GetUserSpecifiedInRequest(c, userDID)
432+
if err != nil {
433+
return err
451434
}
435+
actor := *actorPtr
452436

453437
// fetch followers
454438
followers, err := blueskyapi.GetFollowers(*pds, *oauthToken, "", actor)
@@ -489,30 +473,11 @@ func GetFollowers(c *fiber.Ctx) error {
489473
}
490474

491475
// lets go get our user data
492-
actor := c.FormValue("user_id")
493-
if actor == "" {
494-
actor = c.FormValue("screen_name")
495-
if actor == "" {
496-
if userDID != nil {
497-
actor = *userDID
498-
} else {
499-
return ReturnError(c, "No user was specified", 195, 403)
500-
}
501-
}
502-
} else {
503-
id, err := strconv.ParseInt(actor, 10, 64)
504-
if err != nil {
505-
return ReturnError(c, "Invalid ID format", 195, 403)
506-
}
507-
actorPtr, err := bridge.TwitterIDToBlueSky(&id)
508-
if err != nil {
509-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
510-
}
511-
if actorPtr == nil {
512-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
513-
}
514-
actor = *actorPtr
476+
actorPtr, err := GetUserSpecifiedInRequest(c, userDID)
477+
if err != nil {
478+
return err
515479
}
480+
actor := *actorPtr
516481

517482
cursor := ""
518483
var cursorInt int64
@@ -598,33 +563,17 @@ func GetFollowers(c *fiber.Ctx) error {
598563
// https://web.archive.org/web/20120407214017/https://dev.twitter.com/docs/api/1/get/statuses/friends
599564
func GetStatusesFollows(c *fiber.Ctx) error {
600565
// auth
601-
_, pds, _, oauthToken, err := GetAuthFromReq(c)
566+
userDID, pds, _, oauthToken, err := GetAuthFromReq(c)
602567
if err != nil {
603568
return MissingAuth(c, err)
604569
}
605570

606571
// lets go get our user data
607-
608-
actor := c.FormValue("user_id")
609-
if actor == "" {
610-
actor = c.FormValue("screen_name")
611-
if actor == "" {
612-
return ReturnError(c, "No user was specified", 195, 403)
613-
}
614-
} else {
615-
id, err := strconv.ParseInt(actor, 10, 64)
616-
if err != nil {
617-
return ReturnError(c, "Invalid ID format", 195, 403)
618-
}
619-
actorPtr, err := bridge.TwitterIDToBlueSky(&id)
620-
if err != nil {
621-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
622-
}
623-
if actorPtr == nil {
624-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
625-
}
626-
actor = *actorPtr
572+
actorPtr, err := GetUserSpecifiedInRequest(c, userDID)
573+
if err != nil {
574+
return err
627575
}
576+
actor := *actorPtr
628577

629578
// fetch follows
630579
followers, err := blueskyapi.GetFollows(*pds, *oauthToken, "", actor)
@@ -777,31 +726,88 @@ func GetFollowingIds(c *fiber.Ctx) error {
777726
}
778727

779728
// lets go get our user data
780-
actor := c.FormValue("user_id")
781-
if actor == "" {
782-
actor = c.FormValue("screen_name")
783-
if actor == "" {
784-
if userDID != nil {
785-
actor = *userDID
786-
} else {
787-
return ReturnError(c, "No user was specified", 195, 403)
729+
actorPtr, err := GetUserSpecifiedInRequest(c, userDID)
730+
if err != nil {
731+
return err
732+
}
733+
actor := *actorPtr
734+
735+
cursor := ""
736+
var cursorInt int64
737+
738+
cursorStr := c.FormValue("cursor")
739+
if cursorStr != "" {
740+
cursorInt, err = strconv.ParseInt(cursorStr, 10, 64)
741+
if err != nil || cursorInt > 1 {
742+
cursor, err = bridge.NumToTid(uint64(cursorInt))
743+
if err != nil {
744+
fmt.Println("Error when converting Followers Cursor:", err)
745+
cursor = ""
788746
}
747+
} else {
748+
cursor = ""
789749
}
790750
} else {
791-
id, err := strconv.ParseInt(actor, 10, 64)
792-
if err != nil {
793-
return ReturnError(c, "Invalid ID format", 195, 403)
794-
}
795-
actorPtr, err := bridge.TwitterIDToBlueSky(&id)
796-
if err != nil {
797-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
798-
}
799-
if actorPtr == nil {
800-
return ReturnError(c, "ID not found.", 144, fiber.StatusNotFound)
801-
}
802-
actor = *actorPtr
751+
cursor = ""
752+
}
753+
754+
if cursorInt == 0 {
755+
return EncodeAndSend(c, struct {
756+
Users []bridge.TwitterUser `json:"users" xml:"users"`
757+
bridge.Cursors
758+
}{
759+
Users: []bridge.TwitterUser{},
760+
Cursors: bridge.Cursors{
761+
NextCursor: 0,
762+
PreviousCursor: 0, // Unimplemented. This could probably be figured out if i could figure out what the TID corrisponds to, if it corrisponds to anything at all.
763+
NextCursorStr: "0",
764+
PreviousCursorStr: "0",
765+
},
766+
})
803767
}
804768

769+
// fetch follows
770+
followers, err := blueskyapi.GetFollows(*pds, *oauthToken, cursor, actor)
771+
if err != nil {
772+
fmt.Println("Error:", err)
773+
return HandleBlueskyError(c, err.Error(), "app.bsky.graph.getFollows", GetFollowingIds)
774+
}
775+
776+
var userIDs []int64
777+
for _, user := range followers.Followers {
778+
userIDs = append(userIDs, *bridge.BlueSkyToTwitterID(user.DID))
779+
}
780+
781+
next_cursor, err := bridge.TidToNum(followers.Cursor)
782+
if err != nil {
783+
next_cursor = 0
784+
}
785+
786+
return EncodeAndSend(c, bridge.IdsWithCursor{
787+
Ids: userIDs,
788+
Cursors: bridge.Cursors{
789+
NextCursor: next_cursor,
790+
PreviousCursor: 0, // Unimplemented. This could probably be figured out if i could figure out what the TID corrisponds to, if it corrisponds to anything at all.
791+
NextCursorStr: strconv.FormatUint(next_cursor, 10),
792+
PreviousCursorStr: "0",
793+
},
794+
})
795+
}
796+
797+
func GetFollowersIds(c *fiber.Ctx) error {
798+
// auth
799+
userDID, pds, _, oauthToken, err := GetAuthFromReq(c)
800+
if err != nil {
801+
return MissingAuth(c, err)
802+
}
803+
804+
// lets go get our user data
805+
actorPtr, err := GetUserSpecifiedInRequest(c, userDID)
806+
if err != nil {
807+
return err
808+
}
809+
actor := *actorPtr
810+
805811
cursor := ""
806812
var cursorInt int64
807813

@@ -837,10 +843,10 @@ func GetFollowingIds(c *fiber.Ctx) error {
837843
}
838844

839845
// fetch follows
840-
followers, err := blueskyapi.GetFollows(*pds, *oauthToken, cursor, actor)
846+
followers, err := blueskyapi.GetFollowers(*pds, *oauthToken, cursor, actor)
841847
if err != nil {
842848
fmt.Println("Error:", err)
843-
return HandleBlueskyError(c, err.Error(), "app.bsky.graph.getFollows", GetFollows)
849+
return HandleBlueskyError(c, err.Error(), "app.bsky.graph.getFollowers", GetFollowersIds)
844850
}
845851

846852
var userIDs []int64

0 commit comments

Comments
 (0)