From a1717ec7843251ddb12647ba02d1a3581677c5b1 Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 23 Jul 2019 19:24:18 +0500 Subject: [PATCH] Add send message event to memry backend --- internal/backend.go | 1 + internal/backends/memory/user.go | 26 ++++++++++++++++++ internal/backends/memory/user_test.go | 39 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/internal/backend.go b/internal/backend.go index b546b40..e4136df 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -31,4 +31,5 @@ type User interface { CreateRoom(request createroom.Request) (Room, *models.ApiError) LeaveRoom(room Room) *models.ApiError SetTopic(room Room, topic string) *models.ApiError + SendMessage(room Room, text string) *models.ApiError } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 2c1eb8b..25dfe79 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -128,3 +128,29 @@ func (user *User) LeaveRoom(room internal.Room) *models.ApiError { return internal.NewError(models.M_BAD_STATE, "you are not a member of group") // TODO: check error code } + +func (user *User) SendMessage(room internal.Room, text string) *models.ApiError { + room.(*Room).mutex.Lock() + defer room.(*Room).mutex.Unlock() + + userInRoom := false + for _, roomMember := range room.(*Room).joined { + if roomMember.ID() == user.ID() { + userInRoom = true + } + } + + if !userInRoom { + return internal.NewError(models.M_FORBIDDEN, "") + } + + room.(*Room).events = append(room.(*Room).events, RoomEvent{ + Content: nil, + Type: rooms.Message, + EventID: newToken(defaultTokenSize), + Sender: user, + OriginServerTS: time.Now(), + Room: room}) + + return nil +} diff --git a/internal/backends/memory/user_test.go b/internal/backends/memory/user_test.go index af410b6..f56251b 100644 --- a/internal/backends/memory/user_test.go +++ b/internal/backends/memory/user_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/nxshock/signaller/internal/models/createroom" ) func TestUserID(t *testing.T) { @@ -19,3 +21,40 @@ func TestUserID(t *testing.T) { assert.Equal(t, expectedUserID, user.ID()) } + +func TestUserMessage(t *testing.T) { + backend := NewBackend("localhost") + + user, _, err := backend.Register("user1", "", "") + assert.Nil(t, err) + + request := createroom.Request{ + RoomAliasName: "room1", + Name: "room1"} + + room, err := user.CreateRoom(request) + assert.Nil(t, err) + + err = user.SendMessage(room, "hello") + assert.Nil(t, err) +} + +func TestUserMessageInWrongRoom(t *testing.T) { + backend := NewBackend("localhost") + + user1, _, err := backend.Register("user1", "", "") + assert.Nil(t, err) + + request := createroom.Request{ + RoomAliasName: "room1", + Name: "room1"} + + room, err := user1.CreateRoom(request) + assert.Nil(t, err) + + user2, _, err := backend.Register("user2", "", "") + assert.Nil(t, err) + + err = user2.SendMessage(room, "hello") + assert.NotNil(t, err) +}