Add register.available method

This commit is contained in:
nxshock 2019-08-02 18:17:18 +05:00
parent 2cffa574fa
commit 11097f763c
7 changed files with 68 additions and 0 deletions

View File

@ -8,6 +8,7 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/
- [x] [5.4.4 POST /_matrix/client/r0/logout/all](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all)
- [x] [5.5.1 POST /_matrix/client/r0/register](https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-register)
- [x] [5.5.4 POST /_matrix/client/r0/account/password](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-account-password)
- [x] [5.5.8 GET /_matrix/client/r0/register/available](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-register-available)
- [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami)
- [x] [6.1 GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities)
- [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms)

View File

@ -12,6 +12,7 @@ type Backend interface {
Register(username, password, device string) (user User, token string, err *models.ApiError)
Login(username, password, device string) (user User, token string, err *models.ApiError)
GetUserByToken(token string) (user User)
GetUserByName(userName string) User
GetRoomByID(id string) Room
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
}

View File

@ -100,3 +100,14 @@ func (backend *Backend) GetRoomByID(id string) internal.Room {
return nil
}
func (backend *Backend) GetUserByName(userName string) internal.User {
backend.mutex.Lock()
defer backend.mutex.Unlock()
if user, exists := backend.data[userName]; exists {
return user
}
return nil
}

View File

@ -113,3 +113,26 @@ func TestGetRoomByID(t *testing.T) {
room = backend.GetRoomByID("worng id")
assert.Nil(t, room)
}
func TestGetUserByName(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user"
)
user, token, err := backend.Register(userName, "", "")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.NotEmpty(t, token)
t.Run("Test picking user with username", func(_ *testing.T) {
user2 := backend.GetUserByName(userName)
assert.Equal(t, user, user2)
})
t.Run("Test picking user with wrong username", func(_ *testing.T) {
user2 := backend.GetUserByName("wrong username")
assert.Nil(t, user2)
})
}

View File

@ -8,6 +8,8 @@ import (
"strconv"
"strings"
"github.com/nxshock/signaller/internal/models/registeravailable"
"github.com/nxshock/signaller/internal/models/devices"
"github.com/gorilla/mux"
@ -348,6 +350,26 @@ func DevicesHandler(w http.ResponseWriter, r *http.Request) {
sendJsonResponse(w, http.StatusOK, response)
}
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-register-available
func registerAvailableHandler(w http.ResponseWriter, r *http.Request) {
var request registeravailable.Request
err := getRequest(r, &request)
if err != nil {
errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error())
return
}
user := currServer.Backend.GetUserByName(request.Username) // TODO: add validation of username
if user != nil {
errorResponse(w, models.M_USER_IN_USE, http.StatusBadRequest, "Desired user ID is already taken.")
return
}
response := registeravailable.Response{Available: false}
sendJsonResponse(w, http.StatusOK, response)
}
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-directory-list-room-roomid
// https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-directory-list-room-roomid
func listRoomHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -0,0 +1,9 @@
package registeravailable
type Request struct {
Username string `json:"username"` // The username to check the availability of.
}
type Response struct {
Available bool `json:"available"` // A flag to indicate that the username is available. This should always be true when the server replies with 200 OK.
}

View File

@ -38,6 +38,7 @@ func NewServer(port int) (*Server, error) {
router.HandleFunc("/_matrix/client/r0/devices", DevicesHandler)
router.HandleFunc("/_matrix/client/r0/directory/list/room/{roomID}", listRoomHandler)
router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler)
router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler)
router.HandleFunc("/", RootHandler)
if port <= 0 || port > 65535 {