- Updated the WebSocket hub to replace the insecure `user_id` query parameter with an authentication token for secure connections. - Introduced typed events for mail operations (created, updated, deleted) to streamline event handling. - Implemented heartbeat functionality (ping/pong) to maintain connection health. - Enhanced client reconnection logic and delta replay for improved user experience. - Added limits on connections per user/session to prevent abuse and ensure stability.
120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package imap
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/ultisuite/ulti-backend/internal/mail/rules"
|
|
"github.com/ultisuite/ulti-backend/internal/realtime"
|
|
)
|
|
|
|
type postSyncEvent struct {
|
|
userID string
|
|
accountID string
|
|
messageID string
|
|
kind string // created | updated | deleted
|
|
}
|
|
|
|
// syncPipeline runs rules and realtime notifications after message sync.
|
|
type syncPipeline struct {
|
|
db *pgxpool.Pool
|
|
logger *slog.Logger
|
|
rules *rules.Engine
|
|
hub *realtime.Hub
|
|
}
|
|
|
|
func newSyncPipeline(db *pgxpool.Pool, rulesEngine *rules.Engine, hub *realtime.Hub) *syncPipeline {
|
|
return &syncPipeline{
|
|
db: db,
|
|
logger: slog.Default().With("component", "imap-pipeline"),
|
|
rules: rulesEngine,
|
|
hub: hub,
|
|
}
|
|
}
|
|
|
|
func (p *syncPipeline) handle(ctx context.Context, ev postSyncEvent) {
|
|
if ev.kind == "deleted" {
|
|
p.broadcast(ev, realtime.NewMailDeletedEvent(ev.messageID, ev.accountID))
|
|
return
|
|
}
|
|
|
|
if p.rules != nil && ev.kind == "created" {
|
|
msg, err := p.loadRuleMessage(ctx, ev.messageID)
|
|
if err != nil {
|
|
p.logger.Error("load message for rules", "message_id", ev.messageID, "error", err)
|
|
} else if err := p.rules.EvaluateMessage(ctx, ev.userID, msg); err != nil {
|
|
p.logger.Error("rules evaluation failed", "message_id", ev.messageID, "error", err)
|
|
}
|
|
}
|
|
|
|
event := realtime.NewMailUpdatedEvent(ev.messageID, ev.accountID)
|
|
if ev.kind == "created" {
|
|
event = realtime.NewMailCreatedEvent(ev.messageID, ev.accountID)
|
|
}
|
|
p.broadcast(ev, event)
|
|
}
|
|
|
|
func (p *syncPipeline) broadcast(ev postSyncEvent, event realtime.Event) {
|
|
if p.hub == nil || ev.userID == "" {
|
|
return
|
|
}
|
|
p.hub.Broadcast(ev.userID, event)
|
|
}
|
|
|
|
func (p *syncPipeline) loadRuleMessage(ctx context.Context, messageID string) (*rules.Message, error) {
|
|
var (
|
|
fromJSON []byte
|
|
toJSON []byte
|
|
subject string
|
|
bodyText string
|
|
hasAtt bool
|
|
)
|
|
err := p.db.QueryRow(ctx, `
|
|
SELECT from_addr, to_addrs, subject, body_text, has_attachments
|
|
FROM messages WHERE id = $1
|
|
`, messageID).Scan(&fromJSON, &toJSON, &subject, &bodyText, &hasAtt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
from := firstAddressString(fromJSON)
|
|
to := addressListStrings(toJSON)
|
|
|
|
return &rules.Message{
|
|
ID: messageID,
|
|
From: from,
|
|
To: to,
|
|
Subject: subject,
|
|
BodyText: bodyText,
|
|
HasAttachments: hasAtt,
|
|
}, nil
|
|
}
|
|
|
|
func firstAddressString(fromJSON []byte) string {
|
|
var addrs []EmailAddress
|
|
if err := json.Unmarshal(fromJSON, &addrs); err != nil || len(addrs) == 0 {
|
|
return string(fromJSON)
|
|
}
|
|
a := addrs[0]
|
|
if a.Name != "" {
|
|
return a.Name + " <" + a.Address + ">"
|
|
}
|
|
return a.Address
|
|
}
|
|
|
|
func addressListStrings(toJSON []byte) []string {
|
|
var addrs []EmailAddress
|
|
if err := json.Unmarshal(toJSON, &addrs); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]string, 0, len(addrs))
|
|
for _, a := range addrs {
|
|
if a.Address != "" {
|
|
out = append(out, a.Address)
|
|
}
|
|
}
|
|
return out
|
|
}
|