Query the roomserver for the current room state

We need this to send messages.
This commit is contained in:
Kegan Dougal 2017-03-14 14:11:54 +00:00
parent b9a4551075
commit f54fda1d3f
4 changed files with 45 additions and 7 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/config" "github.com/matrix-org/dendrite/clientapi/config"
"github.com/matrix-org/dendrite/clientapi/routing" "github.com/matrix-org/dendrite/clientapi/routing"
"github.com/matrix-org/dendrite/roomserver/api"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dugong" "github.com/matrix-org/dugong"
@ -53,6 +54,7 @@ func main() {
PrivateKey: privKey, PrivateKey: privKey,
KafkaProducerURIs: []string{"localhost:9092"}, KafkaProducerURIs: []string{"localhost:9092"},
ClientAPIOutputTopic: "clientapiOutput", ClientAPIOutputTopic: "clientapiOutput",
RoomserverURL: "http://localhost:7777",
} }
log.Info("Starting clientapi") log.Info("Starting clientapi")
@ -61,7 +63,8 @@ func main() {
if err != nil { if err != nil {
log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerURIs, err) log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerURIs, err)
} }
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)
routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, producer) routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, producer, queryAPI)
log.Fatal(http.ListenAndServe(bindAddr, nil)) log.Fatal(http.ListenAndServe(bindAddr, nil))
} }

View file

@ -15,4 +15,6 @@ type ClientAPI struct {
KafkaProducerURIs []string KafkaProducerURIs []string
// The topic for events which are written to the logs. // The topic for events which are written to the logs.
ClientAPIOutputTopic string ClientAPIOutputTopic string
// The URL of the roomserver which can service Query API requests
RoomserverURL string
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/config" "github.com/matrix-org/dendrite/clientapi/config"
"github.com/matrix-org/dendrite/clientapi/readers" "github.com/matrix-org/dendrite/clientapi/readers"
"github.com/matrix-org/dendrite/clientapi/writers" "github.com/matrix-org/dendrite/clientapi/writers"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
sarama "gopkg.in/Shopify/sarama.v1" sarama "gopkg.in/Shopify/sarama.v1"
@ -16,7 +17,7 @@ const pathPrefixR0 = "/_matrix/client/r0"
// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client // Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
// to clients which need to make outbound HTTP requests. // to clients which need to make outbound HTTP requests.
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer sarama.SyncProducer) { func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer sarama.SyncProducer, queryAPI api.RoomserverQueryAPI) {
apiMux := mux.NewRouter() apiMux := mux.NewRouter()
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
r0mux.Handle("/createRoom", make("createRoom", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { r0mux.Handle("/createRoom", make("createRoom", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
@ -25,10 +26,10 @@ func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI
r0mux.Handle("/sync", make("sync", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { r0mux.Handle("/sync", make("sync", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
return readers.Sync(req) return readers.Sync(req)
}))) })))
r0mux.Handle("/rooms/{roomID}/send/{eventType}", r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return writers.SendMessage(req, vars["roomID"], vars["eventType"]) return writers.SendMessage(req, vars["roomID"], vars["eventType"], vars["txnID"], cfg, queryAPI)
})), })),
) )

View file

@ -3,22 +3,54 @@ package writers
import ( import (
"net/http" "net/http"
"fmt"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/clientapi/config"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// SendMessage implements /rooms/{roomID}/send/{eventType} // http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
func SendMessage(req *http.Request, roomID, eventType string) util.JSONResponse { type sendMessageResponse struct {
EventID string `json:"event_id"`
}
// SendMessage implements /rooms/{roomID}/send/{eventType}/{txnID}
func SendMessage(req *http.Request, roomID, eventType, txnID string, cfg config.ClientAPI, queryAPI api.RoomserverQueryAPI) util.JSONResponse {
logger := util.GetLogger(req.Context()) logger := util.GetLogger(req.Context())
userID, resErr := auth.VerifyAccessToken(req) userID, resErr := auth.VerifyAccessToken(req)
if resErr != nil { if resErr != nil {
return *resErr return *resErr
} }
var r map[string]interface{} // must be a JSON object
resErr = httputil.UnmarshalJSONRequest(req, &r)
if resErr != nil {
return *resErr
}
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.ServerName)
queryReq := api.QueryLatestEventsAndStateRequest{
RoomID: roomID,
StateToFetch: []common.StateKeyTuple{
{"m.room.member", userID},
},
}
var queryRes api.QueryLatestEventsAndStateResponse
if err := queryAPI.QueryLatestEventsAndState(&queryReq, &queryRes); err != nil {
return httputil.LogThenError(req, err)
}
logger.WithFields(log.Fields{ logger.WithFields(log.Fields{
"roomID": roomID, "roomID": roomID,
"eventType": eventType, "eventType": eventType,
"userID": userID, "userID": userID,
"res": queryRes,
}).Info("Doing stuff...") }).Info("Doing stuff...")
return util.MessageResponse(404, "Not implemented yet") return util.JSONResponse{
Code: 200,
JSON: sendMessageResponse{eventID},
}
} }