2020-11-09 18:46:11 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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 consumers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
2021-03-24 10:25:24 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2020-11-09 18:46:11 +00:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2021-01-26 12:56:20 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2020-11-09 18:46:11 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2020-11-09 18:46:11 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OutputReceiptEventConsumer consumes events that originated in the EDU server.
|
|
|
|
type OutputReceiptEventConsumer struct {
|
|
|
|
receiptConsumer *internal.ContinualConsumer
|
|
|
|
db storage.Database
|
2021-01-08 16:59:06 +00:00
|
|
|
stream types.StreamProvider
|
|
|
|
notifier *notifier.Notifier
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputReceiptEventConsumer creates a new OutputReceiptEventConsumer.
|
|
|
|
// Call Start() to begin consuming from the EDU server.
|
|
|
|
func NewOutputReceiptEventConsumer(
|
2021-01-26 12:56:20 +00:00
|
|
|
process *process.ProcessContext,
|
2020-11-09 18:46:11 +00:00
|
|
|
cfg *config.SyncAPI,
|
|
|
|
kafkaConsumer sarama.Consumer,
|
|
|
|
store storage.Database,
|
2021-01-08 16:59:06 +00:00
|
|
|
notifier *notifier.Notifier,
|
|
|
|
stream types.StreamProvider,
|
2020-11-09 18:46:11 +00:00
|
|
|
) *OutputReceiptEventConsumer {
|
|
|
|
|
|
|
|
consumer := internal.ContinualConsumer{
|
2021-01-26 12:56:20 +00:00
|
|
|
Process: process,
|
2020-11-09 18:46:11 +00:00
|
|
|
ComponentName: "syncapi/eduserver/receipt",
|
|
|
|
Topic: cfg.Matrix.Kafka.TopicFor(config.TopicOutputReceiptEvent),
|
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &OutputReceiptEventConsumer{
|
|
|
|
receiptConsumer: &consumer,
|
|
|
|
db: store,
|
2021-01-08 16:59:06 +00:00
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
consumer.ProcessMessage = s.onMessage
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start consuming from EDU api
|
|
|
|
func (s *OutputReceiptEventConsumer) Start() error {
|
|
|
|
return s.receiptConsumer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OutputReceiptEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
|
|
|
var output api.OutputReceiptEvent
|
|
|
|
if err := json.Unmarshal(msg.Value, &output); err != nil {
|
|
|
|
// If the message was invalid, log it and move on to the next message in the stream
|
|
|
|
log.WithError(err).Errorf("EDU server output log: message parse failure")
|
2021-03-24 10:25:24 +00:00
|
|
|
sentry.CaptureException(err)
|
2020-11-09 18:46:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
streamPos, err := s.db.StoreReceipt(
|
|
|
|
context.TODO(),
|
|
|
|
output.RoomID,
|
|
|
|
output.Type,
|
|
|
|
output.UserID,
|
|
|
|
output.EventID,
|
|
|
|
output.Timestamp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-03-24 10:25:24 +00:00
|
|
|
sentry.CaptureException(err)
|
2020-11-09 18:46:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
|
|
|
|
s.stream.Advance(streamPos)
|
2020-12-18 11:11:21 +00:00
|
|
|
s.notifier.OnNewReceipt(output.RoomID, types.StreamingToken{ReceiptPosition: streamPos})
|
2020-11-09 18:46:11 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|