Skip to content

Commit bbb5576

Browse files
committed
some polish onto mentions
1 parent 31d9014 commit bbb5576

4 files changed

Lines changed: 78 additions & 20 deletions

File tree

bridge/bridge.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ func TwitterMsgIdToBluesky(id *int64) (*string, *time.Time, *string, error) {
632632
return nil, nil, nil, err
633633
}
634634

635+
if createdAt == nil {
636+
return nil, nil, nil, err
637+
}
638+
635639
return uri, createdAt, retweetUserId, nil
636640
}
637641

db_controller/db_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func InitDB(_cfg config.Config) {
148148
db.AutoMigrate(&ShortLink{})
149149
db.AutoMigrate(&NotificationTokens{})
150150

151+
db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_tokens_user_did_token_uuid ON tokens(user_did, token_uuid)`) // annoying!
152+
151153
StartPeriodicAnalyticsWriter(time.Minute)
152154
}
153155

twitterv1/post_viewing.go

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,9 @@ func mentions_timeline(c *fiber.Ctx) error {
11751175
}
11761176

11771177
// Handle pagination
1178-
context := ""
11791178
max_id := c.Query("max_id")
1179+
context := ""
1180+
11801181
// Handle getting things in the past
11811182
if max_id != "" {
11821183
// Get the timeline context from the DB
@@ -1187,11 +1188,49 @@ func mentions_timeline(c *fiber.Ctx) error {
11871188
}
11881189
_, date, _, err := bridge.TwitterMsgIdToBluesky(&maxIDInt)
11891190
if err != nil {
1190-
return ReturnError(c, "max_id was not found", 144, fiber.StatusForbidden)
1191+
tempId := maxIDInt - 1
1192+
_, date, _, err = bridge.TwitterMsgIdToBluesky(&tempId)
1193+
if err != nil {
1194+
tempId = maxIDInt + 1
1195+
_, date, _, err = bridge.TwitterMsgIdToBluesky(&tempId)
1196+
if err != nil {
1197+
return ReturnError(c, "max_id was not found", 144, fiber.StatusForbidden)
1198+
1199+
}
1200+
}
11911201
}
11921202
context = date.Format(time.RFC3339)
11931203
}
11941204

1205+
since_id := c.Query("since_id")
1206+
since_date := time.Time{}
1207+
hasSinceDate := false
1208+
1209+
// Handle getting things in the past
1210+
if since_id != "" {
1211+
// Get the timeline context from the DB
1212+
sinceIdInt, err := strconv.ParseInt(since_id, 10, 64)
1213+
if err != nil {
1214+
return ReturnError(c, "Invalid since_id format", 195, fiber.StatusForbidden)
1215+
}
1216+
_, tempDate, _, err := bridge.TwitterMsgIdToBluesky(&sinceIdInt)
1217+
if err != nil {
1218+
// the offical twitter docs recommended you add one to the id. Because of... technical debt, our IDs do not support this, and thus this must be done
1219+
tempId := sinceIdInt - 1
1220+
_, tempDate, _, err = bridge.TwitterMsgIdToBluesky(&tempId)
1221+
if err != nil {
1222+
tempId = sinceIdInt + 1
1223+
_, tempDate, _, err = bridge.TwitterMsgIdToBluesky(&tempId)
1224+
if err != nil {
1225+
return ReturnError(c, "since_id was not found", 144, fiber.StatusForbidden)
1226+
}
1227+
}
1228+
1229+
}
1230+
since_date = *tempDate
1231+
hasSinceDate = true
1232+
}
1233+
11951234
// Handle count
11961235
count := 20
11971236
if countStr := c.Query("count"); countStr != "" {
@@ -1230,32 +1269,38 @@ func mentions_timeline(c *fiber.Ctx) error {
12301269
}
12311270

12321271
// Create thread-safe maps for results
1233-
var userCache sync.Map
12341272
var postCache sync.Map
12351273

12361274
// Process in parallel
12371275
var wg sync.WaitGroup
12381276

12391277
// Fetch users in chunks
1240-
wg.Add(1)
1241-
go func() {
1242-
defer wg.Done()
1243-
users, err := blueskyapi.GetUsersInfo(*pds, *oauthToken, usersToLookUp, false)
1244-
if err == nil {
1245-
for _, user := range users {
1246-
userCache.Store(user.ScreenName[strings.LastIndex(user.ScreenName, "/")+1:], user)
1247-
}
1248-
}
1249-
}()
1278+
1279+
userChunks := chunkSlice(usersToLookUp, 25)
1280+
wg.Add(len(userChunks))
1281+
for _, chunk := range userChunks {
1282+
go func() {
1283+
defer wg.Done()
1284+
blueskyapi.GetUsersInfo(*pds, *oauthToken, chunk, false)
1285+
}()
1286+
}
12501287

12511288
// Fetch posts in parallel chunks
1289+
var wgPosts sync.WaitGroup
12521290
postChunks := chunkSlice(postsToLookUp, 10)
1291+
12531292
for _, chunk := range postChunks {
1254-
wg.Add(1)
1255-
go func(posts []string) {
1256-
defer wg.Done()
1257-
for _, postID := range posts {
1293+
wgPosts.Add(len(chunk))
1294+
for _, postID := range chunk {
1295+
go func(postID string) {
1296+
defer wgPosts.Done()
1297+
12581298
if post, err := blueskyapi.GetPost(*pds, *oauthToken, postID, 0, 1); err == nil {
1299+
if hasSinceDate {
1300+
if !post.Thread.Post.IndexedAt.After(since_date) {
1301+
return
1302+
}
1303+
}
12591304
tweet := TranslatePostToTweet(
12601305
post.Thread.Post,
12611306
func() string {
@@ -1288,8 +1333,10 @@ func mentions_timeline(c *fiber.Ctx) error {
12881333
)
12891334
postCache.Store(postID, &tweet)
12901335
}
1291-
}
1292-
}(chunk)
1336+
1337+
}(postID)
1338+
}
1339+
wgPosts.Wait()
12931340
}
12941341

12951342
wg.Wait()
@@ -1299,6 +1346,7 @@ func mentions_timeline(c *fiber.Ctx) error {
12991346
for _, notification := range bskyNotifications.Notifications {
13001347
if post, ok := postCache.Load(notification.URI); ok {
13011348
tweet := post.(*bridge.Tweet)
1349+
13021350
tweets = append(tweets, *tweet)
13031351
}
13041352
}

twitterv1/twitterv1.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,15 @@ func AddV1Path(function func(string, ...fiber.Handler) fiber.Router, url string,
224224
}
225225

226226
func GetUserSpecifiedInRequest(c *fiber.Ctx, no_value_default *string) (*string, error) {
227+
var ok bool
227228
actor := c.FormValue("user_id")
228229
if actor == "" {
229230
actor = c.FormValue("screen_name")
230231
if actor == "" {
231-
actor = c.Locals("handle").(string)
232+
actor, ok = c.Locals("handle").(string)
233+
if !ok {
234+
actor = ""
235+
}
232236
}
233237
if actor == "" {
234238
if no_value_default != nil {

0 commit comments

Comments
 (0)