feat: filter receive messages by since
This commit is contained in:
@@ -3,7 +3,9 @@ package isphere
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"isphere-ai-bridge/internal/isphere/logcodec"
|
||||
"isphere-ai-bridge/internal/isphere/packetlog"
|
||||
@@ -25,6 +27,7 @@ type Message struct {
|
||||
type ReceiveMessagesQuery struct {
|
||||
ConversationID string
|
||||
Query string
|
||||
Since string
|
||||
Limit int
|
||||
}
|
||||
|
||||
@@ -37,6 +40,10 @@ type EncryptedPacketLogSource struct {
|
||||
}
|
||||
|
||||
func (s EncryptedPacketLogSource) ReceiveMessages(ctx context.Context, query ReceiveMessagesQuery) (ReceiveMessagesResult, error) {
|
||||
since, hasSince, err := parseReceiveMessagesSince(query.Since)
|
||||
if err != nil {
|
||||
return ReceiveMessagesResult{}, err
|
||||
}
|
||||
limit := query.Limit
|
||||
if limit <= 0 || limit > len(s.Lines) {
|
||||
limit = len(s.Lines)
|
||||
@@ -57,7 +64,7 @@ func (s EncryptedPacketLogSource) ReceiveMessages(ctx context.Context, query Rec
|
||||
return ReceiveMessagesResult{}, fmt.Errorf("parse packet log line %d: %w", index, err)
|
||||
}
|
||||
message := normalizePacketMessage(parsed)
|
||||
if !receiveMessageMatchesQuery(message, query) {
|
||||
if !receiveMessageMatchesQuery(message, query) || !receiveMessageMatchesSince(message, since, hasSince) {
|
||||
continue
|
||||
}
|
||||
messages = append(messages, message)
|
||||
@@ -68,6 +75,18 @@ func (s EncryptedPacketLogSource) ReceiveMessages(ctx context.Context, query Rec
|
||||
return ReceiveMessagesResult{Messages: messages}, nil
|
||||
}
|
||||
|
||||
func parseReceiveMessagesSince(value string) (time.Time, bool, error) {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return time.Time{}, false, nil
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339Nano, trimmed)
|
||||
if err != nil {
|
||||
return time.Time{}, false, fmt.Errorf("parse receive messages since %q: %w", value, err)
|
||||
}
|
||||
return parsed.UTC(), true, nil
|
||||
}
|
||||
|
||||
func receiveMessageMatchesQuery(message Message, query ReceiveMessagesQuery) bool {
|
||||
if strings.TrimSpace(query.ConversationID) != "" && message.ConversationID != strings.TrimSpace(query.ConversationID) {
|
||||
return false
|
||||
@@ -93,6 +112,32 @@ func receiveMessageMatchesQuery(message Message, query ReceiveMessagesQuery) boo
|
||||
return false
|
||||
}
|
||||
|
||||
func receiveMessageMatchesSince(message Message, since time.Time, hasSince bool) bool {
|
||||
if !hasSince {
|
||||
return true
|
||||
}
|
||||
messageTime, ok := packetMessageTimestamp(message.Timestamp)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return !messageTime.Before(since)
|
||||
}
|
||||
|
||||
func packetMessageTimestamp(value string) (time.Time, bool) {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
epoch, err := strconv.ParseInt(trimmed, 10, 64)
|
||||
if err != nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
if epoch > 1_000_000_000_000 {
|
||||
return time.UnixMilli(epoch).UTC(), true
|
||||
}
|
||||
return time.Unix(epoch, 0).UTC(), true
|
||||
}
|
||||
|
||||
func normalizePacketMessage(msg packetlog.Message) Message {
|
||||
sender := bareJID(msg.From)
|
||||
receiver := bareJID(msg.To)
|
||||
|
||||
Reference in New Issue
Block a user