mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-22 02:12:20 +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)
|
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
|
||||||
GetRoomByAlias(string) Room
|
GetRoomByAlias(string) Room
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +30,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
|
||||||
|
@ -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
|
||||||
roomAliases map[string]internal.Room
|
roomAliases map[string]internal.Room
|
||||||
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
|
||||||
@ -180,3 +182,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
|
||||||
|
}
|
||||||
|
@ -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()
|
||||||
|
@ -90,18 +90,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
|
||||||
@ -116,11 +119,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
|
||||||
}
|
}
|
||||||
@ -190,13 +196,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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user