2019-07-12 16:59:53 +02:00
|
|
|
// Copyright 2019 Alex Chen
|
|
|
|
//
|
|
|
|
// 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 (
|
2022-01-05 18:44:49 +01:00
|
|
|
"context"
|
2019-07-12 16:59:53 +02:00
|
|
|
"encoding/json"
|
|
|
|
|
2021-03-24 11:25:24 +01:00
|
|
|
"github.com/getsentry/sentry-go"
|
2020-03-30 16:02:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/cache"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-01-05 18:44:49 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-01-26 13:56:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2019-07-12 16:59:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2022-01-05 18:44:49 +01:00
|
|
|
"github.com/nats-io/nats.go"
|
2019-07-12 16:59:53 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-03-30 16:02:20 +02:00
|
|
|
// OutputTypingEventConsumer consumes events that originated in the EDU server.
|
2019-07-12 16:59:53 +02:00
|
|
|
type OutputTypingEventConsumer struct {
|
2022-01-05 18:44:49 +01:00
|
|
|
ctx context.Context
|
|
|
|
jetstream nats.JetStreamContext
|
2022-01-07 18:31:57 +01:00
|
|
|
durable nats.SubOpt
|
2022-01-05 18:44:49 +01:00
|
|
|
topic string
|
|
|
|
eduCache *cache.EDUCache
|
|
|
|
stream types.StreamProvider
|
|
|
|
notifier *notifier.Notifier
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputTypingEventConsumer creates a new OutputTypingEventConsumer.
|
2020-03-30 16:02:20 +02:00
|
|
|
// Call Start() to begin consuming from the EDU server.
|
2019-07-12 16:59:53 +02:00
|
|
|
func NewOutputTypingEventConsumer(
|
2021-01-26 13:56:20 +01:00
|
|
|
process *process.ProcessContext,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.SyncAPI,
|
2022-01-05 18:44:49 +01:00
|
|
|
js nats.JetStreamContext,
|
2020-01-03 15:07:05 +01:00
|
|
|
store storage.Database,
|
2021-01-08 17:59:06 +01:00
|
|
|
eduCache *cache.EDUCache,
|
|
|
|
notifier *notifier.Notifier,
|
|
|
|
stream types.StreamProvider,
|
2019-07-12 16:59:53 +02:00
|
|
|
) *OutputTypingEventConsumer {
|
2022-01-05 18:44:49 +01:00
|
|
|
return &OutputTypingEventConsumer{
|
|
|
|
ctx: process.Context(),
|
|
|
|
jetstream: js,
|
|
|
|
topic: cfg.Matrix.JetStream.TopicFor(jetstream.OutputTypingEvent),
|
2022-01-07 18:31:57 +01:00
|
|
|
durable: cfg.Matrix.JetStream.Durable("SyncAPIEDUServerTypingConsumer"),
|
2022-01-05 18:44:49 +01:00
|
|
|
eduCache: eduCache,
|
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:02:20 +02:00
|
|
|
// Start consuming from EDU api
|
2019-07-12 16:59:53 +02:00
|
|
|
func (s *OutputTypingEventConsumer) Start() error {
|
2022-01-07 18:31:57 +01:00
|
|
|
_, err := s.jetstream.Subscribe(s.topic, s.onMessage, s.durable)
|
2022-01-05 18:44:49 +01:00
|
|
|
return err
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:44:49 +01:00
|
|
|
func (s *OutputTypingEventConsumer) onMessage(msg *nats.Msg) {
|
|
|
|
jetstream.WithJetStreamMessage(msg, func(msg *nats.Msg) bool {
|
|
|
|
var output api.OutputTypingEvent
|
|
|
|
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
|
|
|
|
}
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-01-05 18:44:49 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"room_id": output.Event.RoomID,
|
|
|
|
"user_id": output.Event.UserID,
|
|
|
|
"typing": output.Event.Typing,
|
|
|
|
}).Debug("received data from EDU server")
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-01-05 18:44:49 +01:00
|
|
|
var typingPos types.StreamPosition
|
|
|
|
typingEvent := output.Event
|
|
|
|
if typingEvent.Typing {
|
|
|
|
typingPos = types.StreamPosition(
|
|
|
|
s.eduCache.AddTypingUser(typingEvent.UserID, typingEvent.RoomID, output.ExpireTime),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
typingPos = types.StreamPosition(
|
|
|
|
s.eduCache.RemoveUser(typingEvent.UserID, typingEvent.RoomID),
|
|
|
|
)
|
|
|
|
}
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-01-05 18:44:49 +01:00
|
|
|
s.stream.Advance(typingPos)
|
|
|
|
s.notifier.OnNewTyping(output.Event.RoomID, types.StreamingToken{TypingPosition: typingPos})
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2022-01-05 18:44:49 +01:00
|
|
|
return true
|
|
|
|
})
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|