Skip to content

Commit 1ef1db5

Browse files
committed
change tweet converter to get all user info
1 parent a15a2d2 commit 1ef1db5

3 files changed

Lines changed: 88 additions & 81 deletions

File tree

twitterv1/interaction.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func status_update(c *fiber.Ctx) error {
3939
}
4040

4141
if thread.Thread.Parent == nil {
42-
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil))
42+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil, *oauthToken))
4343
} else {
44-
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil))
44+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil, *oauthToken))
4545
}
4646
}
4747

@@ -72,15 +72,15 @@ func retweet(c *fiber.Ctx) error {
7272
return c.Status(fiber.StatusInternalServerError).SendString("Failed to update status")
7373
}
7474

75-
retweet := TranslatePostToTweet(originalPost.Thread.Post, originalPost.Thread.Post.URI, originalPost.Thread.Parent.Author.DID, &originalPost.Thread.Parent.Record.CreatedAt, nil)
75+
retweet := TranslatePostToTweet(originalPost.Thread.Post, originalPost.Thread.Post.URI, originalPost.Thread.Parent.Author.DID, &originalPost.Thread.Parent.Record.CreatedAt, nil, *oauthToken)
7676
retweet.Retweeted = true
7777
now := time.Now() // pain, also fix this to use the proper timestamp according to the server.
7878
retweet.ID = bridge.BskyMsgToTwitterID(*retweetPostURI, &now, user_did)
7979
retweet.IDStr = retweet.ID.String()
8080

8181
return c.JSON(bridge.Retweet{
8282
Tweet: retweet,
83-
RetweetedStatus: TranslatePostToTweet(originalPost.Thread.Post, originalPost.Thread.Post.URI, originalPost.Thread.Parent.Author.DID, &originalPost.Thread.Parent.Record.CreatedAt, nil), // TODO: make this respond with proper retweet data
83+
RetweetedStatus: TranslatePostToTweet(originalPost.Thread.Post, originalPost.Thread.Post.URI, originalPost.Thread.Parent.Author.DID, &originalPost.Thread.Parent.Record.CreatedAt, nil, *oauthToken), // TODO: make this respond with proper retweet data
8484
})
8585
}
8686

@@ -113,7 +113,7 @@ func favourite(c *fiber.Ctx) error {
113113
return c.Status(fiber.StatusInternalServerError).SendString("Failed to like post")
114114
}
115115

116-
newTweet := TranslatePostToTweet(post.Thread.Post, post.Thread.Post.URI, post.Thread.Parent.Author.DID, &post.Thread.Parent.Record.CreatedAt, nil)
116+
newTweet := TranslatePostToTweet(post.Thread.Post, post.Thread.Post.URI, post.Thread.Parent.Author.DID, &post.Thread.Parent.Record.CreatedAt, nil, *oauthToken)
117117

118118
return c.JSON(newTweet)
119119
}
@@ -137,9 +137,6 @@ func Unfavourite(c *fiber.Ctx) error { // yes i am canadian
137137
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
138138
}
139139
postId = *postIdPtr
140-
if err != nil {
141-
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
142-
}
143140

144141
err, post := blueskyapi.UnlikePost(*oauthToken, postId, *user_did)
145142

@@ -148,7 +145,7 @@ func Unfavourite(c *fiber.Ctx) error { // yes i am canadian
148145
return c.Status(fiber.StatusInternalServerError).SendString("Failed to unlike post")
149146
}
150147

151-
newTweet := TranslatePostToTweet(post.Thread.Post, post.Thread.Post.URI, post.Thread.Parent.Author.DID, &post.Thread.Parent.Record.CreatedAt, nil)
148+
newTweet := TranslatePostToTweet(post.Thread.Post, post.Thread.Post.URI, post.Thread.Parent.Author.DID, &post.Thread.Parent.Record.CreatedAt, nil, *oauthToken)
152149

153150
return c.JSON(newTweet)
154151
}

twitterv1/post_viewing.go

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func home_timeline(c *fiber.Ctx) error {
6262
tweets := []bridge.Tweet{}
6363

6464
for _, item := range res.Feed {
65-
tweets = append(tweets, TranslatePostToTweet(item.Post, item.Reply.Parent.URI, item.Reply.Parent.Author.DID, &item.Reply.Parent.Record.CreatedAt, item.Reason))
65+
tweets = append(tweets, TranslatePostToTweet(item.Post, item.Reply.Parent.URI, item.Reply.Parent.Author.DID, &item.Reply.Parent.Record.CreatedAt, item.Reason, *oauthToken))
6666
}
6767

6868
// Store the oldest message id, along with our context in the DB
@@ -105,9 +105,6 @@ func RelatedResults(c *fiber.Ctx) error {
105105
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
106106
}
107107
uri := *uriPtr
108-
if err != nil {
109-
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
110-
}
111108

112109
_, _, oauthToken, err := GetAuthFromReq(c)
113110
if err != nil {
@@ -120,6 +117,17 @@ func RelatedResults(c *fiber.Ctx) error {
120117
return err
121118
}
122119

120+
// Caching the user DIDs efficiently
121+
userDIDs := []string{}
122+
123+
for _, item := range *thread.Thread.Replies {
124+
if !slices.Contains(userDIDs, item.Post.Author.DID) {
125+
userDIDs = append(userDIDs, item.Post.Author.DID)
126+
}
127+
}
128+
129+
blueskyapi.GetUsersInfo(*oauthToken, userDIDs)
130+
123131
postAuthor := bridge.BlueSkyToTwitterID(thread.Thread.Post.Author.DID)
124132

125133
twitterReplies := bridge.RelatedResultsQuery{
@@ -134,7 +142,7 @@ func RelatedResults(c *fiber.Ctx) error {
134142
twitterReplies.Results = append(twitterReplies.Results, bridge.Results{
135143
Kind: "Tweet",
136144
Score: 1.0,
137-
Value: TranslatePostToTweet(reply.Post, uri, postAuthor.String(), &thread.Thread.Post.Record.CreatedAt, nil),
145+
Value: TranslatePostToTweet(reply.Post, uri, postAuthor.String(), &thread.Thread.Post.Record.CreatedAt, nil, *oauthToken),
138146
Annotations: []bridge.Annotations{
139147
{
140148
ConversationRole: "Descendant",
@@ -160,9 +168,6 @@ func GetStatusFromId(c *fiber.Ctx) error {
160168
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
161169
}
162170
uri := *uriPtr
163-
if err != nil {
164-
return c.Status(fiber.StatusBadRequest).SendString("Invalid ID format")
165-
}
166171

167172
fmt.Println("URI:", uri)
168173
_, _, oauthToken, err := GetAuthFromReq(c)
@@ -179,13 +184,13 @@ func GetStatusFromId(c *fiber.Ctx) error {
179184

180185
// TODO: Some things may be needed for reposts to show up correctly. thats a later problem :)
181186
if thread.Thread.Parent == nil {
182-
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil))
187+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil, *oauthToken))
183188
} else {
184-
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil))
189+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil, *oauthToken))
185190
}
186191
}
187192

188-
func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUserBskyId string, replyTimeStamp *time.Time, postReason *blueskyapi.PostReason) bridge.Tweet {
193+
func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUserBskyId string, replyTimeStamp *time.Time, postReason *blueskyapi.PostReason, token string) bridge.Tweet {
189194
tweetEntities := bridge.Entities{
190195
Hashtags: nil,
191196
Urls: nil,
@@ -256,6 +261,16 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs
256261
}
257262
}
258263

264+
// Get the user info
265+
var author *bridge.TwitterUser
266+
author, err := blueskyapi.GetUserInfo(token, tweet.Author.DID)
267+
if err != nil {
268+
fmt.Println("Error:", err)
269+
// fallback
270+
authorPtr := GetUserInfoFromTweetData(tweet)
271+
author = &authorPtr
272+
}
273+
259274
convertedTweet := bridge.Tweet{
260275
Coordinates: nil,
261276
Favourited: tweet.Viewer.Like != nil,
@@ -306,56 +321,8 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs
306321
return &idStr
307322
}(),
308323
InReplyToScreenName: &tweet.Author.DisplayName,
309-
User: bridge.TwitterUser{
310-
Name: func() string {
311-
if tweet.Author.DisplayName == "" {
312-
return tweet.Author.Handle
313-
}
314-
return tweet.Author.DisplayName
315-
}(),
316-
ProfileSidebarBorderColor: "eeeeee",
317-
ProfileBackgroundTile: false,
318-
ProfileSidebarFillColor: "efefef",
319-
CreatedAt: bridge.TwitterTimeConverter(tweet.Author.Associated.CreatedAt),
320-
ProfileImageURL: "http://10.0.0.77:3000/cdn/img/?url=" + url.QueryEscape(tweet.Author.Avatar) + "&width=128&height=128",
321-
// ProfileImageURLHttps: "https://10.0.0.77:3000/cdn/img/?url=" + url.QueryEscape(tweet.Author.Avatar) + "&width=128&height=128",
322-
Location: "Twitter",
323-
ProfileLinkColor: "009999",
324-
FollowRequestSent: false,
325-
URL: "",
326-
ScreenName: tweet.Author.Handle,
327-
ContributorsEnabled: false,
328-
UtcOffset: nil,
329-
IsTranslator: false,
330-
ID: *bridge.BlueSkyToTwitterID(tweet.URI),
331-
// IDStr: bridge.BlueSkyToTwitterID(tweet.URI).String(),
332-
ProfileUseBackgroundImage: false,
333-
ProfileTextColor: "333333",
334-
Protected: false,
335-
Lang: "en",
336-
Notifications: nil,
337-
TimeZone: nil,
338-
Verified: false,
339-
ProfileBackgroundColor: "C0DEED",
340-
GeoEnabled: true,
341-
Description: "",
342-
ProfileBackgroundImageURL: "http://a0.twimg.com/images/themes/theme1/bg.png",
343-
// ProfileBackgroundImageURLHttps: "http://a0.twimg.com/images/themes/theme1/bg.png",
344-
Following: nil,
345-
346-
// huh
347-
DefaultProfile: false,
348-
DefaultProfileImage: false,
349-
ShowAllInlineMedia: false,
350-
351-
// User Stats
352-
// ListedCount: 0,
353-
// FavouritesCount: 0,
354-
// FollowersCount: 200,
355-
// FriendsCount: 100,
356-
// StatusesCount: 333,
357-
},
358-
Source: "Bluesky",
324+
User: *author,
325+
Source: "Bluesky",
359326
InReplyToStatusID: func() *big.Int {
360327
if replyMsgBskyURI == "" {
361328
return nil
@@ -375,7 +342,7 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs
375342
if isRetweet {
376343
retweet_bsky := tweet
377344
retweet_bsky.Author = bsky_retweet_author
378-
translatedTweet := TranslatePostToTweet(retweet_bsky, replyMsgBskyURI, replyUserBskyId, replyTimeStamp, nil)
345+
translatedTweet := TranslatePostToTweet(retweet_bsky, replyMsgBskyURI, replyUserBskyId, replyTimeStamp, nil, token)
379346
return &translatedTweet
380347
}
381348
return nil
@@ -384,6 +351,59 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs
384351
return convertedTweet
385352
}
386353

354+
// This is "depercated"/a togglable option in the config (eventually)
355+
func GetUserInfoFromTweetData(tweet blueskyapi.Post) bridge.TwitterUser {
356+
return bridge.TwitterUser{
357+
Name: func() string {
358+
if tweet.Author.DisplayName == "" {
359+
return tweet.Author.Handle
360+
}
361+
return tweet.Author.DisplayName
362+
}(),
363+
ProfileSidebarBorderColor: "eeeeee",
364+
ProfileBackgroundTile: false,
365+
ProfileSidebarFillColor: "efefef",
366+
CreatedAt: bridge.TwitterTimeConverter(tweet.Author.Associated.CreatedAt),
367+
ProfileImageURL: "http://10.0.0.77:3000/cdn/img/?url=" + url.QueryEscape(tweet.Author.Avatar) + "&width=128&height=128",
368+
// ProfileImageURLHttps: "https://10.0.0.77:3000/cdn/img/?url=" + url.QueryEscape(tweet.Author.Avatar) + "&width=128&height=128",
369+
Location: "Twitter",
370+
ProfileLinkColor: "009999",
371+
FollowRequestSent: false,
372+
URL: "",
373+
ScreenName: tweet.Author.Handle,
374+
ContributorsEnabled: false,
375+
UtcOffset: nil,
376+
IsTranslator: false,
377+
ID: *bridge.BlueSkyToTwitterID(tweet.URI),
378+
// IDStr: bridge.BlueSkyToTwitterID(tweet.URI).String(),
379+
ProfileUseBackgroundImage: false,
380+
ProfileTextColor: "333333",
381+
Protected: false,
382+
Lang: "en",
383+
Notifications: nil,
384+
TimeZone: nil,
385+
Verified: false,
386+
ProfileBackgroundColor: "C0DEED",
387+
GeoEnabled: true,
388+
Description: "",
389+
ProfileBackgroundImageURL: "http://a0.twimg.com/images/themes/theme1/bg.png",
390+
// ProfileBackgroundImageURLHttps: "http://a0.twimg.com/images/themes/theme1/bg.png",
391+
Following: nil,
392+
393+
// huh
394+
DefaultProfile: false,
395+
DefaultProfileImage: false,
396+
ShowAllInlineMedia: false,
397+
398+
// User Stats
399+
// ListedCount: 0,
400+
// FavouritesCount: 0,
401+
// FollowersCount: 200,
402+
// FriendsCount: 100,
403+
// StatusesCount: 333,
404+
}
405+
}
406+
387407
// This request is an "internal" request, and thus, these are very little to no docs. this is a problem.
388408
// The most docs I could find: https://blog.fgribreau.com/2012/01/twitter-unofficial-api-getting-tweets.html
389409
func TweetInfo(c *fiber.Ctx) error {

twitterv1/user.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import (
1010
"github.com/gofiber/fiber/v2"
1111
)
1212

13-
// groupUsers splits a slice of users into chunks of the specified size
14-
func groupUsers(users []string, size int) [][]string {
15-
var groups [][]string
16-
for size < len(users) {
17-
users, groups = users[size:], append(groups, users[0:size:size])
18-
}
19-
groups = append(groups, users)
20-
return groups
21-
}
22-
2313
// https://web.archive.org/web/20120508075505/https://dev.twitter.com/docs/api/1/get/users/show
2414
func user_info(c *fiber.Ctx) error {
2515
screen_name := c.Query("screen_name")

0 commit comments

Comments
 (0)