mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-05 02:01:03 +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
|
||||
|
||||
import "github.com/nxshock/signaller/matrix"
|
||||
import "github.com/nxshock/signaller/models"
|
||||
|
||||
type Backend interface {
|
||||
Register(username, password, device string) (token string, error *matrix.ApiError)
|
||||
Login(username, password, device string) (token string, err *matrix.ApiError)
|
||||
Logout(token string) *matrix.ApiError
|
||||
Sync(token string, request matrix.SyncRequest) (response *matrix.SyncReply, err *matrix.ApiError)
|
||||
Register(username, password, device string) (token string, error *models.ApiError)
|
||||
Login(username, password, device string) (token string, err *models.ApiError)
|
||||
Logout(token string) *models.ApiError
|
||||
Sync(token string, request models.SyncRequest) (response *models.SyncReply, err *models.ApiError)
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package main
|
||||
import (
|
||||
"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")
|
||||
|
||||
if message != "" {
|
||||
@ -17,7 +17,7 @@ func errorResponse(w http.ResponseWriter, code matrix.ApiError, httpCode int, me
|
||||
w.Write(code.JSON())
|
||||
}
|
||||
|
||||
func NewError(code matrix.ApiError, message string) *matrix.ApiError {
|
||||
func NewError(code models.ApiError, message string) *models.ApiError {
|
||||
if message != "" {
|
||||
code.Message = message
|
||||
}
|
||||
|
44
handlers.go
44
handlers.go
@ -8,28 +8,28 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/nxshock/signaller/matrix"
|
||||
"github.com/nxshock/signaller/models"
|
||||
)
|
||||
|
||||
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
response := matrix.VersionsReply{Versions: []string{Version}}
|
||||
response := models.VersionsReply{Versions: []string{Version}}
|
||||
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) {
|
||||
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":
|
||||
{
|
||||
type LoginFlow struct {
|
||||
@ -47,10 +47,10 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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":
|
||||
{
|
||||
var request matrix.LoginRequest
|
||||
var request models.LoginRequest
|
||||
getRequest(r, &request) // TODO: handle error
|
||||
|
||||
// delete start "@" if presents
|
||||
@ -64,7 +64,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
response := matrix.LoginReply{
|
||||
response := models.LoginReply{
|
||||
UserID: request.Identifier.User,
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
token := getTokenFromResponse(r)
|
||||
|
||||
if token == "" {
|
||||
errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||
return
|
||||
}
|
||||
|
||||
@ -96,20 +96,20 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
kind := r.FormValue("kind")
|
||||
if kind != "user" {
|
||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
|
||||
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong kind: "+kind)
|
||||
return
|
||||
}
|
||||
|
||||
var request matrix.RegisterRequest
|
||||
var request models.RegisterRequest
|
||||
getRequest(r, &request) // TODO: handle error
|
||||
|
||||
token, apiErr := server.Backend.Register(request.Username, request.Password, request.DeviceID)
|
||||
@ -118,7 +118,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var response matrix.RegisterResponse
|
||||
var response models.RegisterResponse
|
||||
response.UserID = "@" + request.Username
|
||||
response.DeviceID = request.DeviceID
|
||||
response.AccessToken = token
|
||||
@ -126,14 +126,14 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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) {
|
||||
var request matrix.SyncRequest
|
||||
var request models.SyncRequest
|
||||
request.Filter = r.FormValue("filter")
|
||||
|
||||
timeout, err := strconv.Atoi(r.FormValue("timeout"))
|
||||
if err != nil {
|
||||
errorResponse(w, matrix.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes")
|
||||
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "timeout parse failes")
|
||||
return
|
||||
}
|
||||
request.Timeout = timeout
|
||||
@ -141,14 +141,14 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token := getTokenFromResponse(r)
|
||||
if token == "" {
|
||||
errorResponse(w, matrix.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||
errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "")
|
||||
return
|
||||
}
|
||||
|
||||
response, _ := server.Backend.Sync(token, request) // TODO: handle error
|
||||
|
||||
response.NextBatch = "123"
|
||||
response.Rooms = matrix.RoomsSyncReply{}
|
||||
response.Rooms = models.RoomsSyncReply{}
|
||||
|
||||
sendJsonResponse(w, http.StatusOK, response)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package matrix
|
||||
package models
|
||||
|
||||
type Membership string
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package matrix
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package matrix
|
||||
package models
|
||||
|
||||
type JoinedRoomReply struct {
|
||||
JoinedRooms []string `json:"joined_rooms"`
|
||||
|
@ -1,4 +1,4 @@
|
||||
package matrix
|
||||
package models
|
||||
|
||||
// LoginRequest represents login request
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package matrix
|
||||
package models
|
||||
|
||||
type MRelatesTo struct {
|
||||
InReplyTo MInReplyTo `json:"m.in_reply_to"`
|
||||
|
52
users.go
52
users.go
@ -6,7 +6,7 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/nxshock/signaller/matrix"
|
||||
"github.com/nxshock/signaller/models"
|
||||
)
|
||||
|
||||
var first bool
|
||||
@ -29,12 +29,12 @@ func NewMemoryBackend() *MemoryBackend {
|
||||
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()
|
||||
defer memoryBackend.mutex.Unlock()
|
||||
|
||||
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)
|
||||
@ -48,17 +48,17 @@ func (memoryBackend MemoryBackend) Register(username, password, device string) (
|
||||
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()
|
||||
defer memoryBackend.mutex.Unlock()
|
||||
|
||||
user, ok := memoryBackend.data[username]
|
||||
if !ok {
|
||||
return "", NewError(matrix.M_FORBIDDEN, "wrong username")
|
||||
return "", NewError(models.M_FORBIDDEN, "wrong username")
|
||||
}
|
||||
|
||||
if user.Password != password {
|
||||
return "", NewError(matrix.M_FORBIDDEN, "wrong password")
|
||||
return "", NewError(models.M_FORBIDDEN, "wrong password")
|
||||
}
|
||||
|
||||
token = newToken(DefaultTokenSize)
|
||||
@ -68,7 +68,7 @@ func (memoryBackend MemoryBackend) Login(username, password, device string) (tok
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (memoryBackend MemoryBackend) Logout(token string) *matrix.ApiError {
|
||||
func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError {
|
||||
memoryBackend.mutex.Lock()
|
||||
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()
|
||||
defer memoryBackend.mutex.Unlock()
|
||||
|
||||
@ -92,29 +92,29 @@ func (memoryBackend MemoryBackend) Sync(token string, request matrix.SyncRequest
|
||||
|
||||
if !first {
|
||||
log.Println(1)
|
||||
response = &matrix.SyncReply{
|
||||
AccountData: matrix.AccountData{
|
||||
Events: []matrix.Event{
|
||||
matrix.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)},
|
||||
response = &models.SyncReply{
|
||||
AccountData: models.AccountData{
|
||||
Events: []models.Event{
|
||||
models.Event{Type: "m.direct", Content: json.RawMessage(`"@vasyo2:localhost":"!room1:localhost"`)},
|
||||
}},
|
||||
Rooms: matrix.RoomsSyncReply{
|
||||
Join: map[string]matrix.JoinedRoom{
|
||||
"!room1:localhost": matrix.JoinedRoom{
|
||||
Timeline: matrix.Timeline{
|
||||
Events: []matrix.RoomEvent{
|
||||
matrix.RoomEvent{Type: "m.room.create", Sender: "@vasyo2:localhost"},
|
||||
matrix.RoomEvent{Type: "m.room.member", Sender: "@vasyo2:localhost", Content: json.RawMessage(`membership:"join",displayname:"vasyo2"`)},
|
||||
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"`)},
|
||||
}}}}}}
|
||||
/* InviteState: matrix.InviteState{
|
||||
Events: []matrix.StrippedState{
|
||||
matrix.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},
|
||||
matrix.StrippedState{Type: "m.room.member", Content: json.RawMessage(`is_direct:"true",membership:"invite",displayname:"vasyo"`), Sender: "@vasyo2:" + server.Address},
|
||||
/* 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},
|
||||
}}}}}}*/
|
||||
first = true
|
||||
} else {
|
||||
os.Exit(0)
|
||||
response = &matrix.SyncReply{}
|
||||
response = &models.SyncReply{}
|
||||
}
|
||||
|
||||
return response, nil // TODO: implement
|
||||
|
Loading…
Reference in New Issue
Block a user