2017-04-20 22:40:52 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-02-03 16:05:46 +00:00
|
|
|
package writers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
"fmt"
|
2017-03-15 13:36:26 +00:00
|
|
|
"time"
|
|
|
|
|
2017-05-23 16:43:05 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-02-03 16:05:46 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
|
2017-03-17 11:21:52 +00:00
|
|
|
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
|
|
|
|
type sendEventResponse struct {
|
2017-03-15 11:22:40 +00:00
|
|
|
EventID string `json:"event_id"`
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:21:52 +00:00
|
|
|
// SendEvent implements:
|
|
|
|
// /rooms/{roomID}/send/{eventType}/{txnID}
|
|
|
|
// /rooms/{roomID}/state/{eventType}/{stateKey}
|
2017-06-19 14:21:04 +00:00
|
|
|
func SendEvent(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
roomID, eventType, txnID string, stateKey *string,
|
|
|
|
cfg config.Dendrite,
|
|
|
|
queryAPI api.RoomserverQueryAPI,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
) util.JSONResponse {
|
2017-03-15 11:22:40 +00:00
|
|
|
// parse the incoming http request
|
2017-05-23 16:43:05 +00:00
|
|
|
userID := device.UserID
|
2017-03-15 11:22:40 +00:00
|
|
|
var r map[string]interface{} // must be a JSON object
|
2017-05-23 16:43:05 +00:00
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
2017-03-15 11:22:40 +00:00
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the new event and set all the fields we can
|
|
|
|
builder := gomatrixserverlib.EventBuilder{
|
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Type: eventType,
|
2017-03-17 11:21:52 +00:00
|
|
|
StateKey: stateKey,
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
|
|
|
builder.SetContent(r)
|
|
|
|
|
|
|
|
// work out what will be required in order to send this event
|
2017-03-17 16:28:15 +00:00
|
|
|
needed, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
|
2017-03-15 11:22:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the roomserver for information about this room
|
|
|
|
queryReq := api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
2017-03-17 16:28:15 +00:00
|
|
|
StateToFetch: needed.Tuples(),
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
|
|
|
if queryErr := queryAPI.QueryLatestEventsAndState(&queryReq, &queryRes); queryErr != nil {
|
|
|
|
return httputil.LogThenError(req, queryErr)
|
|
|
|
}
|
|
|
|
if !queryRes.RoomExists {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 404,
|
|
|
|
JSON: jsonerror.NotFound("Room does not exist"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the fields we previously couldn't do and build the event
|
|
|
|
builder.PrevEvents = queryRes.LatestEvents // the current events will be the prev events of the new event
|
|
|
|
var refs []gomatrixserverlib.EventReference
|
|
|
|
for _, e := range queryRes.StateEvents {
|
|
|
|
refs = append(refs, e.EventReference())
|
|
|
|
}
|
|
|
|
builder.AuthEvents = refs
|
2017-06-27 14:28:44 +00:00
|
|
|
builder.Depth = queryRes.Depth
|
2017-06-19 14:21:04 +00:00
|
|
|
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
|
|
|
|
e, err := builder.Build(
|
|
|
|
eventID, time.Now(), cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
|
|
|
)
|
2017-03-15 11:22:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if this user can perform this operation
|
|
|
|
stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))
|
|
|
|
for i := range queryRes.StateEvents {
|
|
|
|
stateEvents[i] = &queryRes.StateEvents[i]
|
|
|
|
}
|
|
|
|
provider := gomatrixserverlib.NewAuthEvents(stateEvents)
|
|
|
|
if err = gomatrixserverlib.Allowed(e, &provider); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 403,
|
|
|
|
JSON: jsonerror.Forbidden(err.Error()), // TODO: Is this error string comprehensible to the client?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the new event to the roomserver
|
2017-06-27 14:28:44 +00:00
|
|
|
if err := producer.SendEvents([]gomatrixserverlib.Event{e}, cfg.Matrix.ServerName); err != nil {
|
2017-03-15 11:22:40 +00:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
2017-03-17 11:21:52 +00:00
|
|
|
JSON: sendEventResponse{e.EventID()},
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
|
|
|
}
|