mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 07:28:27 +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
10 changed files with 60 additions and 84 deletions
|
@ -104,17 +104,18 @@ func GetEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, stateEvent := range stateResp.StateEvents {
|
for _, stateEvent := range stateResp.StateEvents {
|
||||||
if stateEvent.StateKeyEquals(r.device.UserID) {
|
if !stateEvent.StateKeyEquals(device.UserID) {
|
||||||
membership, err := stateEvent.Membership()
|
continue
|
||||||
if err != nil {
|
}
|
||||||
util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed")
|
membership, err := stateEvent.Membership()
|
||||||
return jsonerror.InternalServerError()
|
if err != nil {
|
||||||
}
|
util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed")
|
||||||
if membership == gomatrixserverlib.Join {
|
return jsonerror.InternalServerError()
|
||||||
return util.JSONResponse{
|
}
|
||||||
Code: http.StatusOK,
|
if membership == gomatrixserverlib.Join {
|
||||||
JSON: gomatrixserverlib.ToClientEvent(r.requestedEvent, gomatrixserverlib.FormatAll),
|
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()
|
e := jsonerror.InternalServerError()
|
||||||
return &e
|
return &e
|
||||||
}
|
}
|
||||||
ev, ok := membershipRes.StateEvents[tuple]
|
ev := membershipRes.StateEvents[tuple]
|
||||||
if !ok {
|
if ev == nil {
|
||||||
return &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusForbidden,
|
Code: http.StatusForbidden,
|
||||||
JSON: jsonerror.Forbidden("user does not belong to room"),
|
JSON: jsonerror.Forbidden("user does not belong to room"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
membership, err := ev.Membership()
|
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{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusForbidden,
|
Code: http.StatusForbidden,
|
||||||
JSON: jsonerror.Forbidden("user does not belong to room"),
|
JSON: jsonerror.Forbidden("user does not belong to room"),
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/eduserver/api"
|
"github.com/matrix-org/dendrite/eduserver/api"
|
||||||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||||
"github.com/matrix-org/dendrite/userapi/storage/accounts"
|
"github.com/matrix-org/dendrite/userapi/storage/accounts"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,46 +45,19 @@ func SendTyping(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the user is a member of this room
|
// Verify that the user is a member of this room
|
||||||
tuple := gomatrixserverlib.StateKeyTuple{
|
resErr := checkMemberInRoom(req.Context(), stateAPI, userID, roomID)
|
||||||
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)
|
|
||||||
if resErr != nil {
|
if resErr != nil {
|
||||||
return *resErr
|
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,
|
req.Context(), eduAPI, userID, roomID, r.Typing, r.Timeout,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
util.GetLogger(req.Context()).WithError(err).Error("eduProducer.Send failed")
|
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) {
|
func (c *OutputRoomEventConsumer) updateStateEvent(event gomatrixserverlib.HeaderedEvent) (gomatrixserverlib.HeaderedEvent, error) {
|
||||||
var stateKey string
|
stateKey := ""
|
||||||
if event.StateKey() == nil {
|
if event.StateKey() != nil {
|
||||||
stateKey = ""
|
|
||||||
} else {
|
|
||||||
stateKey = *event.StateKey()
|
stateKey = *event.StateKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,7 @@ func SendJoin(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that a state key is provided.
|
// 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{
|
return util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: jsonerror.BadJSON(
|
JSON: jsonerror.BadJSON(
|
||||||
|
@ -253,11 +253,12 @@ func SendJoin(
|
||||||
// there isn't much point in sending another join event into the room.
|
// there isn't much point in sending another join event into the room.
|
||||||
alreadyJoined := false
|
alreadyJoined := false
|
||||||
for _, se := range stateAndAuthChainResponse.StateEvents {
|
for _, se := range stateAndAuthChainResponse.StateEvents {
|
||||||
|
if !se.StateKeyEquals(*event.StateKey()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if membership, merr := se.Membership(); merr == nil {
|
if membership, merr := se.Membership(); merr == nil {
|
||||||
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
|
alreadyJoined = (membership == gomatrixserverlib.Join)
|
||||||
alreadyJoined = (membership == "join")
|
break
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,15 +81,16 @@ func MakeLeave(
|
||||||
// event. This means that /send_leave will be a no-op, which helps
|
// event. This means that /send_leave will be a no-op, which helps
|
||||||
// to reject invites multiple times - hopefully.
|
// to reject invites multiple times - hopefully.
|
||||||
for _, state := range queryRes.StateEvents {
|
for _, state := range queryRes.StateEvents {
|
||||||
if state.Type() == gomatrixserverlib.MRoomMember && state.StateKeyEquals(userID) {
|
if !state.StateKeyEquals(userID) {
|
||||||
if mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
continue
|
||||||
return util.JSONResponse{
|
}
|
||||||
Code: http.StatusOK,
|
if mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
||||||
JSON: map[string]interface{}{
|
return util.JSONResponse{
|
||||||
"room_version": event.RoomVersion,
|
Code: http.StatusOK,
|
||||||
"event": state,
|
JSON: map[string]interface{}{
|
||||||
},
|
"room_version": event.RoomVersion,
|
||||||
}
|
"event": state,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ func (b *backfillRequester) calculateNewStateIDs(targetEvent, prevEvent gomatrix
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// The state IDs BEFORE the target event are the state IDs BEFORE the prev_event PLUS the prev_event itself
|
// 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()
|
newStateIDs[i] = prevEvent.EventID()
|
||||||
foundEvent = true
|
foundEvent = true
|
||||||
break
|
break
|
||||||
|
|
|
@ -204,11 +204,12 @@ func (r *RoomserverInternalAPI) performJoinRoomByID(
|
||||||
// a member of the room.
|
// a member of the room.
|
||||||
alreadyJoined := false
|
alreadyJoined := false
|
||||||
for _, se := range buildRes.StateEvents {
|
for _, se := range buildRes.StateEvents {
|
||||||
|
if !se.StateKeyEquals(userID) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if membership, merr := se.Membership(); merr == nil {
|
if membership, merr := se.Membership(); merr == nil {
|
||||||
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
|
alreadyJoined = (membership == gomatrixserverlib.Join)
|
||||||
alreadyJoined = (membership == gomatrixserverlib.Join)
|
break
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,9 +169,6 @@ func (s *OutputRoomEventConsumer) onNewRoomEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OutputRoomEventConsumer) notifyKeyChanges(ev *gomatrixserverlib.HeaderedEvent) {
|
func (s *OutputRoomEventConsumer) notifyKeyChanges(ev *gomatrixserverlib.HeaderedEvent) {
|
||||||
if ev.Type() != gomatrixserverlib.MRoomMember || ev.StateKey() == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
membership, err := ev.Membership()
|
membership, err := ev.Membership()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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
|
// 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.
|
// with type 'm.room.member' and state_key of userID. Otherwise, an empty string is returned.
|
||||||
func getMembershipFromEvent(ev *gomatrixserverlib.Event, userID string) string {
|
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) {
|
||||||
membership, err := ev.Membership()
|
return ""
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return membership
|
|
||||||
}
|
}
|
||||||
return ""
|
membership, err := ev.Membership()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return membership
|
||||||
}
|
}
|
||||||
|
|
||||||
type stateDelta struct {
|
type stateDelta struct {
|
||||||
|
|
Loading…
Reference in a new issue