added offset and limit

This commit is contained in:
qwqtoday 2024-07-29 20:01:59 +08:00
parent c1a461c1d5
commit 15aaac5983
2 changed files with 17 additions and 9 deletions

View file

@ -25,6 +25,7 @@ import (
"net/http"
"slices"
"sort"
"strconv"
"strings"
"time"
@ -158,8 +159,15 @@ func Enable(
}
roomID := vars["roomID"]
childrens, err := db.GetChildrensByRoomId(req.Context(), roomID, true)
limit, err := strconv.ParseUint(req.URL.Query().Get("limit"), 10, 64)
if err != nil {
limit = 50
}
offset, err := strconv.ParseUint(req.URL.Query().Get("from"), 10, 64)
if err != nil {
offset = 0
}
childrens, err := db.GetChildrensByRoomId(req.Context(), roomID, true, limit, offset)
if err != nil {
return util.ErrorResponse(err)
}
@ -179,7 +187,7 @@ func Enable(
Code: 200,
JSON: map[string]any{
"chunk": chunks,
"next_batch": "next_batch_token",
"next_batch": strconv.FormatUint(uint64(len(chunks))+offset, 10),
},
}
},

View file

@ -44,7 +44,7 @@ type Database interface {
// MarkChildrenExplored sets the 'explored' flag on this event to `true`.
MarkChildrenExplored(ctx context.Context, eventID string) error
GetChildrensByRoomId(ctx context.Context, roomID string, recentFirst bool) ([]eventInfo, error)
GetChildrensByRoomId(ctx context.Context, roomID string, recentFirst bool, limit uint64, offset uint64) ([]eventInfo, error)
}
type DB struct {
@ -150,10 +150,10 @@ func newPostgresDatabase(conMan *sqlutil.Connections, dbOpts *config.DatabaseOpt
LEFT JOIN msc2836_nodes ON msc2836_edges.child_event_id = msc2836_nodes.event_id
WHERE room_id = $1
ORDER BY origin_server_ts`
if d.selectChildrenByRoomIdForParentOldestFirstStmt, err = d.db.Prepare(selectChildrenByRoomIdQuery + "ASC"); err != nil {
if d.selectChildrenByRoomIdForParentOldestFirstStmt, err = d.db.Prepare(selectChildrenByRoomIdQuery + "ASC LIMIT $2 OFFSET $3"); err != nil {
return nil, err
}
if d.selectChildrenByRoomIdForParentRecentFirstStmt, err = d.db.Prepare(selectChildrenByRoomIdQuery + "DESC"); err != nil {
if d.selectChildrenByRoomIdForParentRecentFirstStmt, err = d.db.Prepare(selectChildrenByRoomIdQuery + "DESC LIMIT $2 OFFSET $3"); err != nil {
return nil, err
}
@ -342,13 +342,13 @@ func (p *DB) ParentForChild(ctx context.Context, eventID, relType string) (*even
return &ei, nil
}
func (p *DB) GetChildrensByRoomId(ctx context.Context, roomID string, recentFirst bool) ([]eventInfo, error) {
func (p *DB) GetChildrensByRoomId(ctx context.Context, roomID string, recentFirst bool, limit uint64, offset uint64) ([]eventInfo, error) {
var rows *sql.Rows
var err error
if recentFirst {
rows, err = p.selectChildrenForParentRecentFirstStmt.QueryContext(ctx, roomID)
rows, err = p.selectChildrenForParentRecentFirstStmt.QueryContext(ctx, roomID, limit, offset)
} else {
rows, err = p.selectChildrenByRoomIdForParentOldestFirstStmt.QueryContext(ctx, roomID)
rows, err = p.selectChildrenByRoomIdForParentOldestFirstStmt.QueryContext(ctx, roomID, limit, offset)
}
if err != nil {
return nil, err