mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-05 02:01:03 +00:00
Merge pull request #9 from signaller-matrix/refactor/eventCol
refactor: Make events as one collection in Backend
This commit is contained in:
commit
bbbaf34301
@ -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
|
||||
GetRoomByAlias(string) Room
|
||||
}
|
||||
|
||||
@ -28,7 +30,6 @@ type Room interface {
|
||||
AliasName() string
|
||||
Name() string
|
||||
Topic() string
|
||||
Events() []rooms.Event
|
||||
Visibility() createroom.VisibilityType
|
||||
WorldReadable() bool
|
||||
GuestCanJoin() bool
|
||||
|
@ -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
|
||||
roomAliases map[string]internal.Room
|
||||
hostname string
|
||||
validateUsernameFunc func(string) error // TODO: create ability to redefine validation func
|
||||
@ -180,3 +182,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
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -90,18 +90,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
|
||||
@ -116,11 +119,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
|
||||
}
|
||||
@ -190,13 +196,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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user