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 (
|
2022-01-05 18:44:49 +01:00
|
|
|
"context"
|
2022-03-29 14:14:35 +02:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-03-29 14:14:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-01-05 18:44:49 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-01-26 13:56:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2022-09-30 13:48:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/streams"
|
2019-07-12 16:59:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2022-01-05 18:44:49 +01:00
|
|
|
"github.com/nats-io/nats.go"
|
2019-07-12 16:59:53 +02:00
|
|
|
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 {
|
2022-01-05 18:44:49 +01:00
|
|
|
ctx context.Context
|
|
|
|
jetstream nats.JetStreamContext
|
2022-02-02 14:32:48 +01:00
|
|
|
durable string
|
2022-01-05 18:44:49 +01:00
|
|
|
topic string
|
2022-03-29 14:14:35 +02:00
|
|
|
eduCache *caching.EDUCache
|
2022-09-30 13:48:10 +02:00
|
|
|
stream streams.StreamProvider
|
2022-01-05 18:44:49 +01:00
|
|
|
notifier *notifier.Notifier
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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(
|
2021-01-26 13:56:20 +01:00
|
|
|
process *process.ProcessContext,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.SyncAPI,
|
2022-01-05 18:44:49 +01:00
|
|
|
js nats.JetStreamContext,
|
2022-03-29 14:14:35 +02:00
|
|
|
eduCache *caching.EDUCache,
|
2021-01-08 17:59:06 +01:00
|
|
|
notifier *notifier.Notifier,
|
2022-09-30 13:48:10 +02:00
|
|
|
stream streams.StreamProvider,
|
2019-07-12 16:59:53 +02:00
|
|
|
) *OutputTypingEventConsumer {
|
2022-01-05 18:44:49 +01:00
|
|
|
return &OutputTypingEventConsumer{
|
|
|
|
ctx: process.Context(),
|
|
|
|
jetstream: js,
|
2022-03-23 11:20:18 +01:00
|
|
|
topic: cfg.Matrix.JetStream.Prefixed(jetstream.OutputTypingEvent),
|
2022-03-29 14:14:35 +02:00
|
|
|
durable: cfg.Matrix.JetStream.Durable("SyncAPITypingConsumer"),
|
2022-01-05 18:44:49 +01:00
|
|
|
eduCache: eduCache,
|
|
|
|
notifier: notifier,
|
|
|
|
stream: stream,
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:14:35 +02:00
|
|
|
// Start consuming typing events.
|
2019-07-12 16:59:53 +02:00
|
|
|
func (s *OutputTypingEventConsumer) Start() error {
|
2022-02-02 14:32:48 +01:00
|
|
|
return jetstream.JetStreamConsumer(
|
2022-08-31 13:21:56 +02:00
|
|
|
s.ctx, s.jetstream, s.topic, s.durable, 1,
|
|
|
|
s.onMessage, nats.DeliverAll(), nats.ManualAck(),
|
2022-02-02 14:32:48 +01:00
|
|
|
)
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:21:56 +02:00
|
|
|
func (s *OutputTypingEventConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) bool {
|
|
|
|
msg := msgs[0] // Guaranteed to exist if onMessage is called
|
2022-03-29 14:14:35 +02:00
|
|
|
roomID := msg.Header.Get(jetstream.RoomID)
|
|
|
|
userID := msg.Header.Get(jetstream.UserID)
|
|
|
|
typing, err := strconv.ParseBool(msg.Header.Get("typing"))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("output log: typing parse failure")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
timeout, err := strconv.Atoi(msg.Header.Get("timeout_ms"))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("output log: timeout_ms parse failure")
|
2022-02-02 14:32:48 +01:00
|
|
|
return true
|
|
|
|
}
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-02-02 14:32:48 +01:00
|
|
|
log.WithFields(log.Fields{
|
2022-03-29 14:14:35 +02:00
|
|
|
"room_id": roomID,
|
|
|
|
"user_id": userID,
|
|
|
|
"typing": typing,
|
|
|
|
"timeout": timeout,
|
|
|
|
}).Debug("syncapi received EDU data from client api")
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-02-02 14:32:48 +01:00
|
|
|
var typingPos types.StreamPosition
|
2022-03-29 14:14:35 +02:00
|
|
|
if typing {
|
|
|
|
expiry := time.Now().Add(time.Duration(timeout) * time.Millisecond)
|
2022-02-02 14:32:48 +01:00
|
|
|
typingPos = types.StreamPosition(
|
2022-03-29 14:14:35 +02:00
|
|
|
s.eduCache.AddTypingUser(userID, roomID, &expiry),
|
2022-02-02 14:32:48 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
typingPos = types.StreamPosition(
|
2022-03-29 14:14:35 +02:00
|
|
|
s.eduCache.RemoveUser(userID, roomID),
|
2022-02-02 14:32:48 +01:00
|
|
|
)
|
|
|
|
}
|
2019-07-12 16:59:53 +02:00
|
|
|
|
2022-02-02 14:32:48 +01:00
|
|
|
s.stream.Advance(typingPos)
|
2022-03-29 14:14:35 +02:00
|
|
|
s.notifier.OnNewTyping(roomID, types.StreamingToken{TypingPosition: typingPos})
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2022-02-02 14:32:48 +01:00
|
|
|
return true
|
2019-07-12 16:59:53 +02:00
|
|
|
}
|