From 57a2beceb229bb5d86b2b6f71194e5256a83af92 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 7 Aug 2019 13:07:11 +0400 Subject: [PATCH] refactor: Make events as one collection in Backend --- internal/backend.go | 3 ++- internal/backends/memory/backend.go | 18 ++++++++++++++++++ internal/backends/memory/rooms.go | 15 --------------- internal/backends/memory/user.go | 22 +++++++++++++++------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 3861e7f..c473317 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -18,6 +18,8 @@ type Backend interface { Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err models.ApiError) PublicRooms(filter string) []Room ValidateUsernameFunc() func(string) error + GetEventByID(id string) rooms.Event + PutEvent(rooms.Event) error } type Room interface { @@ -27,7 +29,6 @@ type Room interface { AliasName() string Name() string Topic() string - Events() []rooms.Event Visibility() createroom.VisibilityType WorldReadable() bool GuestCanJoin() bool diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index fdc0600..ac91f00 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -11,12 +11,14 @@ import ( "github.com/signaller-matrix/signaller/internal/models" "github.com/signaller-matrix/signaller/internal/models/common" "github.com/signaller-matrix/signaller/internal/models/createroom" + "github.com/signaller-matrix/signaller/internal/models/rooms" mSync "github.com/signaller-matrix/signaller/internal/models/sync" ) type Backend struct { data map[string]internal.User rooms map[string]internal.Room + events map[string]rooms.Event hostname string validateUsernameFunc func(string) error // TODO: create ability to redefine validation func mutex sync.RWMutex @@ -164,3 +166,19 @@ func defaultValidationUsernameFunc(userName string) error { return nil } + +func (backend *Backend) GetEventByID(id string) rooms.Event { + backend.mutex.RLock() + defer backend.mutex.RUnlock() + + return backend.events[id] +} + +func (backend *Backend) PutEvent(event rooms.Event) error { + backend.mutex.RLock() + defer backend.mutex.RUnlock() + + backend.events[event.EventID] = event + + return nil +} diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index 63c5057..099b286 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -5,7 +5,6 @@ import ( "github.com/signaller-matrix/signaller/internal" "github.com/signaller-matrix/signaller/internal/models/createroom" - "github.com/signaller-matrix/signaller/internal/models/rooms" ) type Room struct { @@ -23,8 +22,6 @@ type Room struct { joined []internal.User invites []internal.User - events []RoomEvent - server *Backend mutex sync.RWMutex @@ -65,18 +62,6 @@ func (room *Room) Users() []internal.User { return room.joined } -func (room *Room) Events() []rooms.Event { - room.mutex.RLock() - defer room.mutex.RUnlock() - - result := make([]rooms.Event, 0) - for _, v := range room.events { - result = append(result, v.ToEvent()) - } - - return result -} - func (room *Room) Visibility() createroom.VisibilityType { room.mutex.RLock() defer room.mutex.RUnlock() diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index a7ef3a5..8537556 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -89,18 +89,21 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models. aliasName: request.RoomAliasName, name: request.Name, topic: request.Topic, - events: events, creator: user, joined: []internal.User{user}, visibility: request.Visibility, server: user.backend, state: request.Preset} - for i, _ := range room.events { - room.events[i].Room = room + for i, _ := range events { + events[i].Room = room //v.Room = room } + for i, _ := range events { + user.backend.PutEvent(events[i].ToEvent()) + } + user.backend.rooms[room.ID()] = room return room, nil @@ -115,11 +118,14 @@ func (user *User) SetTopic(room internal.Room, topic string) models.ApiError { } room.(*Room).topic = topic - room.(*Room).events = append(room.(*Room).events, RoomEvent{ + + rEvent := &RoomEvent{ Type: rooms.Topic, Sender: user, OriginServerTS: time.Now(), - Room: room}) + Room: room} + + user.backend.PutEvent(rEvent.ToEvent()) return nil } @@ -189,13 +195,15 @@ func (user *User) SendMessage(room internal.Room, text string) models.ApiError { return models.NewError(models.M_FORBIDDEN, "") } - room.(*Room).events = append(room.(*Room).events, RoomEvent{ + rEvent := &RoomEvent{ Content: nil, Type: rooms.Message, EventID: internal.RandomString(defaultTokenSize), Sender: user, OriginServerTS: time.Now(), - Room: room}) + Room: room} + + user.backend.PutEvent(rEvent.ToEvent()) return nil }