mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-09 12:11:03 +00:00
Merge pull request #8 from signaller-matrix/feat/filtering
Add FilterAPI
This commit is contained in:
commit
f3ded3e660
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
|||||||
module github.com/nxshock/signaller
|
module github.com/signaller-matrix/signaller
|
||||||
|
|
||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package internal
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/signaller-matrix/signaller/internal/models"
|
"github.com/signaller-matrix/signaller/internal/models"
|
||||||
|
"github.com/signaller-matrix/signaller/internal/models/common"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/devices"
|
"github.com/signaller-matrix/signaller/internal/models/devices"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/rooms"
|
"github.com/signaller-matrix/signaller/internal/models/rooms"
|
||||||
@ -50,4 +51,6 @@ type User interface {
|
|||||||
LogoutAll()
|
LogoutAll()
|
||||||
JoinRoom(Room) models.ApiError
|
JoinRoom(Room) models.ApiError
|
||||||
Invite(Room, User) models.ApiError
|
Invite(Room, User) models.ApiError
|
||||||
|
AddFilter(filterID string, filter common.Filter)
|
||||||
|
GetFilterByID(filterID string) *common.Filter
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/signaller-matrix/signaller/internal/models/common"
|
||||||
|
|
||||||
"github.com/signaller-matrix/signaller/internal"
|
"github.com/signaller-matrix/signaller/internal"
|
||||||
"github.com/signaller-matrix/signaller/internal/models"
|
"github.com/signaller-matrix/signaller/internal/models"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
||||||
@ -52,7 +54,8 @@ func (backend *Backend) Register(username, password, device string) (user intern
|
|||||||
name: username,
|
name: username,
|
||||||
password: password,
|
password: password,
|
||||||
Tokens: make(map[string]Token),
|
Tokens: make(map[string]Token),
|
||||||
backend: backend}
|
backend: backend,
|
||||||
|
filters: make(map[string]common.Filter)}
|
||||||
|
|
||||||
backend.data[username] = user
|
backend.data[username] = user
|
||||||
|
|
||||||
@ -73,7 +76,7 @@ func (backend *Backend) Login(username, password, device string) (user internal.
|
|||||||
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong password")
|
return nil, "", models.NewError(models.M_FORBIDDEN, "wrong password")
|
||||||
}
|
}
|
||||||
|
|
||||||
token = newToken(defaultTokenSize)
|
token = internal.RandomString(defaultTokenSize)
|
||||||
|
|
||||||
backend.data[username].(*User).Tokens[token] = Token{Device: device}
|
backend.data[username].(*User).Tokens[token] = Token{Device: device}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/signaller-matrix/signaller/internal"
|
"github.com/signaller-matrix/signaller/internal"
|
||||||
"github.com/signaller-matrix/signaller/internal/models"
|
"github.com/signaller-matrix/signaller/internal/models"
|
||||||
|
"github.com/signaller-matrix/signaller/internal/models/common"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/devices"
|
"github.com/signaller-matrix/signaller/internal/models/devices"
|
||||||
"github.com/signaller-matrix/signaller/internal/models/rooms"
|
"github.com/signaller-matrix/signaller/internal/models/rooms"
|
||||||
@ -15,6 +16,7 @@ type User struct {
|
|||||||
name string
|
name string
|
||||||
password string
|
password string
|
||||||
Tokens map[string]Token
|
Tokens map[string]Token
|
||||||
|
filters map[string]common.Filter
|
||||||
|
|
||||||
backend *Backend
|
backend *Backend
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
events = append(events, RoomEvent{
|
events = append(events, RoomEvent{
|
||||||
Content: nil,
|
Content: nil,
|
||||||
Type: rooms.Create,
|
Type: rooms.Create,
|
||||||
EventID: newToken(eventIDSize),
|
EventID: internal.RandomString(eventIDSize),
|
||||||
Sender: user,
|
Sender: user,
|
||||||
OriginServerTS: t})
|
OriginServerTS: t})
|
||||||
|
|
||||||
@ -58,7 +60,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
events = append(events, RoomEvent{
|
events = append(events, RoomEvent{
|
||||||
Content: []byte(request.Visibility), // TODO: check visibility vs join rules
|
Content: []byte(request.Visibility), // TODO: check visibility vs join rules
|
||||||
Type: rooms.JoinRules,
|
Type: rooms.JoinRules,
|
||||||
EventID: newToken(eventIDSize),
|
EventID: internal.RandomString(eventIDSize),
|
||||||
Sender: user,
|
Sender: user,
|
||||||
OriginServerTS: t})
|
OriginServerTS: t})
|
||||||
|
|
||||||
@ -67,7 +69,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
events = append(events, RoomEvent{
|
events = append(events, RoomEvent{
|
||||||
Content: nil, // TODO: add
|
Content: nil, // TODO: add
|
||||||
Type: rooms.Name,
|
Type: rooms.Name,
|
||||||
EventID: newToken(eventIDSize),
|
EventID: internal.RandomString(eventIDSize),
|
||||||
Sender: user,
|
Sender: user,
|
||||||
OriginServerTS: t})
|
OriginServerTS: t})
|
||||||
}
|
}
|
||||||
@ -77,13 +79,13 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
events = append(events, RoomEvent{
|
events = append(events, RoomEvent{
|
||||||
Content: nil, // TODO: add
|
Content: nil, // TODO: add
|
||||||
Type: rooms.CanonicalAlias,
|
Type: rooms.CanonicalAlias,
|
||||||
EventID: newToken(eventIDSize),
|
EventID: internal.RandomString(eventIDSize),
|
||||||
Sender: user,
|
Sender: user,
|
||||||
OriginServerTS: t})
|
OriginServerTS: t})
|
||||||
}
|
}
|
||||||
|
|
||||||
room := &Room{
|
room := &Room{
|
||||||
id: newToken(groupIDSize),
|
id: internal.RandomString(groupIDSize),
|
||||||
aliasName: request.RoomAliasName,
|
aliasName: request.RoomAliasName,
|
||||||
name: request.Name,
|
name: request.Name,
|
||||||
topic: request.Topic,
|
topic: request.Topic,
|
||||||
@ -190,7 +192,7 @@ func (user *User) SendMessage(room internal.Room, text string) models.ApiError {
|
|||||||
room.(*Room).events = append(room.(*Room).events, RoomEvent{
|
room.(*Room).events = append(room.(*Room).events, RoomEvent{
|
||||||
Content: nil,
|
Content: nil,
|
||||||
Type: rooms.Message,
|
Type: rooms.Message,
|
||||||
EventID: newToken(defaultTokenSize),
|
EventID: internal.RandomString(defaultTokenSize),
|
||||||
Sender: user,
|
Sender: user,
|
||||||
OriginServerTS: time.Now(),
|
OriginServerTS: time.Now(),
|
||||||
Room: room})
|
Room: room})
|
||||||
@ -275,3 +277,14 @@ func (user *User) JoinRoom(room internal.Room) models.ApiError {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (user *User) AddFilter(filterID string, filter common.Filter) {
|
||||||
|
user.filters[filterID] = filter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (user *User) GetFilterByID(filterID string) *common.Filter {
|
||||||
|
if val, ok := user.filters[filterID]; ok {
|
||||||
|
return &val
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -323,6 +323,61 @@ func CapabilitiesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
sendJsonResponse(w, http.StatusOK, response)
|
sendJsonResponse(w, http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddFilterHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token := getTokenFromResponse(r)
|
||||||
|
if token == "" {
|
||||||
|
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user := currServer.Backend.GetUserByToken(token)
|
||||||
|
if user == nil {
|
||||||
|
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var request common.Filter
|
||||||
|
getRequest(r, &request)
|
||||||
|
|
||||||
|
filterID := RandomString(12)
|
||||||
|
user.AddFilter(filterID, request)
|
||||||
|
|
||||||
|
sendJsonResponse(w, http.StatusOK, map[string]interface{}{
|
||||||
|
"filter_id": filterID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFilterHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token := getTokenFromResponse(r)
|
||||||
|
if token == "" {
|
||||||
|
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user := currServer.Backend.GetUserByToken(token)
|
||||||
|
if user == nil {
|
||||||
|
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filter := user.GetFilterByID(mux.Vars(r)["filterID"])
|
||||||
|
if filter == nil {
|
||||||
|
errorResponse(w, models.M_INVALID_PARAM, http.StatusNotFound, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
sendJsonResponse(w, http.StatusOK, filter)
|
||||||
|
}
|
||||||
|
|
||||||
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-devices
|
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-devices
|
||||||
func DevicesHandler(w http.ResponseWriter, r *http.Request) {
|
func DevicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
|
27
internal/models/common/filter.go
Normal file
27
internal/models/common/filter.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type Filter struct {
|
||||||
|
Room struct {
|
||||||
|
State struct {
|
||||||
|
Types []string `json:"types"`
|
||||||
|
NotRooms []string `json:"not_rooms"`
|
||||||
|
} `json:"state"`
|
||||||
|
Timeline struct {
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
Types []string `json:"types"`
|
||||||
|
NotRooms []string `json:"not_rooms"`
|
||||||
|
NotSenders []string `json:"not_senders"`
|
||||||
|
} `json:"timeline"`
|
||||||
|
Ephemeral struct {
|
||||||
|
Types []string `json:"types"`
|
||||||
|
NotRooms []string `json:"not_rooms"`
|
||||||
|
NotSenders []string `json:"not_senders"`
|
||||||
|
} `json:"ephemeral"`
|
||||||
|
} `json:"room"`
|
||||||
|
Presence struct {
|
||||||
|
Types []string `json:"types"`
|
||||||
|
NotSenders []string `json:"not_senders"`
|
||||||
|
} `json:"presence"`
|
||||||
|
EventFormat string `json:"event_format"`
|
||||||
|
EventFields []string `json:"event_fields"`
|
||||||
|
}
|
@ -1,12 +1,12 @@
|
|||||||
package memory
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// newToken returns new generated token with specified length
|
// RandomString returns new generated token with specified length
|
||||||
func newToken(size int) string {
|
func RandomString(size int) string {
|
||||||
b := make([]byte, size)
|
b := make([]byte, size)
|
||||||
rand.Read(b) // TODO: check may be can be error
|
rand.Read(b) // TODO: check may be can be error
|
||||||
|
|
@ -40,6 +40,9 @@ func NewServer(port int) (*Server, error) {
|
|||||||
router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler)
|
router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler)
|
||||||
router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler)
|
router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler)
|
||||||
router.HandleFunc("/_matrix/client/r0/publicRooms", publicRoomsHandler)
|
router.HandleFunc("/_matrix/client/r0/publicRooms", publicRoomsHandler)
|
||||||
|
router.HandleFunc("/_matrix/client/r0/user/{userId}/filter/{filterID}", GetFilterHandler).Methods("GET")
|
||||||
|
router.HandleFunc("/_matrix/client/r0/user/{userId}/filter", AddFilterHandler).Methods("POST")
|
||||||
|
|
||||||
router.HandleFunc("/", RootHandler)
|
router.HandleFunc("/", RootHandler)
|
||||||
|
|
||||||
if port <= 0 || port > 65535 {
|
if port <= 0 || port > 65535 {
|
||||||
|
Loading…
Reference in New Issue
Block a user