Use QueryStateAfterEventsResponse

This commit is contained in:
Will Hunt 2021-01-04 15:01:14 +00:00
parent 6ca7fa8a4d
commit 9814e714ba

View file

@ -125,6 +125,8 @@ func (s *OutputRoomEventConsumer) filterRoomserverEvents(
// appserviceIsInterestedInEvent returns a boolean depending on whether a given // appserviceIsInterestedInEvent returns a boolean depending on whether a given
// event falls within one of a given application service's namespaces. // event falls within one of a given application service's namespaces.
//
// TODO: This should be cached, see https://github.com/matrix-org/dendrite/issues/1682
func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Context, event *gomatrixserverlib.HeaderedEvent, appservice config.ApplicationService) bool { func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Context, event *gomatrixserverlib.HeaderedEvent, appservice config.ApplicationService) bool {
// No reason to queue events if they'll never be sent to the application // No reason to queue events if they'll never be sent to the application
// service // service
@ -160,28 +162,23 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont
} }
// Check if any of the members in the room match the appservice // Check if any of the members in the room match the appservice
// TODO: appserviceIsInterestedInEvent should be wrapped in a cache membershipReq := api.QueryStateAfterEventsRequest{
// as this part can involve a lot of DB lookups PrevEventIDs: []string{event.EventID()},
membershipReq := api.QueryMembershipsForRoomRequest{
JoinedOnly: true,
RoomID: event.RoomID(), RoomID: event.RoomID(),
} }
var membershipRes api.QueryMembershipsForRoomResponse var membershipRes api.QueryStateAfterEventsResponse
// XXX: This should be membership at the time of the event, not current membership. I'm not sure // XXX: This could potentially race if the state for the event is not known yet
// how to extract this though, help?! // e.g. the event came over federation but we do not have the full state persisted.
// TODO: We also don't need to get all the members. Could we query the DB directly with a set of if err := s.rsAPI.QueryStateAfterEvents(ctx, &membershipReq, &membershipRes); err == nil {
// regex to limit the cost involved? for _, ev := range membershipRes.StateEvents {
log.WithFields(log.Fields{ if ev.Type() == gomatrixserverlib.MRoomMember {
"room_id": event.RoomID(), var membership, _ = ev.Membership()
"membership_count": len(membershipRes.JoinEvents), if membership == gomatrixserverlib.Join && appservice.IsInterestedInUserID(*ev.StateKey()) {
}).Infof("Got membership")
if err := s.rsAPI.QueryMembershipsForRoom(ctx, &membershipReq, &membershipRes); err == nil {
for _, ev := range membershipRes.JoinEvents {
if appservice.IsInterestedInUserID(*ev.StateKey) {
return true return true
} }
} }
}
} else { } else {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"room_id": event.RoomID(), "room_id": event.RoomID(),