From 3bbb8b19a84e5b3b049870f4a57a2be26900747e Mon Sep 17 00:00:00 2001 From: nxshock Date: Thu, 8 Aug 2019 19:38:30 +0500 Subject: [PATCH] Fix room aliases --- internal/backend.go | 1 + internal/backends/memory/backend.go | 3 +-- internal/backends/memory/rooms.go | 14 ++++++++++++ internal/handlers.go | 4 ++-- internal/utils.go | 19 ++++++++++++++-- internal/utils_test.go | 34 +++++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 internal/utils_test.go diff --git a/internal/backend.go b/internal/backend.go index 1bbd926..61b9a07 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -28,6 +28,7 @@ type Room interface { Creator() User Users() []User AliasName() string + Aliases() []string Name() string Topic() string Visibility() createroom.VisibilityType diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index 38dc4cc..c7a3247 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -156,8 +156,7 @@ func (backend *Backend) GetRoomByAlias(alias string) internal.Room { backend.mutex.RLock() defer backend.mutex.RUnlock() - alias = strings.TrimPrefix(alias, "#") // TODO: create strip alias func - alias = strings.TrimSuffix(alias, ":"+backend.hostname) + alias = internal.StripAlias(backend.hostname, alias) if room, exists := backend.roomAliases[alias]; exists { return room diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index 099b286..c027543 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -48,6 +48,20 @@ func (room *Room) AliasName() string { return room.aliasName } +func (room *Room) Aliases() []string { + room.server.mutex.RLock() + defer room.server.mutex.RUnlock() + + var aliases []string + for alias, serverRoom := range room.server.roomAliases { + if serverRoom.ID() == room.ID() { + aliases = append(aliases, "#"+alias+":"+room.server.hostname) + } + } + + return aliases +} + func (room *Room) Topic() string { room.mutex.RLock() defer room.mutex.RUnlock() diff --git a/internal/handlers.go b/internal/handlers.go index ad46de7..4b1c417 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -572,7 +572,7 @@ func roomAliasHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - room := currServer.Backend.GetRoomByAlias(roomAlias) + room := currServer.Backend.GetRoomByAlias(StripAlias(currServer.Address, roomAlias)) if room == nil { errorResponse(w, models.M_NOT_FOUND, http.StatusNotFound, "room not found") return @@ -597,7 +597,7 @@ func roomAliasHandler(w http.ResponseWriter, r *http.Request) { response = struct{}{} case http.MethodDelete: - err := user.DeleteRoomAlias(roomAlias) + err := user.DeleteRoomAlias(StripAlias(currServer.Address, roomAlias)) if err != nil { errorResponse(w, err, http.StatusConflict, "") // TODO: check http code return diff --git a/internal/utils.go b/internal/utils.go index fd91de7..7cd7aa7 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -1,13 +1,28 @@ package internal -import "github.com/signaller-matrix/signaller/internal/models/publicrooms" +import ( + "strings" + + "github.com/signaller-matrix/signaller/internal/models/publicrooms" +) + +func GetCanonicalAlias(hostName string, alias string) string { + return "#" + alias + ":" + hostName +} + +func StripAlias(hostName string, canonicalAlias string) string { + canonicalAlias = strings.TrimPrefix(canonicalAlias, "#") + canonicalAlias = strings.TrimSuffix(canonicalAlias, ":"+hostName) + + return canonicalAlias +} func roomsToPublicRoomsChunks(rooms []Room) []publicrooms.PublicRoomsChunk { var chunks []publicrooms.PublicRoomsChunk for _, room := range rooms { chunk := publicrooms.PublicRoomsChunk{ - // TODO: Aliases: + Aliases: room.Aliases(), CanonicalAlias: room.AliasName(), Name: room.Name(), NumJoinedMembers: len(room.Users()), diff --git a/internal/utils_test.go b/internal/utils_test.go new file mode 100644 index 0000000..35a1b51 --- /dev/null +++ b/internal/utils_test.go @@ -0,0 +1,34 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetCanonicalAlias(t *testing.T) { + tests := []struct { + hostname string + alias string + expected string + }{{"host.com", "alias", "#alias:host.com"}} + + for _, test := range tests { + got := GetCanonicalAlias(test.hostname, test.alias) + assert.Equal(t, test.expected, got) + } +} + +func TestStripAlias(t *testing.T) { + tests := []struct { + hostname string + canonicalAlias string + expected string + }{{"host.com", "#alias:host.com", "alias"}} + + for _, test := range tests { + got := StripAlias(test.hostname, test.canonicalAlias) + assert.Equal(t, test.expected, got) + } + +}