Add set romm topic method for memory backend

This commit is contained in:
nxshock 2019-07-21 16:04:24 +05:00
parent 0dde8e270c
commit cbb1befd60
3 changed files with 52 additions and 0 deletions

View File

@ -22,6 +22,7 @@ type Room interface {
AliasName() string
Name() string
Topic() string
SetTopic(user User, topic string) *models.ApiError
Events() []rooms.Event
}

View File

@ -3,6 +3,8 @@ package memory
import (
"sync"
"github.com/nxshock/signaller/internal/models"
"github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/models/createroom"
"github.com/nxshock/signaller/internal/models/rooms"
@ -77,3 +79,16 @@ func (room *Room) NewEvent(event rooms.Event) {
room.events = append(room.events, event)
}
func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError {
room.mutex.Lock()
defer room.mutex.Unlock()
if room.creator.ID() != user.ID() { // TODO: currently only creator can change topic
return internal.NewError(models.M_FORBIDDEN, "")
}
room.topic = topic
return nil
}

View File

@ -62,3 +62,39 @@ func TestCreateAlreadyExistingRoom(t *testing.T) {
_, err = backend.CreateRoom(user, request)
assert.NotNil(t, err)
}
func TestSetRoomTopic(t *testing.T) {
backend := NewBackend("localhost")
user, _, _ := backend.Register("user1", "", "")
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1",
Topic: "topic"}
room, _ := backend.CreateRoom(user, request)
var newTopic = "new topic"
err := room.SetTopic(user, newTopic)
assert.Nil(t, err)
assert.Equal(t, newTopic, room.Topic())
}
func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {
backend := NewBackend("localhost")
creator, _, _ := backend.Register("user1", "", "")
user2, _, _ := backend.Register("user2", "", "")
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1",
Topic: "topic"}
room, _ := backend.CreateRoom(creator, request)
var newTopic = "new topic"
err := room.SetTopic(user2, newTopic)
assert.NotNil(t, err)
}