mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 01:13:10 -06:00
Use go standard errors in membership.go
This commit is contained in:
parent
cf6c2887ee
commit
bdc71b4757
|
|
@ -15,6 +15,7 @@
|
||||||
package writers
|
package writers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
|
|
@ -32,6 +33,8 @@ import (
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errMissingUserID = errors.New("'user_id' must be supplied.")
|
||||||
|
|
||||||
// SendMembership implements PUT /rooms/{roomID}/(join|kick|ban|unban|leave|invite)
|
// SendMembership implements PUT /rooms/{roomID}/(join|kick|ban|unban|leave|invite)
|
||||||
// by building a m.room.member event then sending it to the room server
|
// by building a m.room.member event then sending it to the room server
|
||||||
func SendMembership(
|
func SendMembership(
|
||||||
|
|
@ -50,14 +53,46 @@ func SendMembership(
|
||||||
return *res
|
return *res
|
||||||
}
|
}
|
||||||
|
|
||||||
stateKey, reason, reqErr := getMembershipStateKey(body, device, membership)
|
event, err := buildMembershipEvent(
|
||||||
if reqErr != nil {
|
body, accountDB, device, membership, roomID, cfg, queryAPI,
|
||||||
return *reqErr
|
)
|
||||||
|
if err == errMissingUserID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 400,
|
||||||
|
JSON: jsonerror.BadJSON(err.Error()),
|
||||||
|
}
|
||||||
|
} else if err == events.ErrRoomNoExists {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 404,
|
||||||
|
JSON: jsonerror.NotFound(err.Error()),
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
return httputil.LogThenError(req, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := producer.SendEvents([]gomatrixserverlib.Event{*event}, cfg.Matrix.ServerName); err != nil {
|
||||||
|
return httputil.LogThenError(req, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 200,
|
||||||
|
JSON: struct{}{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildMembershipEvent(
|
||||||
|
body threepid.MembershipRequest, accountDB *accounts.Database,
|
||||||
|
device *authtypes.Device, membership string, roomID string, cfg config.Dendrite,
|
||||||
|
queryAPI api.RoomserverQueryAPI,
|
||||||
|
) (*gomatrixserverlib.Event, error) {
|
||||||
|
stateKey, reason, err := getMembershipStateKey(body, device, membership)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
profile, err := loadProfile(stateKey, cfg, accountDB)
|
profile, err := loadProfile(stateKey, cfg, accountDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httputil.LogThenError(req, err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
builder := gomatrixserverlib.EventBuilder{
|
builder := gomatrixserverlib.EventBuilder{
|
||||||
|
|
@ -80,27 +115,10 @@ func SendMembership(
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = builder.SetContent(content); err != nil {
|
if err = builder.SetContent(content); err != nil {
|
||||||
return httputil.LogThenError(req, err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
event, err := events.BuildEvent(&builder, cfg, queryAPI, nil)
|
return events.BuildEvent(&builder, cfg, queryAPI, nil)
|
||||||
if err == events.ErrRoomNoExists {
|
|
||||||
return util.JSONResponse{
|
|
||||||
Code: 404,
|
|
||||||
JSON: jsonerror.NotFound(err.Error()),
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
return httputil.LogThenError(req, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := producer.SendEvents([]gomatrixserverlib.Event{*event}, cfg.Matrix.ServerName); err != nil {
|
|
||||||
return httputil.LogThenError(req, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return util.JSONResponse{
|
|
||||||
Code: 200,
|
|
||||||
JSON: struct{}{},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadProfile lookups the profile of a given user from the database and returns
|
// loadProfile lookups the profile of a given user from the database and returns
|
||||||
|
|
@ -130,16 +148,13 @@ func loadProfile(userID string, cfg config.Dendrite, accountDB *accounts.Databas
|
||||||
// returns a JSONResponse with a corresponding error code and message.
|
// returns a JSONResponse with a corresponding error code and message.
|
||||||
func getMembershipStateKey(
|
func getMembershipStateKey(
|
||||||
body threepid.MembershipRequest, device *authtypes.Device, membership string,
|
body threepid.MembershipRequest, device *authtypes.Device, membership string,
|
||||||
) (stateKey string, reason string, response *util.JSONResponse) {
|
) (stateKey string, reason string, err error) {
|
||||||
if membership == "ban" || membership == "unban" || membership == "kick" || membership == "invite" {
|
if membership == "ban" || membership == "unban" || membership == "kick" || membership == "invite" {
|
||||||
// If we're in this case, the state key is contained in the request body,
|
// If we're in this case, the state key is contained in the request body,
|
||||||
// possibly along with a reason (for "kick" and "ban") so we need to parse
|
// possibly along with a reason (for "kick" and "ban") so we need to parse
|
||||||
// it
|
// it
|
||||||
if body.UserID == "" {
|
if body.UserID == "" {
|
||||||
response = &util.JSONResponse{
|
err = errMissingUserID
|
||||||
Code: 400,
|
|
||||||
JSON: jsonerror.BadJSON("'user_id' must be supplied."),
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue