sendevent.go 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16 17 18 19
package writers

import (
	"net/http"

20
	"fmt"
21 22
	"time"

23
	"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
24 25 26
	"github.com/matrix-org/dendrite/clientapi/config"
	"github.com/matrix-org/dendrite/clientapi/httputil"
	"github.com/matrix-org/dendrite/clientapi/jsonerror"
27
	"github.com/matrix-org/dendrite/clientapi/producers"
28 29
	"github.com/matrix-org/dendrite/roomserver/api"
	"github.com/matrix-org/gomatrixserverlib"
30 31 32
	"github.com/matrix-org/util"
)

33
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
K
Kegsay 已提交
34 35
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
type sendEventResponse struct {
36 37 38
	EventID string `json:"event_id"`
}

K
Kegsay 已提交
39 40 41
// SendEvent implements:
//   /rooms/{roomID}/send/{eventType}/{txnID}
//   /rooms/{roomID}/state/{eventType}/{stateKey}
42
func SendEvent(req *http.Request, device *authtypes.Device, roomID, eventType, txnID string, stateKey *string, cfg config.ClientAPI, queryAPI api.RoomserverQueryAPI, producer *producers.RoomserverProducer) util.JSONResponse {
43
	// parse the incoming http request
44
	userID := device.UserID
45
	var r map[string]interface{} // must be a JSON object
46
	resErr := httputil.UnmarshalJSONRequest(req, &r)
47 48 49 50 51 52 53 54 55
	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,
K
Kegsay 已提交
56
		StateKey: stateKey,
57 58 59 60
	}
	builder.SetContent(r)

	// work out what will be required in order to send this event
61
	needed, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
62 63 64 65 66 67 68
	if err != nil {
		return httputil.LogThenError(req, err)
	}

	// Ask the roomserver for information about this room
	queryReq := api.QueryLatestEventsAndStateRequest{
		RoomID:       roomID,
69
		StateToFetch: needed.Tuples(),
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	}
	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
	eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.ServerName)
	e, err := builder.Build(eventID, time.Now(), cfg.ServerName, cfg.KeyID, cfg.PrivateKey)
	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
109
	if err := producer.SendEvents([]gomatrixserverlib.Event{e}); err != nil {
110 111 112 113 114
		return httputil.LogThenError(req, err)
	}

	return util.JSONResponse{
		Code: 200,
K
Kegsay 已提交
115
		JSON: sendEventResponse{e.EventID()},
116 117
	}
}