mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
More tweaks around getting events
This commit is contained in:
parent
e47ad14a18
commit
9a27511b24
|
|
@ -60,21 +60,13 @@ func allowedToSeeEvent(
|
||||||
rsAPI api.FederationRoomserverAPI,
|
rsAPI api.FederationRoomserverAPI,
|
||||||
eventID string,
|
eventID string,
|
||||||
) *util.JSONResponse {
|
) *util.JSONResponse {
|
||||||
var authResponse api.QueryServerAllowedToSeeEventResponse
|
allowed, err := rsAPI.QueryServerAllowedToSeeEvent(ctx, origin, eventID)
|
||||||
err := rsAPI.QueryServerAllowedToSeeEvent(
|
|
||||||
ctx,
|
|
||||||
&api.QueryServerAllowedToSeeEventRequest{
|
|
||||||
EventID: eventID,
|
|
||||||
ServerName: origin,
|
|
||||||
},
|
|
||||||
&authResponse,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resErr := util.ErrorResponse(err)
|
resErr := util.ErrorResponse(err)
|
||||||
return &resErr
|
return &resErr
|
||||||
}
|
}
|
||||||
|
|
||||||
if !authResponse.AllowedToSeeEvent {
|
if !allowed {
|
||||||
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
||||||
return &resErr
|
return &resErr
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,7 @@ type FederationRoomserverAPI interface {
|
||||||
// Query missing events for a room from roomserver
|
// Query missing events for a room from roomserver
|
||||||
QueryMissingEvents(ctx context.Context, req *QueryMissingEventsRequest, res *QueryMissingEventsResponse) error
|
QueryMissingEvents(ctx context.Context, req *QueryMissingEventsRequest, res *QueryMissingEventsResponse) error
|
||||||
// Query whether a server is allowed to see an event
|
// Query whether a server is allowed to see an event
|
||||||
QueryServerAllowedToSeeEvent(ctx context.Context, req *QueryServerAllowedToSeeEventRequest, res *QueryServerAllowedToSeeEventResponse) error
|
QueryServerAllowedToSeeEvent(ctx context.Context, serverName gomatrixserverlib.ServerName, eventID string) (allowed bool, err error)
|
||||||
QueryRoomsForUser(ctx context.Context, req *QueryRoomsForUserRequest, res *QueryRoomsForUserResponse) error
|
QueryRoomsForUser(ctx context.Context, req *QueryRoomsForUserRequest, res *QueryRoomsForUserResponse) error
|
||||||
QueryRestrictedJoinAllowed(ctx context.Context, req *QueryRestrictedJoinAllowedRequest, res *QueryRestrictedJoinAllowedResponse) error
|
QueryRestrictedJoinAllowed(ctx context.Context, req *QueryRestrictedJoinAllowedRequest, res *QueryRestrictedJoinAllowedResponse) error
|
||||||
PerformInboundPeek(ctx context.Context, req *PerformInboundPeekRequest, res *PerformInboundPeekResponse) error
|
PerformInboundPeek(ctx context.Context, req *PerformInboundPeekRequest, res *PerformInboundPeekResponse) error
|
||||||
|
|
|
||||||
|
|
@ -631,6 +631,7 @@ func persistEvents(ctx context.Context, db storage.Database, events []*gomatrixs
|
||||||
|
|
||||||
_, redactedEvent, err := db.MaybeRedactEvent(ctx, roomInfo, eventNID, ev.Unwrap(), true)
|
_, redactedEvent, err := db.MaybeRedactEvent(ctx, roomInfo, eventNID, ev.Unwrap(), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("Failed to redact event")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// If storing this event results in it being redacted, then do so.
|
// If storing this event results in it being redacted, then do so.
|
||||||
|
|
|
||||||
|
|
@ -433,39 +433,39 @@ func (r *Queryer) QueryServerJoinedToRoom(
|
||||||
// QueryServerAllowedToSeeEvent implements api.RoomserverInternalAPI
|
// QueryServerAllowedToSeeEvent implements api.RoomserverInternalAPI
|
||||||
func (r *Queryer) QueryServerAllowedToSeeEvent(
|
func (r *Queryer) QueryServerAllowedToSeeEvent(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.QueryServerAllowedToSeeEventRequest,
|
serverName gomatrixserverlib.ServerName,
|
||||||
response *api.QueryServerAllowedToSeeEventResponse,
|
eventID string,
|
||||||
) (err error) {
|
) (allowed bool, err error) {
|
||||||
events, err := r.DB.EventsFromIDs(ctx, nil, []string{request.EventID})
|
events, err := r.DB.EventNIDs(ctx, []string{eventID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(events) == 0 {
|
if len(events) == 0 {
|
||||||
response.AllowedToSeeEvent = false // event doesn't exist so not allowed to see
|
return allowed, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
roomID := events[0].RoomID()
|
info, err := r.DB.RoomInfoByNID(ctx, events[eventID].RoomNID)
|
||||||
|
|
||||||
inRoomReq := &api.QueryServerJoinedToRoomRequest{
|
|
||||||
RoomID: roomID,
|
|
||||||
ServerName: request.ServerName,
|
|
||||||
}
|
|
||||||
inRoomRes := &api.QueryServerJoinedToRoomResponse{}
|
|
||||||
if err = r.QueryServerJoinedToRoom(ctx, inRoomReq, inRoomRes); err != nil {
|
|
||||||
return fmt.Errorf("r.Queryer.QueryServerJoinedToRoom: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
info, err := r.DB.RoomInfo(ctx, roomID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return allowed, err
|
||||||
}
|
}
|
||||||
if info == nil || info.IsStub() {
|
if info == nil || info.IsStub() {
|
||||||
return nil
|
return allowed, nil
|
||||||
}
|
}
|
||||||
response.AllowedToSeeEvent, err = helpers.CheckServerAllowedToSeeEvent(
|
var isInRoom bool
|
||||||
ctx, r.DB, info, request.EventID, request.ServerName, inRoomRes.IsInRoom,
|
if r.IsLocalServerName(serverName) || serverName == "" {
|
||||||
|
isInRoom, err = r.DB.GetLocalServerInRoom(ctx, info.RoomNID)
|
||||||
|
if err != nil {
|
||||||
|
return allowed, fmt.Errorf("r.DB.GetLocalServerInRoom: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isInRoom, err = r.DB.GetServerInRoom(ctx, info.RoomNID, serverName)
|
||||||
|
if err != nil {
|
||||||
|
return allowed, fmt.Errorf("r.DB.GetServerInRoom: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return helpers.CheckServerAllowedToSeeEvent(
|
||||||
|
ctx, r.DB, info, eventID, serverName, isInRoom,
|
||||||
)
|
)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryMissingEvents implements api.RoomserverInternalAPI
|
// QueryMissingEvents implements api.RoomserverInternalAPI
|
||||||
|
|
@ -487,19 +487,22 @@ func (r *Queryer) QueryMissingEvents(
|
||||||
eventsToFilter[id] = true
|
eventsToFilter[id] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
events, err := r.DB.EventsFromIDs(ctx, nil, front)
|
if len(front) == 0 {
|
||||||
|
return nil // no events to query, give up.
|
||||||
|
}
|
||||||
|
events, err := r.DB.EventNIDs(ctx, []string{front[0]})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(events) == 0 {
|
if len(events) == 0 {
|
||||||
return nil // we are missing the events being asked to search from, give up.
|
return nil // we are missing the events being asked to search from, give up.
|
||||||
}
|
}
|
||||||
info, err := r.DB.RoomInfo(ctx, events[0].RoomID())
|
info, err := r.DB.RoomInfoByNID(ctx, events[front[0]].RoomNID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if info == nil || info.IsStub() {
|
if info == nil || info.IsStub() {
|
||||||
return fmt.Errorf("missing RoomInfo for room %s", events[0].RoomID())
|
return fmt.Errorf("missing RoomInfo for room %d", events[front[0]].RoomNID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resultNIDs, redactEventIDs, err := helpers.ScanEventTree(ctx, r.DB, info, front, visited, request.Limit, request.ServerName)
|
resultNIDs, redactEventIDs, err := helpers.ScanEventTree(ctx, r.DB, info, front, visited, request.Limit, request.ServerName)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/version"
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
@ -553,9 +552,8 @@ func (d *EventDatabase) Events(ctx context.Context, roomInfo *types.RoomInfo, ev
|
||||||
func (d *EventDatabase) events(
|
func (d *EventDatabase) events(
|
||||||
ctx context.Context, txn *sql.Tx, roomInfo *types.RoomInfo, inputEventNIDs types.EventNIDs,
|
ctx context.Context, txn *sql.Tx, roomInfo *types.RoomInfo, inputEventNIDs types.EventNIDs,
|
||||||
) ([]types.Event, error) {
|
) ([]types.Event, error) {
|
||||||
if roomInfo == nil {
|
if roomInfo == nil { // this should never happen
|
||||||
|
return nil, fmt.Errorf("unable to parse events without roomInfo")
|
||||||
roomInfo = &types.RoomInfo{RoomVersion: version.DefaultRoomVersion()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(inputEventNIDs)
|
sort.Sort(inputEventNIDs)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue