mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-21 18:02:24 +00:00
refactor: Rename all references from matrix to models
This commit is contained in:
parent
5889ce7194
commit
b598613caa
10
backend.go
10
backend.go
@ -1,10 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/nxshock/signaller/matrix"
|
import "github.com/nxshock/signaller/models"
|
||||||
|
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
Register(username, password, device string) (token string, error *matrix.ApiError)
|
Register(username, password, device string) (token string, error *models.ApiError)
|
||||||
Login(username, password, device string) (token string, err *matrix.ApiError)
|
Login(username, password, device string) (token string, err *models.ApiError)
|
||||||
Logout(token string) *matrix.ApiError
|
Logout(token string) *models.ApiError
|
||||||
Sync(token string, request matrix.SyncRequest) (response *matrix.SyncReply, err *matrix.ApiError)
|
Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError)
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/nxshock/signaller/matrix"
|
"github.com/nxshock/signaller/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func errorResponse(w http.ResponseWriter, code matrix.ApiError, httpCode int, message string) {
|
func errorResponse(w http.ResponseWriter, code models.ApiError, httpCode int, message string) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if message != "" {
|
if message != "" {
|
||||||
@ -17,7 +17,7 @@ func errorResponse(w http.ResponseWriter, code matrix.ApiError, httpCode int, me
|
|||||||
w.Write(code.JSON())
|
w.Write(code.JSON())
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewError(code matrix.ApiError, message string) *matrix.ApiError {
|
func NewError(code models.ApiError, message string) *models.ApiError {
|
||||||
if message != "" {
|
if message != "" {
|
||||||
code.Message = message
|
code.Message = message
|
||||||
}
|
}
|
||||||
|
44
handlers.go
44
handlers.go
@ -8,28 +8,28 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nxshock/signaller/matrix"
|
"github.com/nxshock/signaller/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(r.RequestURI)
|
log.Println(r.RequestURI)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-versions
|
// https://models.org/docs/spec/client_server/latest#get-models-client-versions
|
||||||
func VersionHandler(w http.ResponseWriter, r *http.Request) {
|
func VersionHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response := matrix.VersionsReply{Versions: []string{Version}}
|
response := models.VersionsReply{Versions: []string{Version}}
|
||||||
sendJsonResponse(w, http.StatusOK, response)
|
sendJsonResponse(w, http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#login
|
// https://models.org/docs/spec/client_server/latest#login
|
||||||
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-login
|
// https://models.org/docs/spec/client_server/latest#get-models-client-r0-login
|
||||||
case "GET":
|
case "GET":
|
||||||
{
|
{
|
||||||
type LoginFlow struct {
|
type LoginFlow struct {
|
||||||
@ -47,10 +47,10 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
sendJsonResponse(w, http.StatusOK, response)
|
sendJsonResponse(w, http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login
|
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-login
|
||||||
case "POST":
|
case "POST":
|
||||||
{
|
{
|
||||||
var request matrix.LoginRequest
|
var request models.LoginRequest
|
||||||
getRequest(r, &request) // TODO: handle error
|
getRequest(r, &request) // TODO: handle error
|
||||||
|
|
||||||
// delete start "@" if presents
|
// delete start "@" if presents
|
||||||
@ -64,7 +64,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response := matrix.LoginReply{
|
response := models.LoginReply{
|
||||||
UserID: request.Identifier.User,
|
UserID: request.Identifier.User,
|
||||||
AccessToken: token}
|
AccessToken: token}
|
||||||
|
|
||||||
@ -73,17 +73,17 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout
|
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-logout
|
||||||
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := getTokenFromResponse(r)
|
token := getTokenFromResponse(r)
|
||||||
|
|
||||||
if token == "" {
|
if token == "" {
|
||||||
errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,20 +96,20 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
sendJsonResponse(w, http.StatusOK, struct{}{})
|
sendJsonResponse(w, http.StatusOK, struct{}{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-register
|
// https://models.org/docs/spec/client_server/latest#post-models-client-r0-register
|
||||||
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
kind := r.FormValue("kind")
|
kind := r.FormValue("kind")
|
||||||
if kind != "user" {
|
if kind != "user" {
|
||||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var request matrix.RegisterRequest
|
var request models.RegisterRequest
|
||||||
getRequest(r, &request) // TODO: handle error
|
getRequest(r, &request) // TODO: handle error
|
||||||
|
|
||||||
token, apiErr := server.Backend.Register(request.Username, request.Password, request.DeviceID)
|
token, apiErr := server.Backend.Register(request.Username, request.Password, request.DeviceID)
|
||||||
@ -118,7 +118,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var response matrix.RegisterResponse
|
var response models.RegisterResponse
|
||||||
response.UserID = "@" + request.Username
|
response.UserID = "@" + request.Username
|
||||||
response.DeviceID = request.DeviceID
|
response.DeviceID = request.DeviceID
|
||||||
response.AccessToken = token
|
response.AccessToken = token
|
||||||
@ -126,14 +126,14 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
sendJsonResponse(w, http.StatusOK, response)
|
sendJsonResponse(w, http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-sync
|
// https://models.org/docs/spec/client_server/latest#get-models-client-r0-sync
|
||||||
func SyncHandler(w http.ResponseWriter, r *http.Request) {
|
func SyncHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var request matrix.SyncRequest
|
var request models.SyncRequest
|
||||||
request.Filter = r.FormValue("filter")
|
request.Filter = r.FormValue("filter")
|
||||||
|
|
||||||
timeout, err := strconv.Atoi(r.FormValue("timeout"))
|
timeout, err := strconv.Atoi(r.FormValue("timeout"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes")
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
request.Timeout = timeout
|
request.Timeout = timeout
|
||||||
@ -141,14 +141,14 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
token := getTokenFromResponse(r)
|
token := getTokenFromResponse(r)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response, _ := server.Backend.Sync(token, request) // TODO: handle error
|
response, _ := server.Backend.Sync(token, request) // TODO: handle error
|
||||||
|
|
||||||
response.NextBatch = "123"
|
response.NextBatch = "123"
|
||||||
response.Rooms = matrix.RoomsSyncReply{}
|
response.Rooms = models.RoomsSyncReply{}
|
||||||
|
|
||||||
sendJsonResponse(w, http.StatusOK, response)
|
sendJsonResponse(w, http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
type Membership string
|
type Membership string
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
type JoinedRoomReply struct {
|
type JoinedRoomReply struct {
|
||||||
JoinedRooms []string `json:"joined_rooms"`
|
JoinedRooms []string `json:"joined_rooms"`
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
// LoginRequest represents login request
|
// LoginRequest represents login request
|
||||||
// https://matrix.org/docs/spec/client_server/r0.4.0.html#post-matrix-client-r0-login
|
// https://matrix.org/docs/spec/client_server/r0.4.0.html#post-matrix-client-r0-login
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package matrix
|
package models
|
||||||
|
|
||||||
type MRelatesTo struct {
|
type MRelatesTo struct {
|
||||||
InReplyTo MInReplyTo `json:"m.in_reply_to"`
|
InReplyTo MInReplyTo `json:"m.in_reply_to"`
|
||||||
|
52
users.go
52
users.go
@ -6,7 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/nxshock/signaller/matrix"
|
"github.com/nxshock/signaller/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var first bool
|
var first bool
|
||||||
@ -29,12 +29,12 @@ func NewMemoryBackend() *MemoryBackend {
|
|||||||
return &MemoryBackend{data: make(map[string]*User)}
|
return &MemoryBackend{data: make(map[string]*User)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *matrix.ApiError) {
|
func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *models.ApiError) {
|
||||||
memoryBackend.mutex.Lock()
|
memoryBackend.mutex.Lock()
|
||||||
defer memoryBackend.mutex.Unlock()
|
defer memoryBackend.mutex.Unlock()
|
||||||
|
|
||||||
if _, ok := memoryBackend.data[username]; ok {
|
if _, ok := memoryBackend.data[username]; ok {
|
||||||
return "", NewError(matrix.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
return "", NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
|
||||||
}
|
}
|
||||||
|
|
||||||
token = newToken(DefaultTokenSize)
|
token = newToken(DefaultTokenSize)
|
||||||
@ -48,17 +48,17 @@ func (memoryBackend MemoryBackend) Register(username, password, device string) (
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *matrix.ApiError) {
|
func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *models.ApiError) {
|
||||||
memoryBackend.mutex.Lock()
|
memoryBackend.mutex.Lock()
|
||||||
defer memoryBackend.mutex.Unlock()
|
defer memoryBackend.mutex.Unlock()
|
||||||
|
|
||||||
user, ok := memoryBackend.data[username]
|
user, ok := memoryBackend.data[username]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", NewError(matrix.M_FORBIDDEN, "wrong username")
|
return "", NewError(models.M_FORBIDDEN, "wrong username")
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.Password != password {
|
if user.Password != password {
|
||||||
return "", NewError(matrix.M_FORBIDDEN, "wrong password")
|
return "", NewError(models.M_FORBIDDEN, "wrong password")
|
||||||
}
|
}
|
||||||
|
|
||||||
token = newToken(DefaultTokenSize)
|
token = newToken(DefaultTokenSize)
|
||||||
@ -68,7 +68,7 @@ func (memoryBackend MemoryBackend) Login(username, password, device string) (tok
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (memoryBackend MemoryBackend) Logout(token string) *matrix.ApiError {
|
func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError {
|
||||||
memoryBackend.mutex.Lock()
|
memoryBackend.mutex.Lock()
|
||||||
defer memoryBackend.mutex.Unlock()
|
defer memoryBackend.mutex.Unlock()
|
||||||
|
|
||||||
@ -81,10 +81,10 @@ func (memoryBackend MemoryBackend) Logout(token string) *matrix.ApiError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewError(matrix.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
|
return NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
|
||||||
}
|
}
|
||||||
|
|
||||||
func (memoryBackend MemoryBackend) Sync(token string, request matrix.SyncRequest) (response *matrix.SyncReply, err *matrix.ApiError) {
|
func (memoryBackend MemoryBackend) Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError) {
|
||||||
memoryBackend.mutex.Lock()
|
memoryBackend.mutex.Lock()
|
||||||
defer memoryBackend.mutex.Unlock()
|
defer memoryBackend.mutex.Unlock()
|
||||||
|
|
||||||
@ -92,29 +92,29 @@ func (memoryBackend MemoryBackend) Sync(token string, request matrix.SyncRequest
|
|||||||
|
|
||||||
if !first {
|
if !first {
|
||||||
log.Println(1)
|
log.Println(1)
|
||||||
response = &matrix.SyncReply{
|
response = &models.SyncReply{
|
||||||
AccountData: matrix.AccountData{
|
AccountData: models.AccountData{
|
||||||
Events: []matrix.Event{
|
Events: []models.Event{
|
||||||
matrix.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)},
|
models.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)},
|
||||||
}},
|
}},
|
||||||
Rooms: matrix.RoomsSyncReply{
|
Rooms: models.RoomsSyncReply{
|
||||||
Join: map[string]matrix.JoinedRoom{
|
Join: map[string]models.JoinedRoom{
|
||||||
"!room1:localhost": matrix.JoinedRoom{
|
"!room1:localhost": models.JoinedRoom{
|
||||||
Timeline: matrix.Timeline{
|
Timeline: models.Timeline{
|
||||||
Events: []matrix.RoomEvent{
|
Events: []models.RoomEvent{
|
||||||
matrix.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"},
|
models.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"},
|
||||||
matrix.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)},
|
models.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)},
|
||||||
}}}}}}
|
}}}}}}
|
||||||
/* InviteState: matrix.InviteState{
|
/* InviteState: models.InviteState{
|
||||||
Events: []matrix.StrippedState{
|
Events: []models.StrippedState{
|
||||||
matrix.StrippedState{Type: "m.room.join_rules", Content: json.RawMessage(`join_rule:"invite"`), Sender: "@vasyo2:" + server.Address},
|
models.StrippedState{Type: "m.room.join_rules", Content: json.RawMessage(`join_rule:"invite"`), Sender: "@vasyo2:" + server.Address},
|
||||||
matrix.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(`membership:"join",displayname:"vasyo2"`), Sender: "@vasyo2:" + server.Address},
|
||||||
matrix.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address},
|
models.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address},
|
||||||
}}}}}}*/
|
}}}}}}*/
|
||||||
first = true
|
first = true
|
||||||
} else {
|
} else {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
response = &matrix.SyncReply{}
|
response = &models.SyncReply{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil // TODO: implement
|
return response, nil // TODO: implement
|
||||||
|
Loading…
Reference in New Issue
Block a user