2018-08-10 15:26:57 +00:00
|
|
|
// 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 (
|
|
|
|
"context"
|
|
|
|
"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"
|
2018-08-10 15:26:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/queue"
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/storage"
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2018-08-10 15:26:57 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// OutputTypingEventConsumer consumes events that originate in EDU server.
|
2018-08-10 15:26:57 +00:00
|
|
|
type OutputTypingEventConsumer struct {
|
2020-05-21 13:40:13 +00:00
|
|
|
consumer *internal.ContinualConsumer
|
2020-01-03 14:07:05 +00:00
|
|
|
db storage.Database
|
2018-08-10 15:26:57 +00:00
|
|
|
queues *queue.OutgoingQueues
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
}
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// NewOutputTypingEventConsumer creates a new OutputTypingEventConsumer. Call Start() to begin consuming from EDU servers.
|
2018-08-10 15:26:57 +00:00
|
|
|
func NewOutputTypingEventConsumer(
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
kafkaConsumer sarama.Consumer,
|
|
|
|
queues *queue.OutgoingQueues,
|
2020-01-03 14:07:05 +00:00
|
|
|
store storage.Database,
|
2018-08-10 15:26:57 +00:00
|
|
|
) *OutputTypingEventConsumer {
|
2020-05-21 13:40:13 +00:00
|
|
|
consumer := internal.ContinualConsumer{
|
2018-08-10 15:26:57 +00:00
|
|
|
Topic: string(cfg.Kafka.Topics.OutputTypingEvent),
|
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
c := &OutputTypingEventConsumer{
|
|
|
|
consumer: &consumer,
|
|
|
|
queues: queues,
|
|
|
|
db: store,
|
|
|
|
ServerName: cfg.Matrix.ServerName,
|
|
|
|
}
|
|
|
|
consumer.ProcessMessage = c.onMessage
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// Start consuming from EDU servers
|
2018-08-10 15:26:57 +00:00
|
|
|
func (t *OutputTypingEventConsumer) Start() error {
|
|
|
|
return t.consumer.Start()
|
|
|
|
}
|
|
|
|
|
2020-03-30 14:02:20 +00:00
|
|
|
// onMessage is called for OutputTypingEvent received from the EDU servers.
|
2018-08-10 15:26:57 +00:00
|
|
|
// Parses the msg, creates a matrix federation EDU and sends it to joined hosts.
|
|
|
|
func (t *OutputTypingEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
|
|
|
// Extract the typing event from msg.
|
|
|
|
var ote api.OutputTypingEvent
|
|
|
|
if err := json.Unmarshal(msg.Value, &ote); err != nil {
|
|
|
|
// Skip this msg but continue processing messages.
|
2020-03-30 14:02:20 +00:00
|
|
|
log.WithError(err).Errorf("eduserver output log: message parse failed")
|
2018-08-10 15:26:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:40:28 +00:00
|
|
|
// only send typing events which originated from us
|
|
|
|
_, typingServerName, err := gomatrixserverlib.SplitID('@', ote.Event.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("user_id", ote.Event.UserID).Error("Failed to extract domain from typing sender")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if typingServerName != t.ServerName {
|
|
|
|
log.WithField("other_server", typingServerName).Info("Suppressing typing notif: originated elsewhere")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:26:57 +00:00
|
|
|
joined, err := t.db.GetJoinedHosts(context.TODO(), ote.Event.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]gomatrixserverlib.ServerName, len(joined))
|
|
|
|
for i := range joined {
|
|
|
|
names[i] = joined[i].ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
edu := &gomatrixserverlib.EDU{Type: ote.Event.Type}
|
|
|
|
if edu.Content, err = json.Marshal(map[string]interface{}{
|
|
|
|
"room_id": ote.Event.RoomID,
|
|
|
|
"user_id": ote.Event.UserID,
|
|
|
|
"typing": ote.Event.Typing,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.queues.SendEDU(edu, t.ServerName, names)
|
|
|
|
}
|