This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 678
[federation] Implement state APIs #486
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/github.com/matrix-org/dendrite/federationapi/routing/state.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package routing | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/matrix-org/dendrite/common/config" | ||
"github.com/matrix-org/dendrite/roomserver/api" | ||
"github.com/matrix-org/gomatrixserverlib" | ||
"github.com/matrix-org/util" | ||
) | ||
|
||
// GetState returns state events & auth events for the roomID, eventID | ||
func GetState( | ||
ctx context.Context, | ||
request *gomatrixserverlib.FederationRequest, | ||
_ config.Dendrite, | ||
query api.RoomserverQueryAPI, | ||
_ time.Time, | ||
_ gomatrixserverlib.KeyRing, | ||
roomID string, | ||
eventID string, | ||
) util.JSONResponse { | ||
state, err := getState(ctx, request, query, roomID, eventID) | ||
if err != nil { | ||
return *err | ||
} | ||
|
||
return util.JSONResponse{Code: http.StatusOK, JSON: state} | ||
} | ||
|
||
// GetStateIDs returns state event IDs & auth event IDs for the roomID, eventID | ||
func GetStateIDs( | ||
ctx context.Context, | ||
request *gomatrixserverlib.FederationRequest, | ||
_ config.Dendrite, | ||
query api.RoomserverQueryAPI, | ||
_ time.Time, | ||
_ gomatrixserverlib.KeyRing, | ||
roomID string, | ||
eventID string, | ||
) util.JSONResponse { | ||
state, err := getState(ctx, request, query, roomID, eventID) | ||
if err != nil { | ||
return *err | ||
} | ||
|
||
stateEventIDs := getIDsFromEvent(state.StateEvents) | ||
authEventIDs := getIDsFromEvent(state.AuthEvents) | ||
|
||
return util.JSONResponse{Code: http.StatusOK, JSON: gomatrixserverlib.RespStateIDs{ | ||
StateEventIDs: stateEventIDs, | ||
AuthEventIDs: authEventIDs, | ||
}, | ||
} | ||
} | ||
|
||
func getState( | ||
ctx context.Context, | ||
request *gomatrixserverlib.FederationRequest, | ||
query api.RoomserverQueryAPI, | ||
roomID string, | ||
eventID string, | ||
) (*gomatrixserverlib.RespState, *util.JSONResponse) { | ||
event, resErr := getEvent(ctx, request, query, eventID) | ||
if resErr != nil { | ||
return nil, resErr | ||
} | ||
|
||
prevEventIDs := getIDsFromEventRef(event.PrevEvents()) | ||
authEventIDs := getIDsFromEventRef(event.AuthEvents()) | ||
|
||
var response api.QueryStateAndAuthChainResponse | ||
err := query.QueryStateAndAuthChain( | ||
ctx, | ||
&api.QueryStateAndAuthChainRequest{ | ||
RoomID: roomID, | ||
PrevEventIDs: prevEventIDs, | ||
AuthEventIDs: authEventIDs, | ||
}, | ||
&response, | ||
) | ||
if err != nil { | ||
resErr := util.ErrorResponse(err) | ||
return nil, &resErr | ||
} | ||
|
||
if !response.RoomExists { | ||
return nil, &util.JSONResponse{Code: http.StatusNotFound, JSON: nil} | ||
} | ||
|
||
return &gomatrixserverlib.RespState{ | ||
StateEvents: response.StateEvents, | ||
AuthEvents: response.AuthChainEvents, | ||
}, nil | ||
} | ||
|
||
func getIDsFromEventRef(events []gomatrixserverlib.EventReference) []string { | ||
IDs := make([]string, len(events)) | ||
for i := range events { | ||
IDs[i] = events[i].EventID | ||
} | ||
|
||
return IDs | ||
} | ||
|
||
func getIDsFromEvent(events []gomatrixserverlib.Event) []string { | ||
IDs := make([]string, len(events)) | ||
for i := range events { | ||
IDs[i] = events[i].EventID() | ||
} | ||
|
||
return IDs | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could also just be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't believe it either. That is not allowed in go.