Move building an invite to it's own function, for reusability

This commit is contained in:
Till Faelligen 2022-02-18 15:11:36 +01:00
parent ff724bce4b
commit 5a346e536f

View file

@ -226,27 +226,42 @@ func SendInvite(
} }
} }
// We already received the return value, so no need to check for an error here.
response, _ := sendInvite(req.Context(), accountDB, device, roomID, body.UserID, body.Reason, cfg, rsAPI, asAPI, evTime)
return response
}
// sendInvite sends an invitation to a user. Returns a JSONResponse and an error
func sendInvite(
ctx context.Context,
accountDB userdb.Database,
device *userapi.Device,
roomID, userID, reason string,
cfg *config.ClientAPI,
rsAPI roomserverAPI.RoomserverInternalAPI,
asAPI appserviceAPI.AppServiceQueryAPI, evTime time.Time,
) (util.JSONResponse, error) {
event, err := buildMembershipEvent( event, err := buildMembershipEvent(
req.Context(), body.UserID, body.Reason, accountDB, device, "invite", ctx, userID, reason, accountDB, device, "invite",
roomID, false, cfg, evTime, rsAPI, asAPI, roomID, false, cfg, evTime, rsAPI, asAPI,
) )
if err == errMissingUserID { if err == errMissingUserID {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(err.Error()), JSON: jsonerror.BadJSON(err.Error()),
} }, err
} else if err == eventutil.ErrRoomNoExists { } else if err == eventutil.ErrRoomNoExists {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusNotFound, Code: http.StatusNotFound,
JSON: jsonerror.NotFound(err.Error()), JSON: jsonerror.NotFound(err.Error()),
} }, err
} else if err != nil { } else if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("buildMembershipEvent failed") util.GetLogger(ctx).WithError(err).Error("buildMembershipEvent failed")
return jsonerror.InternalServerError() return jsonerror.InternalServerError(), err
} }
err = roomserverAPI.SendInvite( err = roomserverAPI.SendInvite(
req.Context(), rsAPI, ctx, rsAPI,
event, event,
nil, // ask the roomserver to draw up invite room state for us nil, // ask the roomserver to draw up invite room state for us
cfg.Matrix.ServerName, cfg.Matrix.ServerName,
@ -254,18 +269,18 @@ func SendInvite(
) )
switch e := err.(type) { switch e := err.(type) {
case *roomserverAPI.PerformError: case *roomserverAPI.PerformError:
return e.JSONResponse() return e.JSONResponse(), err
case nil: case nil:
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: struct{}{}, JSON: struct{}{},
} }, nil
default: default:
util.GetLogger(req.Context()).WithError(err).Error("roomserverAPI.SendInvite failed") util.GetLogger(ctx).WithError(err).Error("roomserverAPI.SendInvite failed")
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
JSON: jsonerror.InternalServerError(), JSON: jsonerror.InternalServerError(),
} }, err
} }
} }