From cbb1befd60a64cb72086b196c1fc18c30ceff550 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 21 Jul 2019 16:04:24 +0500 Subject: [PATCH] Add set romm topic method for memory backend --- internal/backend.go | 1 + internal/backends/memory/rooms.go | 15 +++++++++++ internal/backends/memory/rooms_test.go | 36 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/internal/backend.go b/internal/backend.go index e64c8ad..3de33c6 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -22,6 +22,7 @@ type Room interface { AliasName() string Name() string Topic() string + SetTopic(user User, topic string) *models.ApiError Events() []rooms.Event } diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index f0c6615..3b21e19 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -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 +} diff --git a/internal/backends/memory/rooms_test.go b/internal/backends/memory/rooms_test.go index a844355..8d9dc33 100644 --- a/internal/backends/memory/rooms_test.go +++ b/internal/backends/memory/rooms_test.go @@ -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) +}