mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-09 22:42:58 +00:00
[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:
parent
55498c8deb
commit
3205b9212d
@ -104,17 +104,18 @@ func GetEvent(
|
||||
}
|
||||
|
||||
for _, stateEvent := range stateResp.StateEvents {
|
||||
if stateEvent.StateKeyEquals(r.device.UserID) {
|
||||
membership, err := stateEvent.Membership()
|
||||
if err != nil {
|
||||
util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed")
|
||||
return jsonerror.InternalServerError()
|
||||
}
|
||||
if membership == gomatrixserverlib.Join {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: gomatrixserverlib.ToClientEvent(r.requestedEvent, gomatrixserverlib.FormatAll),
|
||||
}
|
||||
if !stateEvent.StateKeyEquals(device.UserID) {
|
||||
continue
|
||||
}
|
||||
membership, err := stateEvent.Membership()
|
||||
if err != nil {
|
||||
util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed")
|
||||
return jsonerror.InternalServerError()
|
||||
}
|
||||
if membership == gomatrixserverlib.Join {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: gomatrixserverlib.ToClientEvent(r.requestedEvent, gomatrixserverlib.FormatAll),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"),
|
||||
|
@ -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(), ¤tstateAPI.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")
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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 !se.StateKeyEquals(*event.StateKey()) {
|
||||
continue
|
||||
}
|
||||
if membership, merr := se.Membership(); merr == nil {
|
||||
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
|
||||
alreadyJoined = (membership == "join")
|
||||
break
|
||||
}
|
||||
alreadyJoined = (membership == gomatrixserverlib.Join)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,15 +81,16 @@ 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 mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: map[string]interface{}{
|
||||
"room_version": event.RoomVersion,
|
||||
"event": state,
|
||||
},
|
||||
}
|
||||
if !state.StateKeyEquals(userID) {
|
||||
continue
|
||||
}
|
||||
if mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: map[string]interface{}{
|
||||
"room_version": event.RoomVersion,
|
||||
"event": state,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -204,11 +204,12 @@ 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
|
||||
}
|
||||
alreadyJoined = (membership == gomatrixserverlib.Join)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -1216,14 +1216,14 @@ 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) {
|
||||
membership, err := ev.Membership()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return membership
|
||||
if ev.Type() != "m.room.member" || !ev.StateKeyEquals(userID) {
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
membership, err := ev.Membership()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return membership
|
||||
}
|
||||
|
||||
type stateDelta struct {
|
||||
|
Loading…
Reference in New Issue
Block a user