feat: Add basic GetEventsSince method for backend

This commit is contained in:
ChronosX88 2019-08-09 21:02:33 +04:00
parent f119046d58
commit cab885edd8
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
5 changed files with 62 additions and 4 deletions

1
go.mod
View File

@ -5,4 +5,5 @@ go 1.12
require ( require (
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30
) )

2
go.sum
View File

@ -7,3 +7,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30 h1:kZiWylALnUy4kzoKJemjH8eqwCl3RjW1r1ITCjjW7G8=
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=

View File

@ -21,6 +21,7 @@ type Backend interface {
GetEventByID(id string) rooms.Event GetEventByID(id string) rooms.Event
PutEvent(rooms.Event) error PutEvent(rooms.Event) error
GetRoomByAlias(string) Room GetRoomByAlias(string) Room
GetEventsSince(sinceToken string, limit int) []rooms.Event
} }
type Room interface { type Room interface {

View File

@ -6,6 +6,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"time"
"github.com/signaller-matrix/signaller/internal" "github.com/signaller-matrix/signaller/internal"
"github.com/signaller-matrix/signaller/internal/models" "github.com/signaller-matrix/signaller/internal/models"
@ -13,12 +14,13 @@ import (
"github.com/signaller-matrix/signaller/internal/models/createroom" "github.com/signaller-matrix/signaller/internal/models/createroom"
"github.com/signaller-matrix/signaller/internal/models/rooms" "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"
"github.com/wangjia184/sortedset"
) )
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 events *sortedset.SortedSet
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
@ -35,7 +37,7 @@ func NewBackend(hostname string) *Backend {
validateUsernameFunc: defaultValidationUsernameFunc, validateUsernameFunc: defaultValidationUsernameFunc,
rooms: make(map[string]internal.Room), rooms: make(map[string]internal.Room),
roomAliases: make(map[string]internal.Room), roomAliases: make(map[string]internal.Room),
events: make(map[string]rooms.Event), events: sortedset.New(),
data: make(map[string]internal.User)} data: make(map[string]internal.User)}
} }
@ -187,14 +189,57 @@ func (backend *Backend) GetEventByID(id string) rooms.Event {
backend.mutex.RLock() backend.mutex.RLock()
defer backend.mutex.RUnlock() defer backend.mutex.RUnlock()
return backend.events[id] return backend.events.GetByKey(id).Value.(rooms.Event)
} }
func (backend *Backend) PutEvent(event rooms.Event) error { func (backend *Backend) PutEvent(event rooms.Event) error {
backend.mutex.Lock() backend.mutex.Lock()
defer backend.mutex.Unlock() defer backend.mutex.Unlock()
backend.events[event.EventID] = event backend.events.AddOrUpdate(event.EventID, sortedset.SCORE(time.Now().Unix()), event)
return nil return nil
} }
func (backend *Backend) GetEventsSince(user User, sinceToken string, limit int) []rooms.Event {
sinceEventNode := backend.events.GetByKey(sinceToken)
sEvents := backend.events.GetByScoreRange(sinceEventNode.Score(), -1, &sortedset.GetByScoreRangeOptions{
Limit: limit,
})
events := extractEventsFromNodes(sEvents)
var returnEvents []rooms.Event
for _, event := range events {
if isEventRelatedToUser(event, user) {
returnEvents = append(returnEvents, event)
}
}
return returnEvents
}
func extractEventsFromNodes(nodes []*sortedset.SortedSetNode) []rooms.Event {
var events []rooms.Event
for _, e := range nodes {
events = append(events, e.Value.(rooms.Event))
}
return events
}
func isEventRelatedToUser(event rooms.Event, user User) bool {
if internal.InArray(event.RoomID, extractRoomIDsFromModel(user.JoinedRooms())) {
return true
}
return false
}
func extractRoomIDsFromModel(rooms []internal.Room) []string {
var roomIDs []string
for _, room := range rooms {
roomIDs = append(roomIDs, room.ID())
}
return roomIDs
}

View File

@ -37,3 +37,12 @@ func roomsToPublicRoomsChunks(rooms []Room) []publicrooms.PublicRoomsChunk {
return chunks return chunks
} }
func InArray(a string, arr []string) bool {
for _, b := range arr {
if b == a {
return true
}
}
return false
}