mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-09 12:11:03 +00:00
Add register.available method
This commit is contained in:
parent
2cffa574fa
commit
11097f763c
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
@ -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) {
|
||||
|
9
internal/models/registeravailable/registeravailable.go
Normal file
9
internal/models/registeravailable/registeravailable.go
Normal 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.
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user