Check if the user is already in the room in the fedeationapi too

This commit is contained in:
Neil Alexander 2020-05-04 11:08:34 +01:00
parent 7fd784034d
commit 46c46f5532

View file

@ -15,6 +15,7 @@
package routing package routing
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
@ -159,6 +160,16 @@ func SendJoin(
} }
} }
// Check that a state key is provided.
if event.StateKey() == nil || (event.StateKey() != nil && *event.StateKey() == "") {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(
fmt.Sprintf("No state key was provided in the join event."),
),
}
}
// Check that the room ID is correct. // Check that the room ID is correct.
if event.RoomID() != roomID { if event.RoomID() != roomID {
return util.JSONResponse{ return util.JSONResponse{
@ -234,20 +245,40 @@ func SendJoin(
} }
} }
// Check if the user is already in the room. If they're already in then
// there isn't much point in sending another join event into the room.
alreadyJoined := false
for _, se := range stateAndAuthChainResponse.StateEvents {
if se.Type() == gomatrixserverlib.MRoomMember {
if se.StateKey() != nil && *se.StateKey() == *event.StateKey() {
var content map[string]interface{}
if err = json.Unmarshal(se.Content(), &content); err != nil {
continue
}
if membership, ok := content["membership"]; ok {
alreadyJoined = (membership == "join")
break
}
}
}
}
// Send the events to the room server. // Send the events to the room server.
// We are responsible for notifying other servers that the user has joined // We are responsible for notifying other servers that the user has joined
// the room, so set SendAsServer to cfg.Matrix.ServerName // the room, so set SendAsServer to cfg.Matrix.ServerName
_, err = producer.SendEvents( if !alreadyJoined {
httpReq.Context(), _, err = producer.SendEvents(
[]gomatrixserverlib.HeaderedEvent{ httpReq.Context(),
event.Headered(stateAndAuthChainResponse.RoomVersion), []gomatrixserverlib.HeaderedEvent{
}, event.Headered(stateAndAuthChainResponse.RoomVersion),
cfg.Matrix.ServerName, },
nil, cfg.Matrix.ServerName,
) nil,
if err != nil { )
util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendEvents failed") if err != nil {
return jsonerror.InternalServerError() util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendEvents failed")
return jsonerror.InternalServerError()
}
} }
return util.JSONResponse{ return util.JSONResponse{