mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-06 22:43:10 -06:00
added offset and limit
This commit is contained in:
parent
c1a461c1d5
commit
15aaac5983
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -158,8 +159,15 @@ func Enable(
|
||||||
}
|
}
|
||||||
|
|
||||||
roomID := vars["roomID"]
|
roomID := vars["roomID"]
|
||||||
|
limit, err := strconv.ParseUint(req.URL.Query().Get("limit"), 10, 64)
|
||||||
childrens, err := db.GetChildrensByRoomId(req.Context(), roomID, true)
|
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 {
|
if err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
@ -179,7 +187,7 @@ func Enable(
|
||||||
Code: 200,
|
Code: 200,
|
||||||
JSON: map[string]any{
|
JSON: map[string]any{
|
||||||
"chunk": chunks,
|
"chunk": chunks,
|
||||||
"next_batch": "next_batch_token",
|
"next_batch": strconv.FormatUint(uint64(len(chunks))+offset, 10),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ type Database interface {
|
||||||
// MarkChildrenExplored sets the 'explored' flag on this event to `true`.
|
// MarkChildrenExplored sets the 'explored' flag on this event to `true`.
|
||||||
MarkChildrenExplored(ctx context.Context, eventID string) error
|
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 {
|
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
|
LEFT JOIN msc2836_nodes ON msc2836_edges.child_event_id = msc2836_nodes.event_id
|
||||||
WHERE room_id = $1
|
WHERE room_id = $1
|
||||||
ORDER BY origin_server_ts`
|
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
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -342,13 +342,13 @@ func (p *DB) ParentForChild(ctx context.Context, eventID, relType string) (*even
|
||||||
return &ei, nil
|
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 rows *sql.Rows
|
||||||
var err error
|
var err error
|
||||||
if recentFirst {
|
if recentFirst {
|
||||||
rows, err = p.selectChildrenForParentRecentFirstStmt.QueryContext(ctx, roomID)
|
rows, err = p.selectChildrenForParentRecentFirstStmt.QueryContext(ctx, roomID, limit, offset)
|
||||||
} else {
|
} else {
|
||||||
rows, err = p.selectChildrenByRoomIdForParentOldestFirstStmt.QueryContext(ctx, roomID)
|
rows, err = p.selectChildrenByRoomIdForParentOldestFirstStmt.QueryContext(ctx, roomID, limit, offset)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue