queue: include send time in messages

This commit is contained in:
Branden J Brown 2025-04-20 13:55:36 -04:00
parent 0928197781
commit 7cc4482965
3 changed files with 22 additions and 8 deletions

View File

@ -125,11 +125,21 @@ func webhook(q *kgo.Client, secret []byte, errs chan<- error) http.HandlerFunc {
slog.String("broadcaster", ev.Event.Broadcaster), slog.String("broadcaster", ev.Event.Broadcaster),
slog.String("id", ev.Event.ID), slog.String("id", ev.Event.ID),
) )
// Use the external clock for timestamps.
tm, err := time.Parse(time.RFC3339Nano, r.Header.Get("Twitch-Eventsub-Message-Timestamp"))
if err != nil {
log.LogAttrs(ctx, slog.LevelWarn, "bad timestamp",
slog.String("timestamp", r.Header.Get("Twitch-Eventsub-Message-Timestamp")),
slog.Any("err", err),
)
tm = time.Now()
}
msg := queue.Message{ msg := queue.Message{
ID: ev.Event.ID, ID: ev.Event.ID,
Channel: ev.Event.Broadcaster, Channel: ev.Event.Broadcaster,
Sender: queue.Sender(secret, ev.Event.Broadcaster, ev.Event.Chatter), Sender: queue.Sender(secret, ev.Event.Broadcaster, ev.Event.Chatter),
Text: ev.Event.Message.Text, Timestamp: tm.UnixNano(),
Text: ev.Event.Message.Text,
} }
// TODO(branden): the context in the http request cancels once this // TODO(branden): the context in the http request cancels once this
// handler returns, which means the producer doesn't relay it. // handler returns, which means the producer doesn't relay it.

View File

@ -18,6 +18,9 @@ type Message struct {
Channel string `json:"ch"` Channel string `json:"ch"`
// Sender is an obfuscated identifier for the sending user. // Sender is an obfuscated identifier for the sending user.
Sender sender `json:"u"` Sender sender `json:"u"`
// Timestamp is the time at which the message was sent in nanoseconds since
// the Unix epoch.
Timestamp int64 `json:"ts"`
// Text is the message content. // Text is the message content.
Text string `json:"t"` Text string `json:"t"`
} }

View File

@ -21,10 +21,11 @@ func (p *spyProducer) Produce(ctx context.Context, rec *kgo.Record, promise func
func TestSend(t *testing.T) { func TestSend(t *testing.T) {
msg := queue.Message{ msg := queue.Message{
ID: "bocchi", ID: "bocchi",
Channel: "kessoku", Channel: "kessoku",
Sender: queue.Sender(make([]byte, 16), "kessoku", "ryō"), Sender: queue.Sender(make([]byte, 16), "kessoku", "ryō"),
Text: "bocchi the rock!", Timestamp: 1,
Text: "bocchi the rock!",
} }
var q spyProducer var q spyProducer
errs := make(chan error, 1) errs := make(chan error, 1)