2019-07-19 07:39:53 +00:00
|
|
|
package internal
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2019-07-19 07:39:53 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models"
|
2019-07-18 14:10:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var first bool
|
|
|
|
|
|
|
|
type MemoryBackend struct {
|
|
|
|
data map[string]*User
|
|
|
|
mutex sync.Mutex // TODO: replace with RW mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Password string
|
|
|
|
Tokens map[string]Token
|
|
|
|
}
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
Device string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemoryBackend() *MemoryBackend {
|
|
|
|
return &MemoryBackend{data: make(map[string]*User)}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *models.ApiError) {
|
2019-07-18 14:10:43 +00:00
|
|
|
memoryBackend.mutex.Lock()
|
|
|
|
defer memoryBackend.mutex.Unlock()
|
|
|
|
|
|
|
|
if _, ok := memoryBackend.data[username]; ok {
|
2019-07-18 15:15:19 +00:00
|
|
|
return "", NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token = newToken(DefaultTokenSize)
|
|
|
|
|
|
|
|
memoryBackend.data[username] = &User{
|
|
|
|
Password: password,
|
|
|
|
Tokens: map[string]Token{
|
|
|
|
token: {
|
|
|
|
Device: device}}}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *models.ApiError) {
|
2019-07-18 14:10:43 +00:00
|
|
|
memoryBackend.mutex.Lock()
|
|
|
|
defer memoryBackend.mutex.Unlock()
|
|
|
|
|
|
|
|
user, ok := memoryBackend.data[username]
|
|
|
|
if !ok {
|
2019-07-18 15:15:19 +00:00
|
|
|
return "", NewError(models.M_FORBIDDEN, "wrong username")
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user.Password != password {
|
2019-07-18 15:15:19 +00:00
|
|
|
return "", NewError(models.M_FORBIDDEN, "wrong password")
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token = newToken(DefaultTokenSize)
|
|
|
|
|
|
|
|
memoryBackend.data[username].Tokens[token] = Token{Device: device}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError {
|
2019-07-18 14:10:43 +00:00
|
|
|
memoryBackend.mutex.Lock()
|
|
|
|
defer memoryBackend.mutex.Unlock()
|
|
|
|
|
|
|
|
for _, user := range memoryBackend.data {
|
|
|
|
for userToken, _ := range user.Tokens {
|
|
|
|
if userToken == token {
|
|
|
|
delete(user.Tokens, token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
return NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:15:19 +00:00
|
|
|
func (memoryBackend MemoryBackend) Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError) {
|
2019-07-18 14:10:43 +00:00
|
|
|
memoryBackend.mutex.Lock()
|
|
|
|
defer memoryBackend.mutex.Unlock()
|
|
|
|
|
|
|
|
log.Println(request)
|
|
|
|
|
|
|
|
if !first {
|
|
|
|
log.Println(1)
|
2019-07-18 15:15:19 +00:00
|
|
|
response = &models.SyncReply{
|
|
|
|
AccountData: models.AccountData{
|
|
|
|
Events: []models.Event{
|
|
|
|
models.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)},
|
2019-07-18 14:10:43 +00:00
|
|
|
}},
|
2019-07-18 15:15:19 +00:00
|
|
|
Rooms: models.RoomsSyncReply{
|
|
|
|
Join: map[string]models.JoinedRoom{
|
|
|
|
"!room1:localhost": models.JoinedRoom{
|
|
|
|
Timeline: models.Timeline{
|
|
|
|
Events: []models.RoomEvent{
|
|
|
|
models.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"},
|
|
|
|
models.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)},
|
2019-07-18 14:10:43 +00:00
|
|
|
}}}}}}
|
2019-07-18 15:15:19 +00:00
|
|
|
/* InviteState: models.InviteState{
|
|
|
|
Events: []models.StrippedState{
|
|
|
|
models.StrippedState{Type: "m.room.join_rules", Content: json.RawMessage(`join_rule:"invite"`), Sender: "@vasyo2:" + server.Address},
|
|
|
|
models.StrippedState{Type: "m.room.member", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`), Sender: "@vasyo2:" + server.Address},
|
|
|
|
models.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address},
|
2019-07-18 14:10:43 +00:00
|
|
|
}}}}}}*/
|
|
|
|
first = true
|
|
|
|
} else {
|
|
|
|
os.Exit(0)
|
2019-07-18 15:15:19 +00:00
|
|
|
response = &models.SyncReply{}
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil // TODO: implement
|
|
|
|
}
|