From 7293b33b6f7865aed3880170558697f7519a4143 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 24 Jul 2019 20:04:45 +0500 Subject: [PATCH] Add user.JoinedRooms() method --- README.md | 1 + internal/backend.go | 1 + internal/backends/memory/user.go | 20 ++++++++++++++- internal/backends/memory/user_test.go | 20 +++++++++++++++ internal/handlers.go | 29 ++++++++++++++++++++++ internal/models/joinedrooms/joinedrooms.go | 6 +++++ internal/server.go | 1 + 7 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 internal/models/joinedrooms/joinedrooms.go diff --git a/README.md b/README.md index de5a7ab..0a82b24 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,4 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [x] [5.4.3 POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout) - [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.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami) +- [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms) diff --git a/internal/backend.go b/internal/backend.go index 3946f6c..5dbd64f 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -32,6 +32,7 @@ type User interface { LeaveRoom(room Room) *models.ApiError SetTopic(room Room, topic string) *models.ApiError SendMessage(room Room, text string) *models.ApiError + JoinedRooms() []Room Logout(token string) LogoutAll() } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 050fcf4..b220d8d 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -85,7 +85,8 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models topic: request.Topic, events: events, creator: user, - joined: []internal.User{user}} + joined: []internal.User{user}, + server: user.backend} for i, _ := range room.events { room.events[i].Room = room @@ -155,6 +156,23 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError return nil } +func (user *User) JoinedRooms() []internal.Room { + user.backend.mutex.Lock() + defer user.backend.mutex.Unlock() + + var result []internal.Room + + for _, room := range user.backend.rooms { + for _, user := range room.(*Room).joined { + if user.ID() == user.ID() { + result = append(result, room) + } + } + } + + return result +} + func (user *User) Logout(token string) { delete(user.Tokens, token) } diff --git a/internal/backends/memory/user_test.go b/internal/backends/memory/user_test.go index 5db6e01..820269e 100644 --- a/internal/backends/memory/user_test.go +++ b/internal/backends/memory/user_test.go @@ -3,6 +3,8 @@ package memory import ( "testing" + "github.com/nxshock/signaller/internal" + "github.com/stretchr/testify/assert" "github.com/nxshock/signaller/internal/models/createroom" @@ -98,3 +100,21 @@ func TestLogoutWithWrongToken(t *testing.T) { user.Logout("worng token") } + +func TestJoinedRooms(t *testing.T) { + backend := NewBackend("localhost") + + user, _, err := backend.Register("user1", "", "") + assert.Nil(t, err) + + request := createroom.Request{ + RoomAliasName: "room1", + Name: "room1", + Topic: "topic"} + + room, err := user.CreateRoom(request) + assert.Nil(t, err) + + rooms := user.JoinedRooms() + assert.Equal(t, []internal.Room{room}, rooms) +} diff --git a/internal/handlers.go b/internal/handlers.go index 95ebbec..c6807df 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -11,6 +11,7 @@ import ( "github.com/nxshock/signaller/internal/models/common" "github.com/nxshock/signaller/internal/models" + "github.com/nxshock/signaller/internal/models/joinedrooms" login "github.com/nxshock/signaller/internal/models/login" register "github.com/nxshock/signaller/internal/models/register" mSync "github.com/nxshock/signaller/internal/models/sync" @@ -168,6 +169,34 @@ func WhoAmIHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms +func JoinedRoomsHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + return + } + + token := getTokenFromResponse(r) + if token == "" { + errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "") + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + rooms := user.JoinedRooms() + + var response joinedrooms.Response + for _, room := range rooms { + response.JoinedRooms = append(response.JoinedRooms, room.ID()) + } + + sendJsonResponse(w, http.StatusOK, response) +} + // https://models.org/docs/spec/client_server/latest#get-models-client-r0-sync func SyncHandler(w http.ResponseWriter, r *http.Request) { var request mSync.SyncRequest diff --git a/internal/models/joinedrooms/joinedrooms.go b/internal/models/joinedrooms/joinedrooms.go new file mode 100644 index 0000000..915ef16 --- /dev/null +++ b/internal/models/joinedrooms/joinedrooms.go @@ -0,0 +1,6 @@ +package joinedrooms + +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms +type Response struct { + JoinedRooms []string `json:"joined_rooms"` // Required. The ID of each room in which the user has joined membership. +} diff --git a/internal/server.go b/internal/server.go index 1b02c67..bc10e6d 100644 --- a/internal/server.go +++ b/internal/server.go @@ -24,6 +24,7 @@ func New() *Server { router.HandleFunc("/_matrix/client/r0/logout/all", LogoutAllHandler) router.HandleFunc("/_matrix/client/r0/register", RegisterHandler) router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) + router.HandleFunc("/_matrix/client/r0/joined_rooms", JoinedRoomsHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) router.HandleFunc("/", RootHandler)