this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix listrepos cursors (#722)

authored by

Jaz and committed by
GitHub
f07f35dd 63d34ffb

+40 -38
+20 -25
bgs/handlers.go
··· 8 8 "io" 9 9 "net/http" 10 10 "net/url" 11 - "strconv" 12 11 "strings" 13 12 14 13 atproto "github.com/bluesky-social/indigo/api/atproto" ··· 194 193 return nil 195 194 } 196 195 197 - func (s *BGS) handleComAtprotoSyncListRepos(ctx context.Context, cursor string, limit int) (*comatprototypes.SyncListRepos_Output, error) { 198 - // Use UIDs for the cursor 199 - var err error 200 - c := int64(0) 201 - if cursor != "" { 202 - c, err = strconv.ParseInt(cursor, 10, 64) 203 - if err != nil { 204 - return nil, echo.NewHTTPError(http.StatusBadRequest, "couldn't parse your cursor as an integer") 205 - } 206 - } 207 - 208 - resp := &comatprototypes.SyncListRepos_Output{ 209 - Repos: []*comatprototypes.SyncListRepos_Repo{}, 210 - } 211 - 212 - users := []User{} 213 - 196 + func (s *BGS) handleComAtprotoSyncListRepos(ctx context.Context, cursor int64, limit int) (*comatprototypes.SyncListRepos_Output, error) { 197 + // Filter out tombstoned, taken down, and deactivated accounts 214 198 q := fmt.Sprintf("id > ? AND NOT tombstoned AND NOT taken_down AND upstream_status != '%s' AND upstream_status != '%s' AND upstream_status != '%s'", 215 199 events.AccountStatusDeactivated, events.AccountStatusSuspended, events.AccountStatusTakendown) 216 200 217 - if err := s.db.Model(&User{}).Where(q, c).Order("id").Limit(limit).Find(&users).Error; err != nil { 201 + // Load the users 202 + users := []*User{} 203 + if err := s.db.Model(&User{}).Where(q, cursor).Order("id").Limit(limit).Find(&users).Error; err != nil { 218 204 if err == gorm.ErrRecordNotFound { 219 205 return &comatprototypes.SyncListRepos_Output{}, nil 220 206 } ··· 224 210 225 211 if len(users) == 0 { 226 212 // resp.Repos is an explicit empty array, not just 'nil' 227 - return resp, nil 213 + return &comatprototypes.SyncListRepos_Output{ 214 + Repos: []*comatprototypes.SyncListRepos_Repo{}, 215 + }, nil 216 + } 217 + 218 + resp := &comatprototypes.SyncListRepos_Output{ 219 + Repos: make([]*comatprototypes.SyncListRepos_Repo, len(users)), 228 220 } 229 221 222 + // Fetch the repo roots for each user 230 223 for i := range users { 231 224 user := users[i] 232 225 ··· 236 229 return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get repo root for (%s): %v", user.Did, err.Error())) 237 230 } 238 231 239 - resp.Repos = append(resp.Repos, &comatprototypes.SyncListRepos_Repo{ 232 + resp.Repos[i] = &comatprototypes.SyncListRepos_Repo{ 240 233 Did: user.Did, 241 234 Head: root.String(), 242 - }) 235 + } 243 236 } 244 237 245 - c += int64(len(users)) 246 - cursor = strconv.FormatInt(c, 10) 247 - resp.Cursor = &cursor 238 + // If this is not the last page, set the cursor 239 + if len(users) < limit && len(users) > 1 { 240 + nextCursor := fmt.Sprintf("%d", users[len(users)-1].ID) 241 + resp.Cursor = &nextCursor 242 + } 248 243 249 244 return resp, nil 250 245 }
+20 -13
bgs/stubs.go
··· 136 136 func (s *BGS) HandleComAtprotoSyncListRepos(c echo.Context) error { 137 137 ctx, span := otel.Tracer("server").Start(c.Request().Context(), "HandleComAtprotoSyncListRepos") 138 138 defer span.End() 139 - cursor := c.QueryParam("cursor") 139 + 140 + cursorQuery := c.QueryParam("cursor") 141 + limitQuery := c.QueryParam("limit") 142 + 143 + var err error 144 + 145 + limit := 500 146 + if limitQuery != "" { 147 + limit, err = strconv.Atoi(limitQuery) 148 + if err != nil || limit < 1 || limit > 1000 { 149 + return c.JSON(http.StatusBadRequest, XRPCError{Message: fmt.Sprintf("invalid limit: %s", limitQuery)}) 150 + } 151 + } 140 152 141 - var limit int 142 - if p := c.QueryParam("limit"); p != "" { 143 - var err error 144 - limit, err = strconv.Atoi(p) 145 - if err != nil { 146 - return c.JSON(http.StatusBadRequest, XRPCError{Message: fmt.Sprintf("invalid limit: %s", p)}) 153 + cursor := int64(0) 154 + if cursorQuery != "" { 155 + cursor, err = strconv.ParseInt(cursorQuery, 10, 64) 156 + if err != nil || cursor < 0 { 157 + return c.JSON(http.StatusBadRequest, XRPCError{Message: fmt.Sprintf("invalid cursor: %s", cursorQuery)}) 147 158 } 148 - } else { 149 - limit = 500 150 159 } 151 - var out *comatprototypes.SyncListRepos_Output 152 - var handleErr error 153 - // func (s *BGS) handleComAtprotoSyncListRepos(ctx context.Context,cursor string,limit int) (*comatprototypes.SyncListRepos_Output, error) 154 - out, handleErr = s.handleComAtprotoSyncListRepos(ctx, cursor, limit) 160 + 161 + out, handleErr := s.handleComAtprotoSyncListRepos(ctx, cursor, limit) 155 162 if handleErr != nil { 156 163 return handleErr 157 164 }