mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -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/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"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/storage"
|
||||||
"github.com/matrix-org/dendrite/syncapi/types"
|
"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 {
|
type RelationsResponse struct {
|
||||||
|
|
@ -36,7 +38,12 @@ type RelationsResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint:gocyclo
|
// 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 err error
|
||||||
var from, to types.StreamPosition
|
var from, to types.StreamPosition
|
||||||
var limit int
|
var limit int
|
||||||
|
|
@ -77,14 +84,38 @@ func Relations(req *http.Request, device *api.Device, syncDB storage.Database, r
|
||||||
var succeeded bool
|
var succeeded bool
|
||||||
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
||||||
|
|
||||||
res := &RelationsResponse{}
|
res := &RelationsResponse{
|
||||||
res.Chunk, res.PrevBatch, res.NextBatch, err = snapshot.RelationsFor(
|
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,
|
req.Context(), roomID, eventID, relType, eventType, from, to, dir == "b", limit,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.ErrorResponse(err)
|
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
|
succeeded = true
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusOK,
|
Code: http.StatusOK,
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,7 @@ func Setup(
|
||||||
}
|
}
|
||||||
|
|
||||||
return Relations(
|
return Relations(
|
||||||
req, device, syncDB,
|
req, device, syncDB, rsAPI,
|
||||||
vars["roomId"], vars["eventId"], "", "",
|
vars["roomId"], vars["eventId"], "", "",
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
|
@ -133,7 +133,7 @@ func Setup(
|
||||||
}
|
}
|
||||||
|
|
||||||
return Relations(
|
return Relations(
|
||||||
req, device, syncDB,
|
req, device, syncDB, rsAPI,
|
||||||
vars["roomId"], vars["eventId"], vars["relType"], "",
|
vars["roomId"], vars["eventId"], vars["relType"], "",
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
|
@ -147,7 +147,7 @@ func Setup(
|
||||||
}
|
}
|
||||||
|
|
||||||
return Relations(
|
return Relations(
|
||||||
req, device, syncDB,
|
req, device, syncDB, rsAPI,
|
||||||
vars["roomId"], vars["eventId"], vars["relType"], vars["eventType"],
|
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)
|
GetUserUnreadNotificationCountsForRooms(ctx context.Context, userID string, roomIDs map[string]string) (map[string]*eventutil.NotificationData, error)
|
||||||
GetPresence(ctx context.Context, userID string) (*types.PresenceInternal, 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)
|
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 {
|
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) (
|
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{
|
r := types.Range{
|
||||||
From: from,
|
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 there were no entries returned, there were no relations, so stop at this point.
|
||||||
if len(entries) == 0 {
|
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
|
// 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 {
|
for _, entry := range entries {
|
||||||
eventIDs = append(eventIDs, entry.EventID)
|
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 {
|
if err != nil {
|
||||||
return nil, "", "", fmt.Errorf("d.OutputEvents.SelectEvents: %w", err)
|
return nil, "", "", fmt.Errorf("d.OutputEvents.SelectEvents: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the events into client events, and optionally filter based on the event
|
return events, prevBatch, nextBatch, nil
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue