Fix bug in finding the requested state event

This commit is contained in:
Neil Alexander 2020-07-27 12:14:11 +01:00
parent 1aa2c2391e
commit 253492dc15
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -113,16 +113,16 @@ func OnIncomingStateTypeRequest(
// the latest events or the last event from when the user was joined. // the latest events or the last event from when the user was joined.
// Then include the requested event type and state key, assuming it // Then include the requested event type and state key, assuming it
// isn't for the same. // isn't for the same.
toFetch := []gomatrixserverlib.StateKeyTuple{ stateToFetch := []gomatrixserverlib.StateKeyTuple{
{ {
EventType: gomatrixserverlib.MRoomHistoryVisibility, EventType: evType,
StateKey: "", StateKey: stateKey,
}, },
} }
if evType != gomatrixserverlib.MRoomHistoryVisibility && stateKey != "" { if evType != gomatrixserverlib.MRoomHistoryVisibility && stateKey != "" {
toFetch = append(toFetch, gomatrixserverlib.StateKeyTuple{ stateToFetch = append(stateToFetch, gomatrixserverlib.StateKeyTuple{
EventType: evType, EventType: gomatrixserverlib.MRoomHistoryVisibility,
StateKey: stateKey, StateKey: "",
}) })
} }
@ -132,7 +132,7 @@ func OnIncomingStateTypeRequest(
stateRes := api.QueryLatestEventsAndStateResponse{} stateRes := api.QueryLatestEventsAndStateResponse{}
if err := rsAPI.QueryLatestEventsAndState(ctx, &api.QueryLatestEventsAndStateRequest{ if err := rsAPI.QueryLatestEventsAndState(ctx, &api.QueryLatestEventsAndStateRequest{
RoomID: roomID, RoomID: roomID,
StateToFetch: toFetch, StateToFetch: stateToFetch,
}, &stateRes); err != nil { }, &stateRes); err != nil {
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed") util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
@ -141,7 +141,7 @@ func OnIncomingStateTypeRequest(
// Look at the room state and see if we have a history visibility event // Look at the room state and see if we have a history visibility event
// that marks the room as world-readable. If we don't then we assume that // that marks the room as world-readable. If we don't then we assume that
// the room is not world-readable. // the room is not world-readable.
for i, ev := range stateRes.StateEvents { for _, ev := range stateRes.StateEvents {
if ev.Type() == gomatrixserverlib.MRoomHistoryVisibility { if ev.Type() == gomatrixserverlib.MRoomHistoryVisibility {
content := map[string]string{} content := map[string]string{}
if err := json.Unmarshal(ev.Content(), &content); err != nil { if err := json.Unmarshal(ev.Content(), &content); err != nil {
@ -150,12 +150,6 @@ func OnIncomingStateTypeRequest(
} }
if visibility, ok := content["history_visibility"]; ok { if visibility, ok := content["history_visibility"]; ok {
worldReadable = visibility == "world_readable" worldReadable = visibility == "world_readable"
// If the request is for the history visibility of the room
// specifically then keep it in the response, otherwise discard
// it so that the later checks make sense.
if evType != gomatrixserverlib.MRoomHistoryVisibility && stateKey != "" {
stateRes.StateEvents = append(stateRes.StateEvents[:i], stateRes.StateEvents[i+1:]...)
}
break break
} }
} }
@ -209,9 +203,13 @@ func OnIncomingStateTypeRequest(
if wantLatestState { if wantLatestState {
// If we are happy to use the latest state, either because the user is // If we are happy to use the latest state, either because the user is
// still in the room, or because the room is world-readable, then just // still in the room, or because the room is world-readable, then just
// use the result of the previous QueryLatestEventsAndState response. // use the result of the previous QueryLatestEventsAndState response
if len(stateRes.StateEvents) > 0 { // to find the state event, if provided.
event = &stateRes.StateEvents[0] for _, ev := range stateRes.StateEvents {
if ev.Type() == evType && ev.StateKeyEquals(stateKey) {
event = &ev
break
}
} }
} else { } else {
// Otherwise, take the event ID of their leave event and work out what // Otherwise, take the event ID of their leave event and work out what