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