From 9c98ec1cf35e600d44c3dc990d3e44f37e74802d Mon Sep 17 00:00:00 2001 From: nxshock Date: Mon, 22 Jul 2019 18:38:02 +0500 Subject: [PATCH] Move changing room topic to user method --- internal/backend.go | 2 +- internal/backends/memory/rooms.go | 21 --------------------- internal/backends/memory/rooms_test.go | 4 ++-- internal/backends/memory/user.go | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 2911abe..a4726e1 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -21,7 +21,6 @@ type Room interface { AliasName() string Name() string Topic() string - SetTopic(user User, topic string) *models.ApiError Events() []rooms.Event } @@ -30,4 +29,5 @@ type User interface { ID() string Password() string CreateRoom(request createroom.Request) (Room, *models.ApiError) + SetTopic(room Room, topic string) *models.ApiError } diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index 1429ef4..9349b2d 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -2,9 +2,6 @@ package memory import ( "sync" - "time" - - "github.com/nxshock/signaller/internal/models" "github.com/nxshock/signaller/internal" "github.com/nxshock/signaller/internal/models/createroom" @@ -78,21 +75,3 @@ func (room *Room) Creator() internal.User { return room.creator } - -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 - room.events = append(room.events, RoomEvent{ - Type: rooms.Topic, - Sender: user, - OriginServerTS: time.Now(), - Room: room}) - - return nil -} diff --git a/internal/backends/memory/rooms_test.go b/internal/backends/memory/rooms_test.go index dca5937..d9f5885 100644 --- a/internal/backends/memory/rooms_test.go +++ b/internal/backends/memory/rooms_test.go @@ -76,7 +76,7 @@ func TestSetRoomTopic(t *testing.T) { room, _ := user.CreateRoom(request) var newTopic = "new topic" - err := room.SetTopic(user, newTopic) + err := user.SetTopic(room, newTopic) assert.Nil(t, err) assert.Equal(t, newTopic, room.Topic()) assert.Equal(t, 5, len(room.Events())) // TODO: check start event count @@ -96,6 +96,6 @@ func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { room, _ := creator.CreateRoom(request) var newTopic = "new topic" - err := room.SetTopic(user2, newTopic) + err := user2.SetTopic(room, newTopic) assert.NotNil(t, err) } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 8f4487e..2726ac7 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -93,3 +93,21 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models return room, nil } + +func (user *User) SetTopic(room internal.Room, topic string) *models.ApiError { + room.(*Room).mutex.Lock() + defer room.(*Room).mutex.Unlock() + + if room.(*Room).creator.ID() != user.ID() { // TODO: currently only creator can change topic + return internal.NewError(models.M_FORBIDDEN, "") + } + + room.(*Room).topic = topic + room.(*Room).events = append(room.(*Room).events, RoomEvent{ + Type: rooms.Topic, + Sender: user, + OriginServerTS: time.Now(), + Room: room}) + + return nil +}