From 585ccbc332980b0149055459bc5da2efb0ccc016 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 11 Aug 2019 18:53:18 +0500 Subject: [PATCH] Move Sync method to user method --- internal/backend.go | 2 +- internal/backends/memory/backend.go | 8 -------- internal/backends/memory/user.go | 5 +++++ internal/handlers.go | 12 +++++++++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index ac9f4f9..52b7b92 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -15,7 +15,6 @@ type Backend interface { 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) PublicRooms(filter string) []Room ValidateUsernameFunc() func(string) error GetEventByID(id string) events.Event @@ -59,4 +58,5 @@ type User interface { GetFilterByID(filterID string) *common.Filter AddRoomAlias(Room, string) models.ApiError DeleteRoomAlias(string) models.ApiError + Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err models.ApiError) } diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index e365a88..be03636 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -13,7 +13,6 @@ import ( "github.com/signaller-matrix/signaller/internal/models/common" "github.com/signaller-matrix/signaller/internal/models/createroom" "github.com/signaller-matrix/signaller/internal/models/events" - mSync "github.com/signaller-matrix/signaller/internal/models/sync" "github.com/wangjia184/sortedset" ) @@ -89,13 +88,6 @@ func (backend *Backend) Login(username, password, device string) (user internal. return user, token, nil } -func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err models.ApiError) { - backend.mutex.Lock() - defer backend.mutex.Unlock() - - return nil, nil // TODO: implement -} - func (backend *Backend) GetUserByToken(token string) internal.User { backend.mutex.RLock() defer backend.mutex.RUnlock() diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 7c63660..ced850a 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -11,6 +11,7 @@ import ( "github.com/signaller-matrix/signaller/internal/models/createroom" "github.com/signaller-matrix/signaller/internal/models/devices" "github.com/signaller-matrix/signaller/internal/models/events" + mSync "github.com/signaller-matrix/signaller/internal/models/sync" ) type User struct { @@ -351,3 +352,7 @@ func (user *User) GetFilterByID(filterID string) *common.Filter { return nil } + +func (user *User) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err models.ApiError) { + return nil, nil +} diff --git a/internal/handlers.go b/internal/handlers.go index 4b1c417..e282b58 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -281,18 +281,24 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) { timeout, err := strconv.Atoi(r.FormValue("timeout")) if err != nil { - errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes") + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failed") return } request.Timeout = timeout token := getTokenFromResponse(r) if token == "" { - errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "") + errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "") return } - response, _ := currServer.Backend.Sync(token, request) // TODO: handle error + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + response, _ := user.Sync(token, request) // TODO: handle error response.NextBatch = "123" response.Rooms = mSync.RoomsSyncReply{}