2020-11-09 12:46:11 -06: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 07:14:35 -05:00
|
|
|
"strconv"
|
2020-11-09 12:46:11 -06:00
|
|
|
|
2021-03-24 05:25:24 -05:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-09-27 08:01:34 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-01-05 11:44:49 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-01-26 06:56:20 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2020-11-09 12:46:11 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2022-09-30 06:48:10 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/streams"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2020-11-09 12:46:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// OutputReceiptEventConsumer consumes events that originated in the EDU server.
|
|
|
|
type OutputReceiptEventConsumer struct {
|
2022-11-15 09:05:23 -06:00
|
|
|
ctx context.Context
|
|
|
|
jetstream nats.JetStreamContext
|
|
|
|
durable string
|
|
|
|
topic string
|
|
|
|
db storage.Database
|
|
|
|
stream streams.StreamProvider
|
|
|
|
notifier *notifier.Notifier
|
2020-11-09 12:46:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputReceiptEventConsumer creates a new OutputReceiptEventConsumer.
|
|
|
|
// Call Start() to begin consuming from the EDU server.
|
|
|
|
func NewOutputReceiptEventConsumer(
|
2021-01-26 06:56:20 -06:00
|
|
|
process *process.ProcessContext,
|
2020-11-09 12:46:11 -06:00
|
|
|
cfg *config.SyncAPI,
|
2022-01-05 11:44:49 -06:00
|
|
|
js nats.JetStreamContext,
|
2020-11-09 12:46:11 -06:00
|
|
|
store storage.Database,
|
2021-01-08 10:59:06 -06:00
|
|
|
notifier *notifier.Notifier,
|
2022-09-30 06:48:10 -05:00
|
|
|
stream streams.StreamProvider,
|
2020-11-09 12:46:11 -06:00
|
|
|
) *OutputReceiptEventConsumer {
|
2022-01-05 11:44:49 -06:00
|
|
|
return &OutputReceiptEventConsumer{
|
2022-11-15 09:05:23 -06: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 12:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:14:35 -05:00
|
|
|
// Start consuming receipts events.
|
2020-11-09 12:46:11 -06:00
|
|
|
func (s *OutputReceiptEventConsumer) Start() error {
|
2022-02-02 07:32:48 -06:00
|
|
|
return jetstream.JetStreamConsumer(
|
2022-08-31 06:21:56 -05:00
|
|
|
s.ctx, s.jetstream, s.topic, s.durable, 1,
|
|
|
|
s.onMessage, nats.DeliverAll(), nats.ManualAck(),
|
2022-02-02 07:32:48 -06:00
|
|
|
)
|
2020-11-09 12:46:11 -06:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:21:56 -05: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 07:14:35 -05: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 11:13:26 -05:00
|
|
|
timestamp, err := strconv.ParseUint(msg.Header.Get("timestamp"), 10, 64)
|
2022-03-29 07:14:35 -05:00
|
|
|
if err != nil {
|
2022-02-02 07:32:48 -06:00
|
|
|
// If the message was invalid, log it and move on to the next message in the stream
|
2022-03-29 07:14:35 -05:00
|
|
|
log.WithError(err).Errorf("output log: message parse failure")
|
2022-02-02 07:32:48 -06:00
|
|
|
sentry.CaptureException(err)
|
|
|
|
return true
|
|
|
|
}
|
2020-11-09 12:46:11 -06:00
|
|
|
|
2022-03-29 07:14:35 -05:00
|
|
|
output.Timestamp = gomatrixserverlib.Timestamp(timestamp)
|
|
|
|
|
2022-02-02 07:32:48 -06: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 10:59:06 -06:00
|
|
|
|
2022-02-02 07:32:48 -06:00
|
|
|
s.stream.Advance(streamPos)
|
|
|
|
s.notifier.OnNewReceipt(output.RoomID, types.StreamingToken{ReceiptPosition: streamPos})
|
2020-11-09 12:46:11 -06:00
|
|
|
|
2022-02-02 07:32:48 -06:00
|
|
|
return true
|
2020-11-09 12:46:11 -06:00
|
|
|
}
|