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
|
2020-05-01 09:48:17 +00:00
|
|
|
package internal
|
2017-07-13 10:41:30 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
2017-07-13 10:41:30 +00:00
|
|
|
"encoding/json"
|
2020-08-20 15:24:33 +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/roomserver/api"
|
2020-06-12 11:10:08 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-07-13 10:41:30 +00:00
|
|
|
)
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
// WriteOutputEvents implements OutputRoomEventWriter
|
2020-05-01 09:48:17 +00:00
|
|
|
func (r *RoomserverInternalAPI) WriteOutputEvents(roomID string, updates []api.OutputEvent) error {
|
2017-08-08 15:38:03 +00:00
|
|
|
messages := make([]*sarama.ProducerMessage, len(updates))
|
|
|
|
for i := range updates {
|
|
|
|
value, err := json.Marshal(updates[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-12 11:10:08 +00:00
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"room_id": roomID,
|
|
|
|
"type": updates[i].Type,
|
|
|
|
})
|
|
|
|
if updates[i].NewRoomEvent != nil {
|
|
|
|
logger = logger.WithFields(log.Fields{
|
|
|
|
"event_type": updates[i].NewRoomEvent.Event.Type(),
|
|
|
|
"event_id": updates[i].NewRoomEvent.Event.EventID(),
|
|
|
|
"adds_state": len(updates[i].NewRoomEvent.AddsStateEventIDs),
|
|
|
|
"removes_state": len(updates[i].NewRoomEvent.RemovesStateEventIDs),
|
|
|
|
"send_as_server": updates[i].NewRoomEvent.SendAsServer,
|
|
|
|
"sender": updates[i].NewRoomEvent.Event.Sender(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
logger.Infof("Producing to topic '%s'", r.OutputRoomEventTopic)
|
2017-08-08 15:38:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// InputRoomEvents implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) 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) {
|
2020-08-20 15:24:33 +00:00
|
|
|
for i, e := range request.InputRoomEvents {
|
|
|
|
roomID := "global"
|
|
|
|
if r.DB.SupportsConcurrentRoomInputs() {
|
|
|
|
roomID = e.Event.RoomID()
|
|
|
|
}
|
|
|
|
mutex, _ := r.mutexes.LoadOrStore(roomID, &sync.Mutex{})
|
|
|
|
mutex.(*sync.Mutex).Lock()
|
2020-05-20 17:03:06 +00:00
|
|
|
if response.EventID, err = r.processRoomEvent(ctx, request.InputRoomEvents[i]); err != nil {
|
2020-08-20 15:24:33 +00:00
|
|
|
mutex.(*sync.Mutex).Unlock()
|
2017-08-21 15:37:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-20 15:24:33 +00:00
|
|
|
mutex.(*sync.Mutex).Unlock()
|
2017-08-21 15:37:11 +00:00
|
|
|
}
|
2017-07-13 10:41:30 +00:00
|
|
|
return nil
|
|
|
|
}
|