[readability] use event.StateKeyEquals where relevant and minor for-loop refactoring (#1339)

* use event.StateKeyEquals where relevant

Signed-off-by: Olivier Charvin <git@olivier.pfad.fr>

* use userID
This commit is contained in:
oliverpool 2020-08-25 22:04:35 +02:00 committed by GitHub
parent 55498c8deb
commit 3205b9212d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 60 additions and 84 deletions

View File

@ -104,7 +104,9 @@ func GetEvent(
}
for _, stateEvent := range stateResp.StateEvents {
if stateEvent.StateKeyEquals(r.device.UserID) {
if !stateEvent.StateKeyEquals(device.UserID) {
continue
}
membership, err := stateEvent.Membership()
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed")
@ -117,7 +119,6 @@ func GetEvent(
}
}
}
}
return util.JSONResponse{
Code: http.StatusNotFound,

View File

@ -388,15 +388,20 @@ func checkMemberInRoom(ctx context.Context, stateAPI currentstateAPI.CurrentStat
e := jsonerror.InternalServerError()
return &e
}
ev, ok := membershipRes.StateEvents[tuple]
if !ok {
ev := membershipRes.StateEvents[tuple]
if ev == nil {
return &util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("user does not belong to room"),
}
}
membership, err := ev.Membership()
if err != nil || membership != "join" {
if err != nil {
util.GetLogger(ctx).WithError(err).Error("Member event isn't valid")
e := jsonerror.InternalServerError()
return &e
}
if membership != gomatrixserverlib.Join {
return &util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("user does not belong to room"),

View File

@ -21,7 +21,6 @@ import (
"github.com/matrix-org/dendrite/eduserver/api"
userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/dendrite/userapi/storage/accounts"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)
@ -46,46 +45,19 @@ func SendTyping(
}
// Verify that the user is a member of this room
tuple := gomatrixserverlib.StateKeyTuple{
EventType: gomatrixserverlib.MRoomMember,
StateKey: userID,
}
var res currentstateAPI.QueryCurrentStateResponse
err := stateAPI.QueryCurrentState(req.Context(), &currentstateAPI.QueryCurrentStateRequest{
RoomID: roomID,
StateTuples: []gomatrixserverlib.StateKeyTuple{tuple},
}, &res)
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("QueryCurrentState failed")
return jsonerror.InternalServerError()
}
ev := res.StateEvents[tuple]
if ev == nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("User not in this room"),
}
}
membership, err := ev.Membership()
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("Member event isn't valid")
return jsonerror.InternalServerError()
}
if membership != gomatrixserverlib.Join {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("User not in this room"),
}
}
// parse the incoming http request
var r typingContentJSON
resErr := httputil.UnmarshalJSONRequest(req, &r)
resErr := checkMemberInRoom(req.Context(), stateAPI, userID, roomID)
if resErr != nil {
return *resErr
}
if err = api.SendTyping(
// parse the incoming http request
var r typingContentJSON
resErr = httputil.UnmarshalJSONRequest(req, &r)
if resErr != nil {
return *resErr
}
if err := api.SendTyping(
req.Context(), eduAPI, userID, roomID, r.Typing, r.Timeout,
); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("eduProducer.Send failed")

View File

@ -126,10 +126,8 @@ func (c *OutputRoomEventConsumer) Start() error {
}
func (c *OutputRoomEventConsumer) updateStateEvent(event gomatrixserverlib.HeaderedEvent) (gomatrixserverlib.HeaderedEvent, error) {
var stateKey string
if event.StateKey() == nil {
stateKey = ""
} else {
stateKey := ""
if event.StateKey() != nil {
stateKey = *event.StateKey()
}

View File

@ -165,7 +165,7 @@ func SendJoin(
}
// Check that a state key is provided.
if event.StateKey() == nil || (event.StateKey() != nil && *event.StateKey() == "") {
if event.StateKey() == nil || event.StateKeyEquals("") {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(
@ -253,11 +253,12 @@ func SendJoin(
// there isn't much point in sending another join event into the room.
alreadyJoined := false
for _, se := range stateAndAuthChainResponse.StateEvents {
if membership, merr := se.Membership(); merr == nil {
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
alreadyJoined = (membership == "join")
break
if !se.StateKeyEquals(*event.StateKey()) {
continue
}
if membership, merr := se.Membership(); merr == nil {
alreadyJoined = (membership == gomatrixserverlib.Join)
break
}
}

View File

@ -81,7 +81,9 @@ func MakeLeave(
// event. This means that /send_leave will be a no-op, which helps
// to reject invites multiple times - hopefully.
for _, state := range queryRes.StateEvents {
if state.Type() == gomatrixserverlib.MRoomMember && state.StateKeyEquals(userID) {
if !state.StateKeyEquals(userID) {
continue
}
if mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
return util.JSONResponse{
Code: http.StatusOK,
@ -92,7 +94,6 @@ func MakeLeave(
}
}
}
}
// Check that the leave is allowed or not
stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))

View File

@ -104,7 +104,7 @@ func (b *backfillRequester) calculateNewStateIDs(targetEvent, prevEvent gomatrix
continue
}
// The state IDs BEFORE the target event are the state IDs BEFORE the prev_event PLUS the prev_event itself
if ev.Type() == prevEvent.Type() && ev.StateKey() != nil && *ev.StateKey() == *prevEvent.StateKey() {
if ev.Type() == prevEvent.Type() && ev.StateKeyEquals(*prevEvent.StateKey()) {
newStateIDs[i] = prevEvent.EventID()
foundEvent = true
break

View File

@ -204,13 +204,14 @@ func (r *RoomserverInternalAPI) performJoinRoomByID(
// a member of the room.
alreadyJoined := false
for _, se := range buildRes.StateEvents {
if !se.StateKeyEquals(userID) {
continue
}
if membership, merr := se.Membership(); merr == nil {
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
alreadyJoined = (membership == gomatrixserverlib.Join)
break
}
}
}
// If we haven't already joined the room then send an event
// into the room changing our membership status.

View File

@ -169,9 +169,6 @@ func (s *OutputRoomEventConsumer) onNewRoomEvent(
}
func (s *OutputRoomEventConsumer) notifyKeyChanges(ev *gomatrixserverlib.HeaderedEvent) {
if ev.Type() != gomatrixserverlib.MRoomMember || ev.StateKey() == nil {
return
}
membership, err := ev.Membership()
if err != nil {
return

View File

@ -1216,15 +1216,15 @@ func removeDuplicates(stateEvents, recentEvents []gomatrixserverlib.HeaderedEven
// getMembershipFromEvent returns the value of content.membership iff the event is a state event
// with type 'm.room.member' and state_key of userID. Otherwise, an empty string is returned.
func getMembershipFromEvent(ev *gomatrixserverlib.Event, userID string) string {
if ev.Type() == "m.room.member" && ev.StateKeyEquals(userID) {
if ev.Type() != "m.room.member" || !ev.StateKeyEquals(userID) {
return ""
}
membership, err := ev.Membership()
if err != nil {
return ""
}
return membership
}
return ""
}
type stateDelta struct {
roomID string