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"
|
2022-03-29 12:14:35 +00:00
|
|
|
"strconv"
|
2020-11-09 18:46:11 +00:00
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-09-27 13:01:34 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
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"
|
2022-09-30 11:48:10 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/streams"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-11-09 18:46:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OutputReceiptEventConsumer consumes events that originated in the EDU server.
|
|
|
|
type OutputReceiptEventConsumer struct {
|
2022-11-15 15:05:23 +00:00
|
|
|
ctx context.Context
|
|
|
|
jetstream nats.JetStreamContext
|
|
|
|
durable string
|
|
|
|
topic string
|
|
|
|
db storage.Database
|
|
|
|
stream streams.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,
|
2022-09-30 11:48:10 +00:00
|
|
|
stream streams.StreamProvider,
|
2020-11-09 18:46:11 +00:00
|
|
|
) *OutputReceiptEventConsumer {
|
2022-01-05 17:44:49 +00:00
|
|
|
return &OutputReceiptEventConsumer{
|
2022-11-15 15:05:23 +00:00
|
|
|
ctx: process.Context(),
|
|
|
|
jetstream: js,
|
|
|
|
topic: cfg.Matrix.JetStream.Prefixed(jetstream.OutputReceiptEvent),
|
|
|
|
durable: cfg.Matrix.JetStream.Durable("SyncAPIReceiptConsumer"),
|
|
|
|
db: store,
|
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:14:35 +00:00
|
|
|
// Start consuming receipts events.
|
2020-11-09 18:46:11 +00:00
|
|
|
func (s *OutputReceiptEventConsumer) Start() error {
|
2022-02-02 13:32:48 +00:00
|
|
|
return jetstream.JetStreamConsumer(
|
2022-08-31 11:21:56 +00:00
|
|
|
s.ctx, s.jetstream, s.topic, s.durable, 1,
|
|
|
|
s.onMessage, nats.DeliverAll(), nats.ManualAck(),
|
2022-02-02 13:32:48 +00:00
|
|
|
)
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:21:56 +00:00
|
|
|
func (s *OutputReceiptEventConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) bool {
|
|
|
|
msg := msgs[0] // Guaranteed to exist if onMessage is called
|
2022-03-29 12:14:35 +00:00
|
|
|
output := types.OutputReceiptEvent{
|
|
|
|
UserID: msg.Header.Get(jetstream.UserID),
|
|
|
|
RoomID: msg.Header.Get(jetstream.RoomID),
|
|
|
|
EventID: msg.Header.Get(jetstream.EventID),
|
|
|
|
Type: msg.Header.Get("type"),
|
|
|
|
}
|
|
|
|
|
2022-07-05 16:13:26 +00:00
|
|
|
timestamp, err := strconv.ParseUint(msg.Header.Get("timestamp"), 10, 64)
|
2022-03-29 12:14:35 +00:00
|
|
|
if err != nil {
|
2022-02-02 13:32:48 +00:00
|
|
|
// If the message was invalid, log it and move on to the next message in the stream
|
2022-03-29 12:14:35 +00:00
|
|
|
log.WithError(err).Errorf("output log: message parse failure")
|
2022-02-02 13:32:48 +00:00
|
|
|
sentry.CaptureException(err)
|
|
|
|
return true
|
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
|
2023-04-19 14:50:33 +00:00
|
|
|
output.Timestamp = spec.Timestamp(timestamp)
|
2022-03-29 12:14:35 +00:00
|
|
|
|
2022-02-02 13:32:48 +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-02-02 13:32:48 +00:00
|
|
|
s.stream.Advance(streamPos)
|
|
|
|
s.notifier.OnNewReceipt(output.RoomID, types.StreamingToken{ReceiptPosition: streamPos})
|
2020-11-09 18:46:11 +00:00
|
|
|
|
2022-02-02 13:32:48 +00:00
|
|
|
return true
|
2020-11-09 18:46:11 +00:00
|
|
|
}
|