2017-04-20 23:40:52 +01: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-05-23 17:43:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2017-08-04 16:32:10 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/events"
|
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 15:21:04 +01: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 15:21:04 +01:00
|
|
|
func SendEvent(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
2017-09-07 12:50:39 +01:00
|
|
|
roomID, eventType, _ string, stateKey *string,
|
2017-06-19 15:21:04 +01:00
|
|
|
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 17:43:05 +01: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 17:43:05 +01: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
|
|
|
}
|
2017-09-20 10:59:19 +01:00
|
|
|
err := builder.SetContent(r)
|
|
|
|
if err != nil {
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
2017-03-15 11:22:40 +00:00
|
|
|
|
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
2017-09-13 13:37:50 +01:00
|
|
|
e, err := events.BuildEvent(req.Context(), &builder, cfg, queryAPI, &queryRes)
|
2017-08-04 16:32:10 +01:00
|
|
|
if err == events.ErrRoomNoExists {
|
2017-03-15 11:22:40 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 404,
|
|
|
|
JSON: jsonerror.NotFound("Room does not exist"),
|
|
|
|
}
|
2017-08-04 16:32:10 +01:00
|
|
|
} else if err != nil {
|
2017-03-15 11:22:40 +00:00
|
|
|
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)
|
2017-08-04 16:32:10 +01:00
|
|
|
if err = gomatrixserverlib.Allowed(*e, &provider); err != nil {
|
2017-03-15 11:22:40 +00:00
|
|
|
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-09-13 13:37:50 +01:00
|
|
|
if err := producer.SendEvents(
|
|
|
|
req.Context(), []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
|
|
|
}
|
|
|
|
}
|