From a6f8ef9bdb8d80c8301476c4bdc43a57eb6a9d1c Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 21 Jul 2019 16:52:32 +0500 Subject: [PATCH] Move CreateRoom to User method --- internal/backend.go | 2 +- internal/backends/memory/backend.go | 20 -------------- internal/backends/memory/consts.go | 5 +++- internal/backends/memory/rooms_test.go | 12 ++++----- internal/backends/memory/user.go | 37 ++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 3de33c6..2911abe 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -11,7 +11,6 @@ type Backend interface { Register(username, password, device string) (user User, token string, err *models.ApiError) Login(username, password, device string) (token string, err *models.ApiError) Logout(token string) *models.ApiError - CreateRoom(user User, request createroom.Request) (Room, *models.ApiError) Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError) } @@ -30,4 +29,5 @@ type User interface { Name() string ID() string Password() string + CreateRoom(request createroom.Request) (Room, *models.ApiError) } diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index e87b820..51b0590 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -5,7 +5,6 @@ import ( "github.com/nxshock/signaller/internal" "github.com/nxshock/signaller/internal/models" - "github.com/nxshock/signaller/internal/models/createroom" mSync "github.com/nxshock/signaller/internal/models/sync" ) @@ -92,22 +91,3 @@ func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response return nil, nil // TODO: implement } - -func (backend *Backend) CreateRoom(user internal.User, request createroom.Request) (internal.Room, *models.ApiError) { - for _, existingRoom := range backend.rooms { - if existingRoom.AliasName() == request.RoomAliasName { // TODO: strip and check request room alias name before use - return nil, internal.NewError(models.M_ROOM_IN_USE, "") - } - } - - room := &Room{ - id: internal.NewToken(groupIDSize), - aliasName: request.RoomAliasName, - name: request.Name, - topic: request.Topic, - creator: user} - - backend.rooms[room.id] = room - - return room, nil -} diff --git a/internal/backends/memory/consts.go b/internal/backends/memory/consts.go index 0dd3bb3..2af747d 100644 --- a/internal/backends/memory/consts.go +++ b/internal/backends/memory/consts.go @@ -1,3 +1,6 @@ package memory -const groupIDSize = 16 +const ( + groupIDSize = 16 + eventIDSize = 16 +) diff --git a/internal/backends/memory/rooms_test.go b/internal/backends/memory/rooms_test.go index 5ee9135..6a8821e 100644 --- a/internal/backends/memory/rooms_test.go +++ b/internal/backends/memory/rooms_test.go @@ -37,7 +37,7 @@ func TestCreateRoom(t *testing.T) { Name: "room1", Topic: "topic"} - room, err := backend.CreateRoom(user, request) + room, err := user.CreateRoom(request) assert.Nil(t, err) assert.Equal(t, request.RoomAliasName, room.AliasName()) assert.Equal(t, request.Name, room.Name()) @@ -56,10 +56,10 @@ func TestCreateAlreadyExistingRoom(t *testing.T) { Name: "room1", Topic: "topic"} - _, err := backend.CreateRoom(user, request) + _, err := user.CreateRoom(request) assert.Nil(t, err) - _, err = backend.CreateRoom(user, request) + _, err = user.CreateRoom(request) assert.NotNil(t, err) } @@ -73,13 +73,13 @@ func TestSetRoomTopic(t *testing.T) { Name: "room1", Topic: "topic"} - room, _ := backend.CreateRoom(user, request) + room, _ := user.CreateRoom(request) var newTopic = "new topic" err := room.SetTopic(user, newTopic) assert.Nil(t, err) assert.Equal(t, newTopic, room.Topic()) - assert.Equal(t, 1, len(room.Events())) + assert.Equal(t, 2, len(room.Events())) // create event + topic event == 2 } func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { @@ -93,7 +93,7 @@ func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { Name: "room1", Topic: "topic"} - room, _ := backend.CreateRoom(creator, request) + room, _ := creator.CreateRoom(request) var newTopic = "new topic" err := room.SetTopic(user2, newTopic) diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 3a87bd9..5500b90 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -1,5 +1,14 @@ package memory +import ( + "time" + + "github.com/nxshock/signaller/internal" + "github.com/nxshock/signaller/internal/models" + "github.com/nxshock/signaller/internal/models/createroom" + "github.com/nxshock/signaller/internal/models/rooms" +) + type User struct { name string password string @@ -19,3 +28,31 @@ func (user *User) Name() string { func (user *User) Password() string { return user.password } + +func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models.ApiError) { + for _, existingRoom := range user.backend.rooms { + if existingRoom.AliasName() == request.RoomAliasName { // TODO: strip and check request room alias name before use + return nil, internal.NewError(models.M_ROOM_IN_USE, "") + } + } + + room := &Room{ + id: internal.NewToken(groupIDSize), + aliasName: request.RoomAliasName, + name: request.Name, + topic: request.Topic, + events: []RoomEvent{ + RoomEvent{ + Content: nil, + Type: rooms.Create, + EventID: internal.NewToken(eventIDSize), + Sender: user, + OriginServerTS: time.Now()}}, + creator: user} + + room.events[0].Room = room + + user.backend.rooms[room.id] = room + + return room, nil +}