refactor: Make events as one collection in Backend

This commit is contained in:
ChronosX88 2019-08-07 13:07:11 +04:00
parent ab108855c4
commit 57a2beceb2
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 35 additions and 23 deletions

View File

@ -18,6 +18,8 @@ type Backend interface {
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err models.ApiError) Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err models.ApiError)
PublicRooms(filter string) []Room PublicRooms(filter string) []Room
ValidateUsernameFunc() func(string) error ValidateUsernameFunc() func(string) error
GetEventByID(id string) rooms.Event
PutEvent(rooms.Event) error
} }
type Room interface { type Room interface {
@ -27,7 +29,6 @@ type Room interface {
AliasName() string AliasName() string
Name() string Name() string
Topic() string Topic() string
Events() []rooms.Event
Visibility() createroom.VisibilityType Visibility() createroom.VisibilityType
WorldReadable() bool WorldReadable() bool
GuestCanJoin() bool GuestCanJoin() bool

View File

@ -11,12 +11,14 @@ import (
"github.com/signaller-matrix/signaller/internal/models" "github.com/signaller-matrix/signaller/internal/models"
"github.com/signaller-matrix/signaller/internal/models/common" "github.com/signaller-matrix/signaller/internal/models/common"
"github.com/signaller-matrix/signaller/internal/models/createroom" "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" mSync "github.com/signaller-matrix/signaller/internal/models/sync"
) )
type Backend struct { type Backend struct {
data map[string]internal.User data map[string]internal.User
rooms map[string]internal.Room rooms map[string]internal.Room
events map[string]rooms.Event
hostname string hostname string
validateUsernameFunc func(string) error // TODO: create ability to redefine validation func validateUsernameFunc func(string) error // TODO: create ability to redefine validation func
mutex sync.RWMutex mutex sync.RWMutex
@ -164,3 +166,19 @@ func defaultValidationUsernameFunc(userName string) error {
return nil 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
}

View File

@ -5,7 +5,6 @@ import (
"github.com/signaller-matrix/signaller/internal" "github.com/signaller-matrix/signaller/internal"
"github.com/signaller-matrix/signaller/internal/models/createroom" "github.com/signaller-matrix/signaller/internal/models/createroom"
"github.com/signaller-matrix/signaller/internal/models/rooms"
) )
type Room struct { type Room struct {
@ -23,8 +22,6 @@ type Room struct {
joined []internal.User joined []internal.User
invites []internal.User invites []internal.User
events []RoomEvent
server *Backend server *Backend
mutex sync.RWMutex mutex sync.RWMutex
@ -65,18 +62,6 @@ func (room *Room) Users() []internal.User {
return room.joined 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 { func (room *Room) Visibility() createroom.VisibilityType {
room.mutex.RLock() room.mutex.RLock()
defer room.mutex.RUnlock() defer room.mutex.RUnlock()

View File

@ -89,18 +89,21 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
aliasName: request.RoomAliasName, aliasName: request.RoomAliasName,
name: request.Name, name: request.Name,
topic: request.Topic, topic: request.Topic,
events: events,
creator: user, creator: user,
joined: []internal.User{user}, joined: []internal.User{user},
visibility: request.Visibility, visibility: request.Visibility,
server: user.backend, server: user.backend,
state: request.Preset} state: request.Preset}
for i, _ := range room.events { for i, _ := range events {
room.events[i].Room = room events[i].Room = room
//v.Room = room //v.Room = room
} }
for i, _ := range events {
user.backend.PutEvent(events[i].ToEvent())
}
user.backend.rooms[room.ID()] = room user.backend.rooms[room.ID()] = room
return room, nil return room, nil
@ -115,11 +118,14 @@ func (user *User) SetTopic(room internal.Room, topic string) models.ApiError {
} }
room.(*Room).topic = topic room.(*Room).topic = topic
room.(*Room).events = append(room.(*Room).events, RoomEvent{
rEvent := &RoomEvent{
Type: rooms.Topic, Type: rooms.Topic,
Sender: user, Sender: user,
OriginServerTS: time.Now(), OriginServerTS: time.Now(),
Room: room}) Room: room}
user.backend.PutEvent(rEvent.ToEvent())
return nil return nil
} }
@ -189,13 +195,15 @@ func (user *User) SendMessage(room internal.Room, text string) models.ApiError {
return models.NewError(models.M_FORBIDDEN, "") return models.NewError(models.M_FORBIDDEN, "")
} }
room.(*Room).events = append(room.(*Room).events, RoomEvent{ rEvent := &RoomEvent{
Content: nil, Content: nil,
Type: rooms.Message, Type: rooms.Message,
EventID: internal.RandomString(defaultTokenSize), EventID: internal.RandomString(defaultTokenSize),
Sender: user, Sender: user,
OriginServerTS: time.Now(), OriginServerTS: time.Now(),
Room: room}) Room: room}
user.backend.PutEvent(rEvent.ToEvent())
return nil return nil
} }