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"
|
|
|
|
|
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"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-01-05 17:44:49 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
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"
|
2022-01-05 17:44:49 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
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 {
|
2022-01-05 17:44:49 +00:00
|
|
|
ctx context.Context
|
|
|
|
jetstream nats.JetStreamContext
|
2022-01-07 17:31:57 +00:00
|
|
|
durable nats.SubOpt
|
2022-01-05 17:44:49 +00:00
|
|
|
topic string
|
|
|
|
db storage.Database
|
|
|
|
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,
|
2022-01-05 17:44:49 +00:00
|
|
|
js nats.JetStreamContext,
|
2020-11-09 18:46:11 +00:00
|
|
|
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 {
|
2022-01-05 17:44:49 +00:00
|
|
|
return &OutputReceiptEventConsumer{
|
|
|
|
ctx: process.Context(),
|
|
|
|
jetstream: js,
|
|
|
|
topic: cfg.Matrix.JetStream.TopicFor(jetstream.OutputReceiptEvent),
|
2022-01-07 17:31:57 +00:00
|
|
|
durable: cfg.Matrix.JetStream.Durable("SyncAPIEDUServerReceiptConsumer"),
|
2022-01-05 17:44:49 +00:00
|
|
|
db: store,
|
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start consuming from EDU api
|
|
|
|
func (s *OutputReceiptEventConsumer) Start() error {
|
2022-01-07 17:31:57 +00:00
|
|
|
_, err := s.jetstream.Subscribe(s.topic, s.onMessage, s.durable)
|
2022-01-05 17:44:49 +00:00
|
|
|
return err
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 17:44:49 +00:00
|
|
|
func (s *OutputReceiptEventConsumer) onMessage(msg *nats.Msg) {
|
|
|
|
jetstream.WithJetStreamMessage(msg, func(msg *nats.Msg) bool {
|
|
|
|
var output api.OutputReceiptEvent
|
|
|
|
if err := json.Unmarshal(msg.Data, &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")
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
return true
|
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
|
2022-01-05 17:44:49 +00:00
|
|
|
streamPos, err := s.db.StoreReceipt(
|
|
|
|
s.ctx,
|
|
|
|
output.RoomID,
|
|
|
|
output.Type,
|
|
|
|
output.UserID,
|
|
|
|
output.EventID,
|
|
|
|
output.Timestamp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
return true
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2022-01-05 17:44:49 +00:00
|
|
|
s.stream.Advance(streamPos)
|
|
|
|
s.notifier.OnNewReceipt(output.RoomID, types.StreamingToken{ReceiptPosition: streamPos})
|
2020-11-09 18:46:11 +00:00
|
|
|
|
2022-01-05 17:44:49 +00:00
|
|
|
return true
|
|
|
|
})
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|