From b598613caa0a7693adc1e1f14586fca52c01168a Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 18 Jul 2019 19:15:19 +0400 Subject: [PATCH] refactor: Rename all references from matrix to models --- backend.go | 10 ++++----- errors.go | 6 +++--- handlers.go | 44 +++++++++++++++++++-------------------- models/enums.go | 2 +- models/errors.go | 2 +- models/replies.go | 2 +- models/requests.go | 2 +- models/sync.go | 2 +- models/types.go | 2 +- users.go | 52 +++++++++++++++++++++++----------------------- 10 files changed, 62 insertions(+), 62 deletions(-) diff --git a/backend.go b/backend.go index 07a0d5f..db73228 100644 --- a/backend.go +++ b/backend.go @@ -1,10 +1,10 @@ package main -import "github.com/nxshock/signaller/matrix" +import "github.com/nxshock/signaller/models" type Backend interface { - Register(username, password, device string) (token string, error *matrix.ApiError) - Login(username, password, device string) (token string, err *matrix.ApiError) - Logout(token string) *matrix.ApiError - Sync(token string, request matrix.SyncRequest) (response *matrix.SyncReply, err *matrix.ApiError) + Register(username, password, device string) (token string, error *models.ApiError) + Login(username, password, device string) (token string, err *models.ApiError) + Logout(token string) *models.ApiError + Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError) } diff --git a/errors.go b/errors.go index 3485536..2323ddd 100644 --- a/errors.go +++ b/errors.go @@ -3,10 +3,10 @@ package main import ( "net/http" - "github.com/nxshock/signaller/matrix" + "github.com/nxshock/signaller/models" ) -func errorResponse(w http.ResponseWriter, code matrix.ApiError, httpCode int, message string) { +func errorResponse(w http.ResponseWriter, code models.ApiError, httpCode int, message string) { w.Header().Set("Content-Type", "application/json") if message != "" { @@ -17,7 +17,7 @@ func errorResponse(w http.ResponseWriter, code matrix.ApiError, httpCode int, me w.Write(code.JSON()) } -func NewError(code matrix.ApiError, message string) *matrix.ApiError { +func NewError(code models.ApiError, message string) *models.ApiError { if message != "" { code.Message = message } diff --git a/handlers.go b/handlers.go index cdc37e1..bda8abd 100644 --- a/handlers.go +++ b/handlers.go @@ -8,28 +8,28 @@ import ( "strconv" "strings" - "github.com/nxshock/signaller/matrix" + "github.com/nxshock/signaller/models" ) func RootHandler(w http.ResponseWriter, r *http.Request) { log.Println(r.RequestURI) } -// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-versions +// https://models.org/docs/spec/client_server/latest#get-models-client-versions func VersionHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { - errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) return } - response := matrix.VersionsReply{Versions: []string{Version}} + response := models.VersionsReply{Versions: []string{Version}} sendJsonResponse(w, http.StatusOK, response) } -// https://matrix.org/docs/spec/client_server/latest#login +// https://models.org/docs/spec/client_server/latest#login func LoginHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { - // https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-login + // https://models.org/docs/spec/client_server/latest#get-models-client-r0-login case "GET": { type LoginFlow struct { @@ -47,10 +47,10 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } - // https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login + // https://models.org/docs/spec/client_server/latest#post-models-client-r0-login case "POST": { - var request matrix.LoginRequest + var request models.LoginRequest getRequest(r, &request) // TODO: handle error // delete start "@" if presents @@ -64,7 +64,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { return } - response := matrix.LoginReply{ + response := models.LoginReply{ UserID: request.Identifier.User, AccessToken: token} @@ -73,17 +73,17 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { } } -// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout +// https://models.org/docs/spec/client_server/latest#post-models-client-r0-logout func LogoutHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) return } token := getTokenFromResponse(r) if token == "" { - errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "") + errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "") return } @@ -96,20 +96,20 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, struct{}{}) } -// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-register +// https://models.org/docs/spec/client_server/latest#post-models-client-r0-register func RegisterHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) return } kind := r.FormValue("kind") if kind != "user" { - errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind) + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind) return } - var request matrix.RegisterRequest + var request models.RegisterRequest getRequest(r, &request) // TODO: handle error token, apiErr := server.Backend.Register(request.Username, request.Password, request.DeviceID) @@ -118,7 +118,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) { return } - var response matrix.RegisterResponse + var response models.RegisterResponse response.UserID = "@" + request.Username response.DeviceID = request.DeviceID response.AccessToken = token @@ -126,14 +126,14 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } -// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-sync +// https://models.org/docs/spec/client_server/latest#get-models-client-r0-sync func SyncHandler(w http.ResponseWriter, r *http.Request) { - var request matrix.SyncRequest + var request models.SyncRequest request.Filter = r.FormValue("filter") timeout, err := strconv.Atoi(r.FormValue("timeout")) if err != nil { - errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes") + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes") return } request.Timeout = timeout @@ -141,14 +141,14 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) { token := getTokenFromResponse(r) if token == "" { - errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "") + errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "") return } response, _ := server.Backend.Sync(token, request) // TODO: handle error response.NextBatch = "123" - response.Rooms = matrix.RoomsSyncReply{} + response.Rooms = models.RoomsSyncReply{} sendJsonResponse(w, http.StatusOK, response) } diff --git a/models/enums.go b/models/enums.go index d19f2c6..9dcd48c 100644 --- a/models/enums.go +++ b/models/enums.go @@ -1,4 +1,4 @@ -package matrix +package models type Membership string diff --git a/models/errors.go b/models/errors.go index 6133f99..3f8f614 100644 --- a/models/errors.go +++ b/models/errors.go @@ -1,4 +1,4 @@ -package matrix +package models import ( "encoding/json" diff --git a/models/replies.go b/models/replies.go index cdff185..b35c232 100644 --- a/models/replies.go +++ b/models/replies.go @@ -1,4 +1,4 @@ -package matrix +package models type JoinedRoomReply struct { JoinedRooms []string `json:"joined_rooms"` diff --git a/models/requests.go b/models/requests.go index 6852776..fa7c756 100644 --- a/models/requests.go +++ b/models/requests.go @@ -1,4 +1,4 @@ -package matrix +package models // LoginRequest represents login request // https://matrix.org/docs/spec/client_server/r0.4.0.html#post-matrix-client-r0-login diff --git a/models/sync.go b/models/sync.go index 6b3074d..f1670ad 100644 --- a/models/sync.go +++ b/models/sync.go @@ -1,4 +1,4 @@ -package matrix +package models import ( "encoding/json" diff --git a/models/types.go b/models/types.go index e32c9c8..7a2335a 100644 --- a/models/types.go +++ b/models/types.go @@ -1,4 +1,4 @@ -package matrix +package models type MRelatesTo struct { InReplyTo MInReplyTo `json:"m.in_reply_to"` diff --git a/users.go b/users.go index 1badee5..d90fd3f 100644 --- a/users.go +++ b/users.go @@ -6,7 +6,7 @@ import ( "os" "sync" - "github.com/nxshock/signaller/matrix" + "github.com/nxshock/signaller/models" ) var first bool @@ -29,12 +29,12 @@ func NewMemoryBackend() *MemoryBackend { return &MemoryBackend{data: make(map[string]*User)} } -func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *matrix.ApiError) { +func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *models.ApiError) { memoryBackend.mutex.Lock() defer memoryBackend.mutex.Unlock() if _, ok := memoryBackend.data[username]; ok { - return "", NewError(matrix.M_USER_IN_USE, "trying to register a user ID which has been taken") + return "", NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken") } token = newToken(DefaultTokenSize) @@ -48,17 +48,17 @@ func (memoryBackend MemoryBackend) Register(username, password, device string) ( return token, nil } -func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *matrix.ApiError) { +func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *models.ApiError) { memoryBackend.mutex.Lock() defer memoryBackend.mutex.Unlock() user, ok := memoryBackend.data[username] if !ok { - return "", NewError(matrix.M_FORBIDDEN, "wrong username") + return "", NewError(models.M_FORBIDDEN, "wrong username") } if user.Password != password { - return "", NewError(matrix.M_FORBIDDEN, "wrong password") + return "", NewError(models.M_FORBIDDEN, "wrong password") } token = newToken(DefaultTokenSize) @@ -68,7 +68,7 @@ func (memoryBackend MemoryBackend) Login(username, password, device string) (tok return token, nil } -func (memoryBackend MemoryBackend) Logout(token string) *matrix.ApiError { +func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError { memoryBackend.mutex.Lock() defer memoryBackend.mutex.Unlock() @@ -81,10 +81,10 @@ func (memoryBackend MemoryBackend) Logout(token string) *matrix.ApiError { } } - return NewError(matrix.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct + return NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct } -func (memoryBackend MemoryBackend) Sync(token string, request matrix.SyncRequest) (response *matrix.SyncReply, err *matrix.ApiError) { +func (memoryBackend MemoryBackend) Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError) { memoryBackend.mutex.Lock() defer memoryBackend.mutex.Unlock() @@ -92,29 +92,29 @@ func (memoryBackend MemoryBackend) Sync(token string, request matrix.SyncRequest if !first { log.Println(1) - response = &matrix.SyncReply{ - AccountData: matrix.AccountData{ - Events: []matrix.Event{ - matrix.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)}, + response = &models.SyncReply{ + AccountData: models.AccountData{ + Events: []models.Event{ + models.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)}, }}, - Rooms: matrix.RoomsSyncReply{ - Join: map[string]matrix.JoinedRoom{ - "!room1:localhost": matrix.JoinedRoom{ - Timeline: matrix.Timeline{ - Events: []matrix.RoomEvent{ - matrix.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"}, - matrix.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)}, + Rooms: models.RoomsSyncReply{ + Join: map[string]models.JoinedRoom{ + "!room1:localhost": models.JoinedRoom{ + Timeline: models.Timeline{ + Events: []models.RoomEvent{ + models.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"}, + models.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)}, }}}}}} - /* InviteState: matrix.InviteState{ - Events: []matrix.StrippedState{ - matrix.StrippedState{Type: "m.room.join_rules", Content: json.RawMessage(`join_rule:"invite"`), Sender: "@vasyo2:" + server.Address}, - matrix.StrippedState{Type: "m.room.member", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`), Sender: "@vasyo2:" + server.Address}, - matrix.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address}, + /* InviteState: models.InviteState{ + Events: []models.StrippedState{ + models.StrippedState{Type: "m.room.join_rules", Content: json.RawMessage(`join_rule:"invite"`), Sender: "@vasyo2:" + server.Address}, + models.StrippedState{Type: "m.room.member", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`), Sender: "@vasyo2:" + server.Address}, + models.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address}, }}}}}}*/ first = true } else { os.Exit(0) - response = &matrix.SyncReply{} + response = &models.SyncReply{} } return response, nil // TODO: implement