signaller/internal/backends/memory/backend.go

246 lines
6.0 KiB
Go
Raw Normal View History

package memory
import (
2019-08-03 07:54:32 +00:00
"fmt"
"regexp"
"sort"
"strings"
"sync"
"time"
2019-08-03 14:19:18 +00:00
"github.com/signaller-matrix/signaller/internal"
"github.com/signaller-matrix/signaller/internal/models"
"github.com/signaller-matrix/signaller/internal/models/common"
2019-08-03 14:19:18 +00:00
"github.com/signaller-matrix/signaller/internal/models/createroom"
"github.com/signaller-matrix/signaller/internal/models/rooms"
2019-08-03 14:19:18 +00:00
mSync "github.com/signaller-matrix/signaller/internal/models/sync"
"github.com/wangjia184/sortedset"
)
type Backend struct {
2019-08-03 07:54:32 +00:00
data map[string]internal.User
rooms map[string]internal.Room
events *sortedset.SortedSet
roomAliases map[string]internal.Room
2019-08-03 07:54:32 +00:00
hostname string
validateUsernameFunc func(string) error // TODO: create ability to redefine validation func
2019-08-03 17:45:08 +00:00
mutex sync.RWMutex
}
type Token struct {
Device string
}
func NewBackend(hostname string) *Backend {
return &Backend{
2019-08-03 07:54:32 +00:00
hostname: hostname,
validateUsernameFunc: defaultValidationUsernameFunc,
rooms: make(map[string]internal.Room),
roomAliases: make(map[string]internal.Room),
events: sortedset.New(),
2019-08-03 07:54:32 +00:00
data: make(map[string]internal.User)}
}
func (backend *Backend) Register(username, password, device string) (user internal.User, token string, err models.ApiError) {
backend.mutex.Lock()
2019-08-03 07:54:32 +00:00
if backend.validateUsernameFunc != nil {
err := backend.validateUsernameFunc(username)
if err != nil {
return nil, "", models.NewError(models.M_INVALID_USERNAME, err.Error())
}
}
if _, ok := backend.data[username]; ok {
2019-07-31 15:53:36 +00:00
backend.mutex.Unlock()
return nil, "", models.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
}
user = &User{
name: username,
password: password,
2019-07-31 15:53:36 +00:00
Tokens: make(map[string]Token),
2019-08-05 10:12:48 +00:00
backend: backend,
filters: make(map[string]common.Filter)}
backend.data[username] = user
2019-07-31 15:53:36 +00:00
backend.mutex.Unlock()
return backend.Login(username, password, device)
}
func (backend *Backend) Login(username, password, device string) (user internal.User, token string, err models.ApiError) {
backend.mutex.Lock()
defer backend.mutex.Unlock()
user, ok := backend.data[username]
if !ok {
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong username")
}
if user.Password() != password {
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong password")
}
2019-08-04 16:46:31 +00:00
token = internal.RandomString(defaultTokenSize)
backend.data[username].(*User).Tokens[token] = Token{Device: device}
2019-07-31 15:53:36 +00:00
return user, token, nil
}
func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err models.ApiError) {
backend.mutex.Lock()
defer backend.mutex.Unlock()
return nil, nil // TODO: implement
}
2019-07-23 14:37:02 +00:00
func (backend *Backend) GetUserByToken(token string) internal.User {
2019-08-03 17:45:08 +00:00
backend.mutex.RLock()
defer backend.mutex.RUnlock()
2019-07-23 14:37:02 +00:00
for _, user := range backend.data {
for userToken := range user.(*User).Tokens {
if userToken == token {
return user
}
}
}
return nil
}
2019-07-31 15:01:20 +00:00
func (backend *Backend) GetRoomByID(id string) internal.Room {
2019-08-03 17:45:08 +00:00
backend.mutex.RLock()
defer backend.mutex.RUnlock()
2019-07-31 15:01:20 +00:00
for roomID, room := range backend.rooms {
if roomID == id {
return room
}
}
return nil
}
2019-08-02 13:17:18 +00:00
func (backend *Backend) GetUserByName(userName string) internal.User {
2019-08-03 17:45:08 +00:00
backend.mutex.RLock()
defer backend.mutex.RUnlock()
2019-08-02 13:17:18 +00:00
if user, exists := backend.data[userName]; exists {
return user
}
return nil
}
func (backend *Backend) PublicRooms(filter string) []internal.Room {
2019-08-03 17:45:08 +00:00
backend.mutex.RLock()
defer backend.mutex.RUnlock()
var rooms []internal.Room
for _, room := range backend.rooms {
if room.State() == createroom.PublicChat &&
(strings.Contains(room.Name(), filter) ||
strings.Contains(room.Topic(), filter) ||
strings.Contains(room.AliasName(), filter)) {
rooms = append(rooms, room)
}
}
sort.Sort(BySize(rooms))
return rooms
}
2019-08-03 07:54:32 +00:00
func (backend *Backend) GetRoomByAlias(alias string) internal.Room {
backend.mutex.RLock()
defer backend.mutex.RUnlock()
2019-08-08 14:38:30 +00:00
alias = internal.StripAlias(backend.hostname, alias)
if room, exists := backend.roomAliases[alias]; exists {
return room
}
return nil
}
2019-08-03 07:54:32 +00:00
func (backend *Backend) ValidateUsernameFunc() func(string) error {
2019-08-03 17:45:08 +00:00
backend.mutex.RLock()
defer backend.mutex.RUnlock()
2019-08-03 07:54:32 +00:00
return backend.validateUsernameFunc
}
func defaultValidationUsernameFunc(userName string) error {
const re = `^\w{5,}$`
2019-08-03 07:54:32 +00:00
if !regexp.MustCompile(re).MatchString(userName) {
return fmt.Errorf("username does not match %s", re)
}
return nil
}
func (backend *Backend) GetEventByID(id string) rooms.Event {
backend.mutex.RLock()
defer backend.mutex.RUnlock()
return backend.events.GetByKey(id).Value.(rooms.Event)
}
func (backend *Backend) PutEvent(event rooms.Event) error {
backend.mutex.Lock()
defer backend.mutex.Unlock()
backend.events.AddOrUpdate(event.EventID, sortedset.SCORE(time.Now().Unix()), event)
return nil
}
func (backend *Backend) GetEventsSince(user internal.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 internal.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
}