mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Apply history visibility
This commit is contained in:
parent
3a3b252b31
commit
edc625f215
|
|
@ -24,9 +24,11 @@ import (
|
|||
|
||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/dendrite/syncapi/internal"
|
||||
"github.com/matrix-org/dendrite/syncapi/storage"
|
||||
"github.com/matrix-org/dendrite/syncapi/types"
|
||||
"github.com/matrix-org/dendrite/userapi/api"
|
||||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||
)
|
||||
|
||||
type RelationsResponse struct {
|
||||
|
|
@ -36,7 +38,12 @@ type RelationsResponse struct {
|
|||
}
|
||||
|
||||
// nolint:gocyclo
|
||||
func Relations(req *http.Request, device *api.Device, syncDB storage.Database, roomID, eventID, relType, eventType string) util.JSONResponse {
|
||||
func Relations(
|
||||
req *http.Request, device *userapi.Device,
|
||||
syncDB storage.Database,
|
||||
rsAPI api.SyncRoomserverAPI,
|
||||
roomID, eventID, relType, eventType string,
|
||||
) util.JSONResponse {
|
||||
var err error
|
||||
var from, to types.StreamPosition
|
||||
var limit int
|
||||
|
|
@ -77,14 +84,38 @@ func Relations(req *http.Request, device *api.Device, syncDB storage.Database, r
|
|||
var succeeded bool
|
||||
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
||||
|
||||
res := &RelationsResponse{}
|
||||
res.Chunk, res.PrevBatch, res.NextBatch, err = snapshot.RelationsFor(
|
||||
res := &RelationsResponse{
|
||||
Chunk: []gomatrixserverlib.ClientEvent{},
|
||||
}
|
||||
var events []types.StreamEvent
|
||||
events, res.PrevBatch, res.NextBatch, err = snapshot.RelationsFor(
|
||||
req.Context(), roomID, eventID, relType, eventType, from, to, dir == "b", limit,
|
||||
)
|
||||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
|
||||
headeredEvents := make([]*gomatrixserverlib.HeaderedEvent, 0, len(events))
|
||||
for _, event := range events {
|
||||
headeredEvents = append(headeredEvents, event.HeaderedEvent)
|
||||
}
|
||||
|
||||
// Apply history visibility to the result events.
|
||||
filteredEvents, err := internal.ApplyHistoryVisibilityFilter(req.Context(), snapshot, rsAPI, headeredEvents, nil, device.UserID, "relations")
|
||||
if err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
|
||||
// Convert the events into client events, and optionally filter based on the event
|
||||
// type if it was specified.
|
||||
res.Chunk = make([]gomatrixserverlib.ClientEvent, 0, len(filteredEvents))
|
||||
for _, event := range filteredEvents {
|
||||
res.Chunk = append(
|
||||
res.Chunk,
|
||||
gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll),
|
||||
)
|
||||
}
|
||||
|
||||
succeeded = true
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ func Setup(
|
|||
}
|
||||
|
||||
return Relations(
|
||||
req, device, syncDB,
|
||||
req, device, syncDB, rsAPI,
|
||||
vars["roomId"], vars["eventId"], "", "",
|
||||
)
|
||||
}),
|
||||
|
|
@ -133,7 +133,7 @@ func Setup(
|
|||
}
|
||||
|
||||
return Relations(
|
||||
req, device, syncDB,
|
||||
req, device, syncDB, rsAPI,
|
||||
vars["roomId"], vars["eventId"], vars["relType"], "",
|
||||
)
|
||||
}),
|
||||
|
|
@ -147,7 +147,7 @@ func Setup(
|
|||
}
|
||||
|
||||
return Relations(
|
||||
req, device, syncDB,
|
||||
req, device, syncDB, rsAPI,
|
||||
vars["roomId"], vars["eventId"], vars["relType"], vars["eventType"],
|
||||
)
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ type DatabaseTransaction interface {
|
|||
GetUserUnreadNotificationCountsForRooms(ctx context.Context, userID string, roomIDs map[string]string) (map[string]*eventutil.NotificationData, error)
|
||||
GetPresence(ctx context.Context, userID string) (*types.PresenceInternal, error)
|
||||
PresenceAfter(ctx context.Context, after types.StreamPosition, filter gomatrixserverlib.EventFilter) (map[string]*types.PresenceInternal, error)
|
||||
RelationsFor(ctx context.Context, roomID, eventID, relType, eventType string, from, to types.StreamPosition, backwards bool, limit int) (clientEvents []gomatrixserverlib.ClientEvent, prevBatch, nextBatch string, err error)
|
||||
RelationsFor(ctx context.Context, roomID, eventID, relType, eventType string, from, to types.StreamPosition, backwards bool, limit int) (events []types.StreamEvent, prevBatch, nextBatch string, err error)
|
||||
}
|
||||
|
||||
type Database interface {
|
||||
|
|
|
|||
|
|
@ -596,7 +596,7 @@ func (d *DatabaseTransaction) MaxStreamPositionForRelations(ctx context.Context)
|
|||
}
|
||||
|
||||
func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID, relType, eventType string, from, to types.StreamPosition, backwards bool, limit int) (
|
||||
clientEvents []gomatrixserverlib.ClientEvent, prevBatch, nextBatch string, err error,
|
||||
events []types.StreamEvent, prevBatch, nextBatch string, err error,
|
||||
) {
|
||||
r := types.Range{
|
||||
From: from,
|
||||
|
|
@ -644,7 +644,7 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
|
|||
|
||||
// If there were no entries returned, there were no relations, so stop at this point.
|
||||
if len(entries) == 0 {
|
||||
return []gomatrixserverlib.ClientEvent{}, "", "", nil
|
||||
return nil, "", "", nil
|
||||
}
|
||||
|
||||
// Otherwise, let's try and work out what sensible prev_batch and next_batch values
|
||||
|
|
@ -663,20 +663,10 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
|
|||
for _, entry := range entries {
|
||||
eventIDs = append(eventIDs, entry.EventID)
|
||||
}
|
||||
events, err := d.OutputEvents.SelectEvents(ctx, d.txn, eventIDs, nil, true)
|
||||
events, err = d.OutputEvents.SelectEvents(ctx, d.txn, eventIDs, nil, true)
|
||||
if err != nil {
|
||||
return nil, "", "", fmt.Errorf("d.OutputEvents.SelectEvents: %w", err)
|
||||
}
|
||||
|
||||
// Convert the events into client events, and optionally filter based on the event
|
||||
// type if it was specified.
|
||||
clientEvents = make([]gomatrixserverlib.ClientEvent, 0, len(events))
|
||||
for _, event := range events {
|
||||
clientEvents = append(
|
||||
clientEvents,
|
||||
gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll),
|
||||
)
|
||||
}
|
||||
|
||||
return clientEvents, prevBatch, nextBatch, nil
|
||||
return events, prevBatch, nextBatch, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue