Move CreateRoom to User method

This commit is contained in:
nxshock 2019-07-21 16:52:32 +05:00
parent feb71433c4
commit a6f8ef9bdb
5 changed files with 48 additions and 28 deletions

View File

@ -11,7 +11,6 @@ type Backend interface {
Register(username, password, device string) (user User, token string, err *models.ApiError) Register(username, password, device string) (user User, token string, err *models.ApiError)
Login(username, password, device string) (token string, err *models.ApiError) Login(username, password, device string) (token string, err *models.ApiError)
Logout(token string) *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) Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
} }
@ -30,4 +29,5 @@ type User interface {
Name() string Name() string
ID() string ID() string
Password() string Password() string
CreateRoom(request createroom.Request) (Room, *models.ApiError)
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/nxshock/signaller/internal" "github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/models" "github.com/nxshock/signaller/internal/models"
"github.com/nxshock/signaller/internal/models/createroom"
mSync "github.com/nxshock/signaller/internal/models/sync" 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 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
}

View File

@ -1,3 +1,6 @@
package memory package memory
const groupIDSize = 16 const (
groupIDSize = 16
eventIDSize = 16
)

View File

@ -37,7 +37,7 @@ func TestCreateRoom(t *testing.T) {
Name: "room1", Name: "room1",
Topic: "topic"} Topic: "topic"}
room, err := backend.CreateRoom(user, request) room, err := user.CreateRoom(request)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, request.RoomAliasName, room.AliasName()) assert.Equal(t, request.RoomAliasName, room.AliasName())
assert.Equal(t, request.Name, room.Name()) assert.Equal(t, request.Name, room.Name())
@ -56,10 +56,10 @@ func TestCreateAlreadyExistingRoom(t *testing.T) {
Name: "room1", Name: "room1",
Topic: "topic"} Topic: "topic"}
_, err := backend.CreateRoom(user, request) _, err := user.CreateRoom(request)
assert.Nil(t, err) assert.Nil(t, err)
_, err = backend.CreateRoom(user, request) _, err = user.CreateRoom(request)
assert.NotNil(t, err) assert.NotNil(t, err)
} }
@ -73,13 +73,13 @@ func TestSetRoomTopic(t *testing.T) {
Name: "room1", Name: "room1",
Topic: "topic"} Topic: "topic"}
room, _ := backend.CreateRoom(user, request) room, _ := user.CreateRoom(request)
var newTopic = "new topic" var newTopic = "new topic"
err := room.SetTopic(user, newTopic) err := room.SetTopic(user, newTopic)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, newTopic, room.Topic()) 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) { func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {
@ -93,7 +93,7 @@ func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {
Name: "room1", Name: "room1",
Topic: "topic"} Topic: "topic"}
room, _ := backend.CreateRoom(creator, request) room, _ := creator.CreateRoom(request)
var newTopic = "new topic" var newTopic = "new topic"
err := room.SetTopic(user2, newTopic) err := room.SetTopic(user2, newTopic)

View File

@ -1,5 +1,14 @@
package memory 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 { type User struct {
name string name string
password string password string
@ -19,3 +28,31 @@ func (user *User) Name() string {
func (user *User) Password() string { func (user *User) Password() string {
return user.password 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
}