2019-07-12 14:59:53 +00: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 (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-04-22 14:26:56 +00:00
|
|
|
"github.com/Shopify/sarama"
|
2020-03-30 14:02:20 +00:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/cache"
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2019-07-12 14:59:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// OutputTypingEventConsumer consumes events that originated in the EDU server.
|
2019-07-12 14:59:53 +00:00
|
|
|
type OutputTypingEventConsumer struct {
|
2020-05-21 13:40:13 +00:00
|
|
|
typingConsumer *internal.ContinualConsumer
|
2021-01-08 16:59:06 +00:00
|
|
|
eduCache *cache.EDUCache
|
|
|
|
stream types.StreamProvider
|
|
|
|
notifier *notifier.Notifier
|
2019-07-12 14:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputTypingEventConsumer creates a new OutputTypingEventConsumer.
|
2020-03-30 14:02:20 +00:00
|
|
|
// Call Start() to begin consuming from the EDU server.
|
2019-07-12 14:59:53 +00:00
|
|
|
func NewOutputTypingEventConsumer(
|
2020-08-10 13:18:04 +00:00
|
|
|
cfg *config.SyncAPI,
|
2019-07-12 14:59:53 +00:00
|
|
|
kafkaConsumer sarama.Consumer,
|
2020-01-03 14:07:05 +00:00
|
|
|
store storage.Database,
|
2021-01-08 16:59:06 +00:00
|
|
|
eduCache *cache.EDUCache,
|
|
|
|
notifier *notifier.Notifier,
|
|
|
|
stream types.StreamProvider,
|
2019-07-12 14:59:53 +00:00
|
|
|
) *OutputTypingEventConsumer {
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
consumer := internal.ContinualConsumer{
|
2020-09-01 15:53:38 +00:00
|
|
|
ComponentName: "syncapi/eduserver/typing",
|
2020-08-10 14:18:37 +00:00
|
|
|
Topic: string(cfg.Matrix.Kafka.TopicFor(config.TopicOutputTypingEvent)),
|
2019-07-12 14:59:53 +00:00
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &OutputTypingEventConsumer{
|
|
|
|
typingConsumer: &consumer,
|
2021-01-08 16:59:06 +00:00
|
|
|
eduCache: eduCache,
|
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2019-07-12 14:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
consumer.ProcessMessage = s.onMessage
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// Start consuming from EDU api
|
2019-07-12 14:59:53 +00:00
|
|
|
func (s *OutputTypingEventConsumer) Start() error {
|
2021-01-08 16:59:06 +00:00
|
|
|
s.eduCache.SetTimeoutCallback(func(userID, roomID string, latestSyncPosition int64) {
|
|
|
|
pos := types.StreamPosition(latestSyncPosition)
|
|
|
|
s.notifier.OnNewTyping(roomID, types.StreamingToken{TypingPosition: pos})
|
2019-07-12 14:59:53 +00:00
|
|
|
})
|
|
|
|
return s.typingConsumer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OutputTypingEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
|
|
|
var output api.OutputTypingEvent
|
|
|
|
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
|
2020-03-30 14:02:20 +00:00
|
|
|
log.WithError(err).Errorf("EDU server output log: message parse failure")
|
2019-07-12 14:59:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"room_id": output.Event.RoomID,
|
|
|
|
"user_id": output.Event.UserID,
|
|
|
|
"typing": output.Event.Typing,
|
2020-03-30 14:02:20 +00:00
|
|
|
}).Debug("received data from EDU server")
|
2019-07-12 14:59:53 +00:00
|
|
|
|
2020-01-23 17:51:10 +00:00
|
|
|
var typingPos types.StreamPosition
|
2019-07-12 14:59:53 +00:00
|
|
|
typingEvent := output.Event
|
|
|
|
if typingEvent.Typing {
|
2021-01-08 16:59:06 +00:00
|
|
|
typingPos = types.StreamPosition(
|
|
|
|
s.eduCache.AddTypingUser(typingEvent.UserID, typingEvent.RoomID, output.ExpireTime),
|
|
|
|
)
|
2019-07-12 14:59:53 +00:00
|
|
|
} else {
|
2021-01-08 16:59:06 +00:00
|
|
|
typingPos = types.StreamPosition(
|
|
|
|
s.eduCache.RemoveUser(typingEvent.UserID, typingEvent.RoomID),
|
|
|
|
)
|
2019-07-12 14:59:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
s.stream.Advance(typingPos)
|
2020-12-18 11:11:21 +00:00
|
|
|
s.notifier.OnNewTyping(output.Event.RoomID, types.StreamingToken{TypingPosition: typingPos})
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2019-07-12 14:59:53 +00:00
|
|
|
return nil
|
|
|
|
}
|