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 (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-04-22 16:26:56 +02:00
|
|
|
"github.com/Shopify/sarama"
|
2020-03-30 16:02:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2019-07-12 16:59:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/sync"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
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 {
|
2020-05-21 15:40:13 +02:00
|
|
|
typingConsumer *internal.ContinualConsumer
|
2020-01-03 15:07:05 +01:00
|
|
|
db storage.Database
|
2019-07-12 16:59:53 +02:00
|
|
|
notifier *sync.Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
kafkaConsumer sarama.Consumer,
|
|
|
|
n *sync.Notifier,
|
2020-01-03 15:07:05 +01:00
|
|
|
store storage.Database,
|
2019-07-12 16:59:53 +02:00
|
|
|
) *OutputTypingEventConsumer {
|
|
|
|
|
2020-05-21 15:40:13 +02:00
|
|
|
consumer := internal.ContinualConsumer{
|
2019-07-12 16:59:53 +02:00
|
|
|
Topic: string(cfg.Kafka.Topics.OutputTypingEvent),
|
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &OutputTypingEventConsumer{
|
|
|
|
typingConsumer: &consumer,
|
|
|
|
db: store,
|
|
|
|
notifier: n,
|
|
|
|
}
|
|
|
|
|
|
|
|
consumer.ProcessMessage = s.onMessage
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
s.db.SetTypingTimeoutCallback(func(userID, roomID string, latestSyncPosition int64) {
|
2020-01-23 18:51:10 +01:00
|
|
|
s.notifier.OnNewEvent(
|
|
|
|
nil, roomID, nil,
|
2020-05-13 13:14:50 +02:00
|
|
|
types.NewStreamToken(0, types.StreamPosition(latestSyncPosition)),
|
2020-01-23 18:51:10 +01:00
|
|
|
)
|
2019-07-12 16:59:53 +02: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 16:02:20 +02:00
|
|
|
log.WithError(err).Errorf("EDU server output log: message parse failure")
|
2019-07-12 16:59:53 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"room_id": output.Event.RoomID,
|
|
|
|
"user_id": output.Event.UserID,
|
|
|
|
"typing": output.Event.Typing,
|
2020-03-30 16:02:20 +02:00
|
|
|
}).Debug("received data from EDU server")
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2020-01-23 18:51:10 +01:00
|
|
|
var typingPos types.StreamPosition
|
2019-07-12 16:59:53 +02:00
|
|
|
typingEvent := output.Event
|
|
|
|
if typingEvent.Typing {
|
|
|
|
typingPos = s.db.AddTypingUser(typingEvent.UserID, typingEvent.RoomID, output.ExpireTime)
|
|
|
|
} else {
|
|
|
|
typingPos = s.db.RemoveTypingUser(typingEvent.UserID, typingEvent.RoomID)
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:14:50 +02:00
|
|
|
s.notifier.OnNewEvent(nil, output.Event.RoomID, nil, types.NewStreamToken(0, typingPos))
|
2019-07-12 16:59:53 +02:00
|
|
|
return nil
|
|
|
|
}
|