MCPcopy Create free account
hub / github.com/daodst/chat / RegisterAvailable

Function RegisterAvailable

clientapi/routing/register.go:1011–1060  ·  view source on GitHub ↗

RegisterAvailable checks if the username is already taken or invalid.

(
	req *http.Request,
	cfg *config.ClientAPI,
	registerAPI userapi.ClientUserAPI,
)

Source from the content-addressed store, hash-verified

1009
1010// RegisterAvailable checks if the username is already taken or invalid.
1011func RegisterAvailable(
1012 req *http.Request,
1013 cfg *config.ClientAPI,
1014 registerAPI userapi.ClientUserAPI,
1015) util.JSONResponse {
1016 username := req.URL.Query().Get("username")
1017
1018 // Squash username to all lowercase letters
1019 username = strings.ToLower(username)
1020
1021 if err := validateUsername(username); err != nil {
1022 return *err
1023 }
1024
1025 // Check if this username is reserved by an application service
1026 userID := userutil.MakeUserID(username, cfg.Matrix.ServerName)
1027 for _, appservice := range cfg.Derived.ApplicationServices {
1028 if appservice.OwnsNamespaceCoveringUserId(userID) {
1029 return util.JSONResponse{
1030 Code: http.StatusBadRequest,
1031 JSON: jsonerror.UserInUse("Desired user ID is reserved by an application service."),
1032 }
1033 }
1034 }
1035
1036 res := &userapi.QueryAccountAvailabilityResponse{}
1037 err := registerAPI.QueryAccountAvailability(req.Context(), &userapi.QueryAccountAvailabilityRequest{
1038 Localpart: username,
1039 }, res)
1040 if err != nil {
1041 return util.JSONResponse{
1042 Code: http.StatusInternalServerError,
1043 JSON: jsonerror.Unknown("failed to check availability:" + err.Error()),
1044 }
1045 }
1046
1047 if !res.Available {
1048 return util.JSONResponse{
1049 Code: http.StatusBadRequest,
1050 JSON: jsonerror.UserInUse("Desired User ID is already taken."),
1051 }
1052 }
1053
1054 return util.JSONResponse{
1055 Code: http.StatusOK,
1056 JSON: availableResponse{
1057 Available: true,
1058 },
1059 }
1060}
1061
1062func handleSharedSecretRegistration(userAPI userapi.ClientUserAPI, sr *SharedSecretRegistration, req *http.Request) util.JSONResponse {
1063 ssrr, err := NewSharedSecretRegistrationRequest(req.Body)

Callers 1

SetupFunction · 0.85

Calls 6

validateUsernameFunction · 0.85
ContextMethod · 0.80
GetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected