2020-08-04 12:41:48 +02:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
2018-08-10 17:26:57 +02: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-03-30 16:02:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
2018-08-10 17:26:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/queue"
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/storage"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2021-07-14 14:34:42 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-01-26 13:56:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2018-08-10 17:26:57 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-14 13:33:37 +02:00
|
|
|
"github.com/matrix-org/util"
|
2021-11-03 15:28:40 +01:00
|
|
|
"github.com/nats-io/nats.go"
|
2018-08-10 17:26:57 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-07-14 13:33:37 +02:00
|
|
|
// OutputEDUConsumer consumes events that originate in EDU server.
|
|
|
|
type OutputEDUConsumer struct {
|
2021-11-03 15:28:40 +01:00
|
|
|
jetstream nats.JetStreamContext
|
|
|
|
db storage.Database
|
|
|
|
queues *queue.OutgoingQueues
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
typingTopic string
|
|
|
|
sendToDeviceTopic string
|
|
|
|
receiptTopic string
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:33:37 +02:00
|
|
|
// NewOutputEDUConsumer creates a new OutputEDUConsumer. Call Start() to begin consuming from EDU servers.
|
|
|
|
func NewOutputEDUConsumer(
|
2021-01-26 13:56:20 +01:00
|
|
|
process *process.ProcessContext,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationSender,
|
2021-11-03 15:28:40 +01:00
|
|
|
js nats.JetStreamContext,
|
2018-08-10 17:26:57 +02:00
|
|
|
queues *queue.OutgoingQueues,
|
2020-01-03 15:07:05 +01:00
|
|
|
store storage.Database,
|
2020-07-14 13:33:37 +02:00
|
|
|
) *OutputEDUConsumer {
|
2021-11-03 15:28:40 +01:00
|
|
|
return &OutputEDUConsumer{
|
|
|
|
jetstream: js,
|
2020-07-14 13:33:37 +02:00
|
|
|
queues: queues,
|
|
|
|
db: store,
|
|
|
|
ServerName: cfg.Matrix.ServerName,
|
2021-11-03 15:28:40 +01:00
|
|
|
typingTopic: cfg.Matrix.JetStream.TopicFor(jetstream.OutputTypingEvent),
|
|
|
|
sendToDeviceTopic: cfg.Matrix.JetStream.TopicFor(jetstream.OutputSendToDeviceEvent),
|
|
|
|
receiptTopic: cfg.Matrix.JetStream.TopicFor(jetstream.OutputReceiptEvent),
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:02:20 +02:00
|
|
|
// Start consuming from EDU servers
|
2020-07-14 13:33:37 +02:00
|
|
|
func (t *OutputEDUConsumer) Start() error {
|
2021-11-03 15:28:40 +01:00
|
|
|
if _, err := t.jetstream.Subscribe(t.typingTopic, t.onTypingEvent); err != nil {
|
|
|
|
return err
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
2021-11-03 15:28:40 +01:00
|
|
|
if _, err := t.jetstream.Subscribe(t.sendToDeviceTopic, t.onSendToDeviceEvent); err != nil {
|
|
|
|
return err
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
2021-11-03 15:28:40 +01:00
|
|
|
if _, err := t.jetstream.Subscribe(t.receiptTopic, t.onReceiptEvent); err != nil {
|
|
|
|
return err
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
2020-07-14 13:33:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// onSendToDeviceEvent is called in response to a message received on the
|
|
|
|
// send-to-device events topic from the EDU server.
|
2021-11-03 15:28:40 +01:00
|
|
|
func (t *OutputEDUConsumer) onSendToDeviceEvent(msg *nats.Msg) {
|
2020-07-14 13:33:37 +02:00
|
|
|
// Extract the send-to-device event from msg.
|
|
|
|
var ote api.OutputSendToDeviceEvent
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := json.Unmarshal(msg.Data, &ote); err != nil {
|
2020-07-14 13:33:37 +02:00
|
|
|
log.WithError(err).Errorf("eduserver output log: message parse failed (expected send-to-device)")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// only send send-to-device events which originated from us
|
|
|
|
_, originServerName, err := gomatrixserverlib.SplitID('@', ote.Sender)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("user_id", ote.Sender).Error("Failed to extract domain from send-to-device sender")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
|
|
|
if originServerName != t.ServerName {
|
|
|
|
log.WithField("other_server", originServerName).Info("Suppressing send-to-device: originated elsewhere")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, destServerName, err := gomatrixserverlib.SplitID('@', ote.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("user_id", ote.UserID).Error("Failed to extract domain from send-to-device destination")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pack the EDU and marshal it
|
|
|
|
edu := &gomatrixserverlib.EDU{
|
|
|
|
Type: gomatrixserverlib.MDirectToDevice,
|
|
|
|
Origin: string(t.ServerName),
|
|
|
|
}
|
|
|
|
tdm := gomatrixserverlib.ToDeviceMessage{
|
|
|
|
Sender: ote.Sender,
|
|
|
|
Type: ote.Type,
|
|
|
|
MessageID: util.RandomString(32),
|
|
|
|
Messages: map[string]map[string]json.RawMessage{
|
|
|
|
ote.UserID: {
|
|
|
|
ote.DeviceID: ote.Content,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if edu.Content, err = json.Marshal(tdm); err != nil {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).Error("failed to marshal EDU JSON")
|
|
|
|
return
|
2020-07-14 13:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Sending send-to-device message into %q destination queue", destServerName)
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := t.queues.SendEDU(edu, t.ServerName, []gomatrixserverlib.ServerName{destServerName}); err != nil {
|
|
|
|
log.WithError(err).Error("failed to send EDU")
|
|
|
|
}
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:33:37 +02:00
|
|
|
// onTypingEvent is called in response to a message received on the typing
|
|
|
|
// events topic from the EDU server.
|
2021-11-03 15:28:40 +01:00
|
|
|
func (t *OutputEDUConsumer) onTypingEvent(msg *nats.Msg) {
|
2018-08-10 17:26:57 +02:00
|
|
|
// Extract the typing event from msg.
|
|
|
|
var ote api.OutputTypingEvent
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := json.Unmarshal(msg.Data, &ote); err != nil {
|
2018-08-10 17:26:57 +02:00
|
|
|
// Skip this msg but continue processing messages.
|
2020-07-14 13:33:37 +02:00
|
|
|
log.WithError(err).Errorf("eduserver output log: message parse failed (expected typing)")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 17:40:28 +02: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")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-03-30 17:40:28 +02:00
|
|
|
}
|
|
|
|
if typingServerName != t.ServerName {
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-03-30 17:40:28 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:26:57 +02:00
|
|
|
joined, err := t.db.GetJoinedHosts(context.TODO(), ote.Event.RoomID)
|
|
|
|
if err != nil {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).WithField("room_id", ote.Event.RoomID).Error("failed to get joined hosts for room")
|
|
|
|
return
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).Error("failed to marshal EDU JSON")
|
|
|
|
return
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := t.queues.SendEDU(edu, t.ServerName, names); err != nil {
|
|
|
|
log.WithError(err).Error("failed to send EDU")
|
|
|
|
}
|
2018-08-10 17:26:57 +02:00
|
|
|
}
|
2020-11-09 19:46:11 +01:00
|
|
|
|
|
|
|
// onReceiptEvent is called in response to a message received on the receipt
|
|
|
|
// events topic from the EDU server.
|
2021-11-03 15:28:40 +01:00
|
|
|
func (t *OutputEDUConsumer) onReceiptEvent(msg *nats.Msg) {
|
2020-11-09 19:46:11 +01:00
|
|
|
// Extract the typing event from msg.
|
|
|
|
var receipt api.OutputReceiptEvent
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := json.Unmarshal(msg.Data, &receipt); err != nil {
|
2020-11-09 19:46:11 +01:00
|
|
|
// Skip this msg but continue processing messages.
|
|
|
|
log.WithError(err).Errorf("eduserver output log: message parse failed (expected receipt)")
|
2021-11-03 15:28:40 +01:00
|
|
|
return
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// only send receipt events which originated from us
|
|
|
|
_, receiptServerName, err := gomatrixserverlib.SplitID('@', receipt.UserID)
|
|
|
|
if err != nil {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).WithField("user_id", receipt.UserID).Error("failed to extract domain from receipt sender")
|
|
|
|
return
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
|
|
|
if receiptServerName != t.ServerName {
|
2021-11-03 15:28:40 +01:00
|
|
|
return // don't log, very spammy as it logs for each remote receipt
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
joined, err := t.db.GetJoinedHosts(context.TODO(), receipt.RoomID)
|
|
|
|
if err != nil {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).WithField("room_id", receipt.RoomID).Error("failed to get joined hosts for room")
|
|
|
|
return
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]gomatrixserverlib.ServerName, len(joined))
|
|
|
|
for i := range joined {
|
|
|
|
names[i] = joined[i].ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
content := map[string]api.FederationReceiptMRead{}
|
|
|
|
content[receipt.RoomID] = api.FederationReceiptMRead{
|
|
|
|
User: map[string]api.FederationReceiptData{
|
|
|
|
receipt.UserID: {
|
|
|
|
Data: api.ReceiptTS{
|
|
|
|
TS: receipt.Timestamp,
|
|
|
|
},
|
|
|
|
EventIDs: []string{receipt.EventID},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
edu := &gomatrixserverlib.EDU{
|
|
|
|
Type: gomatrixserverlib.MReceipt,
|
|
|
|
Origin: string(t.ServerName),
|
|
|
|
}
|
|
|
|
if edu.Content, err = json.Marshal(content); err != nil {
|
2021-11-03 15:28:40 +01:00
|
|
|
log.WithError(err).Error("failed to marshal EDU JSON")
|
|
|
|
return
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:28:40 +01:00
|
|
|
if err := t.queues.SendEDU(edu, t.ServerName, names); err != nil {
|
|
|
|
log.WithError(err).Error("failed to send EDU")
|
|
|
|
}
|
2020-11-09 19:46:11 +01:00
|
|
|
}
|