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"
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2019-07-12 14:59:53 +00: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 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
|
2020-01-03 14:07:05 +00:00
|
|
|
db storage.Database
|
2019-07-12 14:59:53 +00:00
|
|
|
notifier *sync.Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
n *sync.Notifier,
|
2020-01-03 14:07:05 +00:00
|
|
|
store storage.Database,
|
2019-07-12 14:59:53 +00:00
|
|
|
) *OutputTypingEventConsumer {
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
consumer := internal.ContinualConsumer{
|
2020-08-10 13:18:04 +00:00
|
|
|
Topic: string(cfg.Matrix.Kafka.Topics.OutputTypingEvent),
|
2019-07-12 14:59:53 +00:00
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &OutputTypingEventConsumer{
|
|
|
|
typingConsumer: &consumer,
|
|
|
|
db: store,
|
|
|
|
notifier: n,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
s.db.SetTypingTimeoutCallback(func(userID, roomID string, latestSyncPosition int64) {
|
2020-01-23 17:51:10 +00:00
|
|
|
s.notifier.OnNewEvent(
|
|
|
|
nil, roomID, nil,
|
2020-07-29 18:00:04 +00:00
|
|
|
types.NewStreamToken(0, types.StreamPosition(latestSyncPosition), nil),
|
2020-01-23 17:51:10 +00:00
|
|
|
)
|
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 {
|
|
|
|
typingPos = s.db.AddTypingUser(typingEvent.UserID, typingEvent.RoomID, output.ExpireTime)
|
|
|
|
} else {
|
|
|
|
typingPos = s.db.RemoveTypingUser(typingEvent.UserID, typingEvent.RoomID)
|
|
|
|
}
|
|
|
|
|
2020-07-29 18:00:04 +00:00
|
|
|
s.notifier.OnNewEvent(nil, output.Event.RoomID, nil, types.NewStreamToken(0, typingPos, nil))
|
2019-07-12 14:59:53 +00:00
|
|
|
return nil
|
|
|
|
}
|