Fix POST /get_missing_events/{roomId} #622 (#678)

* Fix #622 /get_missing_events/{roomId}

Signed-off-by: Alex Chen <minecnly@gmail.com>

* Set initial size for eventsToFilter in /get_missing_events/{roomId}

Signed-off-by: Alex Chen <minecnly@gmail.com>
main
Alex Chen 2019-01-14 18:20:19 +08:00 committed by Brendan Abolivier
parent 088a939461
commit e063433b74
2 changed files with 16 additions and 3 deletions

View File

@ -212,13 +212,13 @@ func Setup(
}, },
)).Methods(http.MethodGet) )).Methods(http.MethodGet)
v1fedmux.Handle("get_missing_events/{roomID}", common.MakeFedAPI( v1fedmux.Handle("/get_missing_events/{roomID}", common.MakeFedAPI(
"federation_get_missing_events", cfg.Matrix.ServerName, keys, "federation_get_missing_events", cfg.Matrix.ServerName, keys,
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
vars := mux.Vars(httpReq) vars := mux.Vars(httpReq)
return GetMissingEvents(httpReq, request, query, vars["roomID"]) return GetMissingEvents(httpReq, request, query, vars["roomID"])
}, },
)).Methods(http.MethodGet) )).Methods(http.MethodPost)
v1fedmux.Handle("/backfill/{roomID}/", common.MakeFedAPI( v1fedmux.Handle("/backfill/{roomID}/", common.MakeFedAPI(
"federation_backfill", cfg.Matrix.ServerName, keys, "federation_backfill", cfg.Matrix.ServerName, keys,

View File

@ -438,6 +438,7 @@ func (r *RoomserverQueryAPI) QueryMissingEvents(
response *api.QueryMissingEventsResponse, response *api.QueryMissingEventsResponse,
) error { ) error {
var front []string var front []string
eventsToFilter := make(map[string]bool, len(request.LatestEvents))
visited := make(map[string]bool, request.Limit) // request.Limit acts as a hint to size. visited := make(map[string]bool, request.Limit) // request.Limit acts as a hint to size.
for _, id := range request.EarliestEvents { for _, id := range request.EarliestEvents {
visited[id] = true visited[id] = true
@ -446,6 +447,7 @@ func (r *RoomserverQueryAPI) QueryMissingEvents(
for _, id := range request.LatestEvents { for _, id := range request.LatestEvents {
if !visited[id] { if !visited[id] {
front = append(front, id) front = append(front, id)
eventsToFilter[id] = true
} }
} }
@ -454,7 +456,18 @@ func (r *RoomserverQueryAPI) QueryMissingEvents(
return err return err
} }
response.Events, err = r.loadEvents(ctx, resultNIDs) loadedEvents, err := r.loadEvents(ctx, resultNIDs)
if err != nil {
return err
}
response.Events = make([]gomatrixserverlib.Event, 0, len(loadedEvents)-len(eventsToFilter))
for _, event := range loadedEvents {
if !eventsToFilter[event.EventID()] {
response.Events = append(response.Events, event)
}
}
return err return err
} }