Unverified Commit df5893b3 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Make haystack cursors consistent with listing (#994)

parent 235bed3b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Consistent validation of override operator in runtime leaderboard record writes.
- Correctly filter open/closed groups in the listing API.
- Ensure direct message channel message listing is correctly scoped to participants only.
- Make next and previous cursor of leaderboard and tournament records around owner operations consistent with record listing.
- Make next and previous cursor of leaderboard and tournament records haystack operations consistent with record listing.

## [3.15.0] - 2023-01-04
### Added
+32 −32
Original line number Diff line number Diff line
@@ -758,11 +758,6 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
			ownerRecord.ExpiryTime = &timestamppb.Timestamp{Seconds: expiryTime}
		}

		if limit == 1 {
			ownerRecord.Rank = rankCache.Get(leaderboardId, expiryTime.Unix(), ownerID)
			return &api.LeaderboardRecordList{Records: []*api.LeaderboardRecord{ownerRecord}}, nil
		}

		query := `SELECT leaderboard_id, owner_id, username, score, subscore, num_score, max_num_score, metadata, create_time, update_time, expiry_time
	FROM leaderboard_record
	WHERE leaderboard_id = $1
@@ -793,10 +788,10 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
			return nil, err
		}

		setNextCursor := false
		setPrevCursor := false
		if len(firstRecords) > limit {
			// Check if there might be a next cursor
			setNextCursor = true
			setPrevCursor = true
			firstRecords = firstRecords[:len(firstRecords)-1]
		}

@@ -832,10 +827,10 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
			return nil, err
		}

		setPrevCursor := false
		setNextCursor := false
		if len(secondRecords) > secondLimit {
			// Check if there might be a prev cursor
			setPrevCursor = true
			setNextCursor = true
			secondRecords = secondRecords[:len(secondRecords)-1]
		}

@@ -844,7 +839,7 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *

		numRecords := len(records)
		start := numRecords - limit
		if start < 0 || len(firstRecords) < limit/2 {
		if start < 0 || len(firstRecords) < secondLimit {
			start = 0
		}
		end := start + limit
@@ -852,45 +847,50 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
			end = numRecords
		}

		if start > 0 {
			// There was a previous result that was discarded, the prev_cursor should be set.
			setPrevCursor = true
		}

		records = records[start:end]
		rankCache.Fill(leaderboardId, expiryTime.Unix(), records)

		var nextCursorStr string
		if setNextCursor {
			firstRecord := records[0]
		var prevCursorStr string
		if setPrevCursor {
			record := records[0]

			nextCursor := &leaderboardRecordListCursor{
			prevCursor := &leaderboardRecordListCursor{
				IsNext:        false,
				LeaderboardId: firstRecord.LeaderboardId,
				LeaderboardId: record.LeaderboardId,
				ExpiryTime:    expiryTime.Unix(),
				Score:         firstRecord.Score,
				Subscore:      firstRecord.Subscore,
				OwnerId:       firstRecord.OwnerId,
				Rank:          firstRecord.Rank,
				Score:         record.Score,
				Subscore:      record.Subscore,
				OwnerId:       record.OwnerId,
				Rank:          record.Rank,
			}
			nextCursorStr, err = marshalLeaderboardRecordsListCursor(nextCursor)
			prevCursorStr, err = marshalLeaderboardRecordsListCursor(prevCursor)
			if err != nil {
				logger.Error("Error creating leaderboard records list next cursor", zap.Error(err))
				logger.Error("Error creating leaderboard records list previous cursor", zap.Error(err))
				return nil, err
			}
		}

		var prevCursorStr string
		if setPrevCursor {
			lastRecord := records[len(records)-1]
		var nextCursorStr string
		if setNextCursor {
			record := records[len(records)-1]

			prevCursor := &leaderboardRecordListCursor{
			nextCursor := &leaderboardRecordListCursor{
				IsNext:        true,
				LeaderboardId: lastRecord.LeaderboardId,
				LeaderboardId: record.LeaderboardId,
				ExpiryTime:    expiryTime.Unix(),
				Score:         lastRecord.Score,
				Subscore:      lastRecord.Subscore,
				OwnerId:       lastRecord.OwnerId,
				Rank:          lastRecord.Rank,
				Score:         record.Score,
				Subscore:      record.Subscore,
				OwnerId:       record.OwnerId,
				Rank:          record.Rank,
			}
			prevCursorStr, err = marshalLeaderboardRecordsListCursor(prevCursor)
			nextCursorStr, err = marshalLeaderboardRecordsListCursor(nextCursor)
			if err != nil {
				logger.Error("Error creating leaderboard records list previous cursor", zap.Error(err))
				logger.Error("Error creating leaderboard records list next cursor", zap.Error(err))
				return nil, err
			}
		}