2019-07-21 09:43:14 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2019-08-18 10:56:16 +00:00
|
|
|
"encoding/json"
|
2019-08-03 07:54:32 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2019-08-02 17:55:35 +00:00
|
|
|
"sort"
|
2019-08-03 14:47:33 +00:00
|
|
|
"strings"
|
2019-07-21 09:43:14 +00:00
|
|
|
"sync"
|
2019-08-09 17:02:33 +00:00
|
|
|
"time"
|
2019-07-21 09:43:14 +00:00
|
|
|
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal"
|
|
|
|
"github.com/signaller-matrix/signaller/internal/models"
|
2019-08-05 15:06:46 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/common"
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
2019-08-10 07:48:55 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/events"
|
2019-08-18 10:56:16 +00:00
|
|
|
"github.com/tidwall/buntdb"
|
2019-07-21 09:43:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Backend struct {
|
2019-08-03 07:54:32 +00:00
|
|
|
data map[string]internal.User
|
|
|
|
rooms map[string]internal.Room
|
2019-08-18 10:56:16 +00:00
|
|
|
events *buntdb.DB
|
2019-08-06 15:28:39 +00:00
|
|
|
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
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
Device string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBackend(hostname string) *Backend {
|
2019-08-18 10:56:16 +00:00
|
|
|
eventDB, err := buntdb.Open(":memory:")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
eventDB.CreateIndex("origin_server_ts", "*", buntdb.IndexJSON("origin_server_ts"))
|
|
|
|
eventDB.CreateIndex("room_id", "*", buntdb.IndexJSON("room_id"))
|
2019-07-21 09:43:14 +00:00
|
|
|
return &Backend{
|
2019-08-03 07:54:32 +00:00
|
|
|
hostname: hostname,
|
|
|
|
validateUsernameFunc: defaultValidationUsernameFunc,
|
|
|
|
rooms: make(map[string]internal.Room),
|
2019-08-06 15:28:39 +00:00
|
|
|
roomAliases: make(map[string]internal.Room),
|
2019-08-18 10:56:16 +00:00
|
|
|
events: eventDB,
|
2019-08-03 07:54:32 +00:00
|
|
|
data: make(map[string]internal.User)}
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 06:46:57 +00:00
|
|
|
func (backend *Backend) Register(username, password, device string) (user internal.User, token string, err models.ApiError) {
|
2019-07-21 09:43:14 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 09:43:14 +00:00
|
|
|
if _, ok := backend.data[username]; ok {
|
2019-07-31 15:53:36 +00:00
|
|
|
backend.mutex.Unlock()
|
2019-08-03 06:46:57 +00:00
|
|
|
return nil, "", models.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-08-05 15:06:46 +00:00
|
|
|
filters: make(map[string]common.Filter)}
|
2019-07-21 09:43:14 +00:00
|
|
|
|
|
|
|
backend.data[username] = user
|
|
|
|
|
2019-07-31 15:53:36 +00:00
|
|
|
backend.mutex.Unlock()
|
|
|
|
return backend.Login(username, password, device)
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 06:46:57 +00:00
|
|
|
func (backend *Backend) Login(username, password, device string) (user internal.User, token string, err models.ApiError) {
|
2019-07-21 09:43:14 +00:00
|
|
|
backend.mutex.Lock()
|
|
|
|
defer backend.mutex.Unlock()
|
|
|
|
|
|
|
|
user, ok := backend.data[username]
|
|
|
|
if !ok {
|
2019-08-03 06:46:57 +00:00
|
|
|
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong username")
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user.Password() != password {
|
2019-08-03 06:46:57 +00:00
|
|
|
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong password")
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 16:46:31 +00:00
|
|
|
token = internal.RandomString(defaultTokenSize)
|
2019-07-21 09:43:14 +00:00
|
|
|
|
|
|
|
backend.data[username].(*User).Tokens[token] = Token{Device: device}
|
|
|
|
|
2019-07-31 15:53:36 +00:00
|
|
|
return user, token, nil
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-08-02 17:55:35 +00:00
|
|
|
|
2019-08-03 14:47:33 +00:00
|
|
|
func (backend *Backend) PublicRooms(filter string) []internal.Room {
|
2019-08-03 17:45:08 +00:00
|
|
|
backend.mutex.RLock()
|
|
|
|
defer backend.mutex.RUnlock()
|
2019-08-02 17:55:35 +00:00
|
|
|
|
|
|
|
var rooms []internal.Room
|
|
|
|
|
|
|
|
for _, room := range backend.rooms {
|
2019-08-03 14:47:33 +00:00
|
|
|
if room.State() == createroom.PublicChat &&
|
|
|
|
(strings.Contains(room.Name(), filter) ||
|
|
|
|
strings.Contains(room.Topic(), filter) ||
|
|
|
|
strings.Contains(room.AliasName(), filter)) {
|
2019-08-02 17:55:35 +00:00
|
|
|
rooms = append(rooms, room)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(BySize(rooms))
|
|
|
|
|
|
|
|
return rooms
|
|
|
|
}
|
2019-08-03 07:54:32 +00:00
|
|
|
|
2019-08-06 15:28:39 +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)
|
2019-08-06 15:28:39 +00:00
|
|
|
|
|
|
|
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 {
|
2019-08-03 13:54:58 +00:00
|
|
|
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
|
|
|
|
}
|
2019-08-07 09:07:11 +00:00
|
|
|
|
2019-08-10 07:48:55 +00:00
|
|
|
func (backend *Backend) GetEventByID(id string) events.Event {
|
2019-08-07 09:07:11 +00:00
|
|
|
backend.mutex.RLock()
|
|
|
|
defer backend.mutex.RUnlock()
|
|
|
|
|
2019-08-18 10:56:16 +00:00
|
|
|
var event events.Event
|
|
|
|
|
|
|
|
backend.events.View(func(tx *buntdb.Tx) error {
|
|
|
|
val, err := tx.Get(id, true)
|
|
|
|
if err == nil {
|
|
|
|
json.Unmarshal([]byte(val), event)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return event
|
2019-08-07 09:07:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 07:48:55 +00:00
|
|
|
func (backend *Backend) PutEvent(event events.Event) error {
|
2019-08-07 14:33:10 +00:00
|
|
|
backend.mutex.Lock()
|
|
|
|
defer backend.mutex.Unlock()
|
2019-08-07 09:07:11 +00:00
|
|
|
|
2019-08-18 10:56:16 +00:00
|
|
|
marshalledEvent, err := json.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backend.events.Update(func(tx *buntdb.Tx) error {
|
|
|
|
_, _, err := tx.Set(event.ID(), string(marshalledEvent), nil)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-07 09:07:11 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-09 17:02:33 +00:00
|
|
|
|
2019-08-10 07:48:55 +00:00
|
|
|
func (backend *Backend) GetEventsSince(user internal.User, sinceToken string, limit int) []events.Event {
|
2019-08-18 10:56:16 +00:00
|
|
|
if sinceToken != "" {
|
|
|
|
var sinceEvent events.Event
|
|
|
|
var eventSlice []events.Event
|
|
|
|
err := backend.events.View(func(tx *buntdb.Tx) error {
|
|
|
|
// handler error
|
|
|
|
val, err := tx.Get(sinceToken, true)
|
|
|
|
if err == nil {
|
|
|
|
json.Unmarshal([]byte(val), sinceEvent)
|
|
|
|
}
|
2019-08-18 14:40:10 +00:00
|
|
|
sinceRoomEvent := sinceEvent.(*events.RoomEvent)
|
2019-08-18 10:56:16 +00:00
|
|
|
tx.AscendRange("origin_server_ts", `{"origin_server_ts": `+string(sinceRoomEvent.OriginServerTs)+`}`, `{"origin_server_ts": `+string(time.Now().Unix())+`}`, func(key, value string) bool {
|
|
|
|
var unmarshalledEvent events.Event
|
|
|
|
json.Unmarshal([]byte(value), unmarshalledEvent)
|
|
|
|
eventSlice = append(eventSlice, unmarshalledEvent)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
2019-08-09 17:02:33 +00:00
|
|
|
|
2019-08-18 10:56:16 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-09 17:02:33 +00:00
|
|
|
|
2019-08-18 10:56:16 +00:00
|
|
|
var returnEvents []events.Event
|
|
|
|
if eventSlice != nil {
|
|
|
|
for _, event := range eventSlice {
|
|
|
|
if isEventRelatedToUser(event, user) {
|
|
|
|
returnEvents = append(returnEvents, event)
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 17:02:33 +00:00
|
|
|
}
|
2019-08-18 10:56:16 +00:00
|
|
|
|
|
|
|
return returnEvents
|
2019-08-09 17:02:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 10:56:16 +00:00
|
|
|
return nil
|
2019-08-09 17:02:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 07:48:55 +00:00
|
|
|
func isEventRelatedToUser(event events.Event, user internal.User) bool {
|
2019-08-11 13:47:25 +00:00
|
|
|
if roomEvent, ok := event.(*events.RoomEvent); ok {
|
2019-08-17 08:42:56 +00:00
|
|
|
if internal.InArray(roomEvent.RoomID, extractRoomIDsFromModel(user.JoinedRooms())) { // TODO check for invited or archived rooms
|
2019-08-11 13:47:25 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-08-09 17:02:33 +00:00
|
|
|
}
|
2019-08-11 13:47:25 +00:00
|
|
|
|
2019-08-09 17:02:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractRoomIDsFromModel(rooms []internal.Room) []string {
|
|
|
|
var roomIDs []string
|
|
|
|
for _, room := range rooms {
|
|
|
|
roomIDs = append(roomIDs, room.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
return roomIDs
|
|
|
|
}
|