mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-09 12:11:03 +00:00
Fix room aliases
This commit is contained in:
parent
f8b9591af0
commit
3bbb8b19a8
@ -28,6 +28,7 @@ type Room interface {
|
|||||||
Creator() User
|
Creator() User
|
||||||
Users() []User
|
Users() []User
|
||||||
AliasName() string
|
AliasName() string
|
||||||
|
Aliases() []string
|
||||||
Name() string
|
Name() string
|
||||||
Topic() string
|
Topic() string
|
||||||
Visibility() createroom.VisibilityType
|
Visibility() createroom.VisibilityType
|
||||||
|
@ -156,8 +156,7 @@ func (backend *Backend) GetRoomByAlias(alias string) internal.Room {
|
|||||||
backend.mutex.RLock()
|
backend.mutex.RLock()
|
||||||
defer backend.mutex.RUnlock()
|
defer backend.mutex.RUnlock()
|
||||||
|
|
||||||
alias = strings.TrimPrefix(alias, "#") // TODO: create strip alias func
|
alias = internal.StripAlias(backend.hostname, alias)
|
||||||
alias = strings.TrimSuffix(alias, ":"+backend.hostname)
|
|
||||||
|
|
||||||
if room, exists := backend.roomAliases[alias]; exists {
|
if room, exists := backend.roomAliases[alias]; exists {
|
||||||
return room
|
return room
|
||||||
|
@ -48,6 +48,20 @@ func (room *Room) AliasName() string {
|
|||||||
return room.aliasName
|
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 {
|
func (room *Room) Topic() string {
|
||||||
room.mutex.RLock()
|
room.mutex.RLock()
|
||||||
defer room.mutex.RUnlock()
|
defer room.mutex.RUnlock()
|
||||||
|
@ -572,7 +572,7 @@ func roomAliasHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
room := currServer.Backend.GetRoomByAlias(roomAlias)
|
room := currServer.Backend.GetRoomByAlias(StripAlias(currServer.Address, roomAlias))
|
||||||
if room == nil {
|
if room == nil {
|
||||||
errorResponse(w, models.M_NOT_FOUND, http.StatusNotFound, "room not found")
|
errorResponse(w, models.M_NOT_FOUND, http.StatusNotFound, "room not found")
|
||||||
return
|
return
|
||||||
@ -597,7 +597,7 @@ func roomAliasHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
response = struct{}{}
|
response = struct{}{}
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
err := user.DeleteRoomAlias(roomAlias)
|
err := user.DeleteRoomAlias(StripAlias(currServer.Address, roomAlias))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorResponse(w, err, http.StatusConflict, "") // TODO: check http code
|
errorResponse(w, err, http.StatusConflict, "") // TODO: check http code
|
||||||
return
|
return
|
||||||
|
@ -1,13 +1,28 @@
|
|||||||
package internal
|
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 {
|
func roomsToPublicRoomsChunks(rooms []Room) []publicrooms.PublicRoomsChunk {
|
||||||
var chunks []publicrooms.PublicRoomsChunk
|
var chunks []publicrooms.PublicRoomsChunk
|
||||||
|
|
||||||
for _, room := range rooms {
|
for _, room := range rooms {
|
||||||
chunk := publicrooms.PublicRoomsChunk{
|
chunk := publicrooms.PublicRoomsChunk{
|
||||||
// TODO: Aliases:
|
Aliases: room.Aliases(),
|
||||||
CanonicalAlias: room.AliasName(),
|
CanonicalAlias: room.AliasName(),
|
||||||
Name: room.Name(),
|
Name: room.Name(),
|
||||||
NumJoinedMembers: len(room.Users()),
|
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