2017-07-13 10:41:30 +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.
|
|
|
|
|
|
|
|
// Package input contains the code processes new room events
|
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
2017-07-13 10:41:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2017-12-15 15:22:06 +00:00
|
|
|
"sync"
|
2017-07-13 10:41:30 +00:00
|
|
|
|
2020-04-22 14:26:56 +00:00
|
|
|
"github.com/Shopify/sarama"
|
2017-07-13 10:41:30 +00:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverInputAPI implements api.RoomserverInputAPI
|
|
|
|
type RoomserverInputAPI struct {
|
|
|
|
DB RoomEventDatabase
|
|
|
|
Producer sarama.SyncProducer
|
|
|
|
// The kafkaesque topic to output new room events to.
|
|
|
|
// This is the name used in kafka to identify the stream to write events to.
|
|
|
|
OutputRoomEventTopic string
|
2017-12-15 15:22:06 +00:00
|
|
|
// Protects calls to processRoomEvent
|
|
|
|
mutex sync.Mutex
|
2017-07-13 10:41:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
// WriteOutputEvents implements OutputRoomEventWriter
|
|
|
|
func (r *RoomserverInputAPI) WriteOutputEvents(roomID string, updates []api.OutputEvent) error {
|
|
|
|
messages := make([]*sarama.ProducerMessage, len(updates))
|
|
|
|
for i := range updates {
|
|
|
|
value, err := json.Marshal(updates[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
messages[i] = &sarama.ProducerMessage{
|
|
|
|
Topic: r.OutputRoomEventTopic,
|
|
|
|
Key: sarama.StringEncoder(roomID),
|
|
|
|
Value: sarama.ByteEncoder(value),
|
|
|
|
}
|
2017-07-13 10:41:30 +00:00
|
|
|
}
|
2017-08-08 15:38:03 +00:00
|
|
|
return r.Producer.SendMessages(messages)
|
2017-07-13 10:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputRoomEvents implements api.RoomserverInputAPI
|
|
|
|
func (r *RoomserverInputAPI) InputRoomEvents(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2017-07-13 10:41:30 +00:00
|
|
|
request *api.InputRoomEventsRequest,
|
|
|
|
response *api.InputRoomEventsResponse,
|
2018-05-26 11:03:35 +00:00
|
|
|
) (err error) {
|
2017-12-19 11:29:49 +00:00
|
|
|
// We lock as processRoomEvent can only be called once at a time
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
2017-07-13 10:41:30 +00:00
|
|
|
for i := range request.InputRoomEvents {
|
2018-05-26 11:03:35 +00:00
|
|
|
if response.EventID, err = processRoomEvent(ctx, r.DB, r, request.InputRoomEvents[i]); err != nil {
|
2017-07-13 10:41:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 15:37:11 +00:00
|
|
|
for i := range request.InputInviteEvents {
|
2018-05-26 11:03:35 +00:00
|
|
|
if err = processInviteEvent(ctx, r.DB, r, request.InputInviteEvents[i]); err != nil {
|
2017-08-21 15:37:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 10:41:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupHTTP adds the RoomserverInputAPI handlers to the http.ServeMux.
|
|
|
|
func (r *RoomserverInputAPI) SetupHTTP(servMux *http.ServeMux) {
|
|
|
|
servMux.Handle(api.RoomserverInputRoomEventsPath,
|
2017-09-28 13:50:40 +00:00
|
|
|
common.MakeInternalAPI("inputRoomEvents", func(req *http.Request) util.JSONResponse {
|
2017-07-13 10:41:30 +00:00
|
|
|
var request api.InputRoomEventsRequest
|
|
|
|
var response api.InputRoomEventsResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
2018-03-13 15:55:45 +00:00
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
2017-07-13 10:41:30 +00:00
|
|
|
}
|
2017-09-13 12:37:50 +00:00
|
|
|
if err := r.InputRoomEvents(req.Context(), &request, &response); err != nil {
|
2017-07-13 10:41:30 +00:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 15:55:45 +00:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-07-13 10:41:30 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|