mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-05 02:01:03 +00:00
Fix room aliases
This commit is contained in:
parent
f8b9591af0
commit
3bbb8b19a8
@ -28,6 +28,7 @@ type Room interface {
|
||||
Creator() User
|
||||
Users() []User
|
||||
AliasName() string
|
||||
Aliases() []string
|
||||
Name() string
|
||||
Topic() string
|
||||
Visibility() createroom.VisibilityType
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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()),
|
||||
|
34
internal/utils_test.go
Normal file
34
internal/utils_test.go
Normal file
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user