Fix HTTP message forward delivery
This commit is contained in:
@@ -3604,11 +3604,17 @@ func serveIMMessageForward(wrt http.ResponseWriter, req *http.Request) {
|
||||
Head: forwardBundleMessageHead(clientMid),
|
||||
Content: bundle,
|
||||
}
|
||||
if err := prepareHTTPForwardMessage(principal.TenantID, forwarded); err != nil {
|
||||
failed = append(failed, forwardFailurePayload("", 0, target, relationErrorReason(err)))
|
||||
continue
|
||||
}
|
||||
saved, err := saveMessageMaybeKafka(req.Context(), principal.TenantID, forwarded, nil, true)
|
||||
if err != nil {
|
||||
failed = append(failed, forwardFailurePayload("", 0, target, relationErrorReason(err)))
|
||||
continue
|
||||
}
|
||||
forwarded.SeqId = saved.SeqId
|
||||
broadcastHTTPForwardMessage(principal.TenantID, forwarded)
|
||||
items = append(items, map[string]any{
|
||||
"sources": bundle["source_refs"],
|
||||
"target_topic": target,
|
||||
@@ -3656,11 +3662,17 @@ func serveIMMessageForward(wrt http.ResponseWriter, req *http.Request) {
|
||||
Head: forwardedMessageHead(msg, clientMid),
|
||||
Content: forwardedMessageContent(msg, body.Comment),
|
||||
}
|
||||
if err := prepareHTTPForwardMessage(principal.TenantID, forwarded); err != nil {
|
||||
failed = append(failed, forwardFailurePayload(sourceTopic, int64(source.Seq), target, relationErrorReason(err)))
|
||||
continue
|
||||
}
|
||||
saved, err := saveMessageMaybeKafka(req.Context(), principal.TenantID, forwarded, nil, true)
|
||||
if err != nil {
|
||||
failed = append(failed, forwardFailurePayload(sourceTopic, int64(source.Seq), target, relationErrorReason(err)))
|
||||
continue
|
||||
}
|
||||
forwarded.SeqId = saved.SeqId
|
||||
broadcastHTTPForwardMessage(principal.TenantID, forwarded)
|
||||
items = append(items, map[string]any{
|
||||
"source": map[string]any{"topic": sourceTopic, "seq": int64(source.Seq)},
|
||||
"target_topic": target,
|
||||
@@ -8947,6 +8959,64 @@ func buildForwardBundle(principal *imPrincipal, sources []struct {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func prepareHTTPForwardMessage(tenantID types.TenantID, msg *types.Message) error {
|
||||
if msg == nil || msg.TenantID != tenantID || strings.TrimSpace(msg.Topic) == "" {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
if msg.SeqId > 0 {
|
||||
return nil
|
||||
}
|
||||
if !kafkaEnabled() && globals.hub == nil {
|
||||
return nil
|
||||
}
|
||||
if store.Topics == nil {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
topicRecord, err := store.Topics.Get(tenantID, msg.Topic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if topicRecord == nil {
|
||||
return types.ErrNotFound
|
||||
}
|
||||
msg.SeqId = topicRecord.SeqId + 1
|
||||
if msg.CreatedAt.IsZero() {
|
||||
msg.ObjHeader.CreatedAt = time.Now().UTC().Round(time.Millisecond)
|
||||
}
|
||||
if msg.UpdatedAt.IsZero() {
|
||||
msg.ObjHeader.UpdatedAt = msg.CreatedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func broadcastHTTPForwardMessage(tenantID types.TenantID, msg *types.Message) {
|
||||
if globals.hub == nil || msg == nil || msg.SeqId <= 0 || msg.Topic == "" {
|
||||
return
|
||||
}
|
||||
timestamp := msg.CreatedAt
|
||||
if timestamp.IsZero() {
|
||||
timestamp = time.Now().UTC().Round(time.Millisecond)
|
||||
}
|
||||
select {
|
||||
case globals.hub.routeSrv <- &ServerComMessage{
|
||||
TenantID: tenantID,
|
||||
RcptTo: msg.Topic,
|
||||
AsUser: types.ParseUid(msg.From).UserId(),
|
||||
Timestamp: timestamp,
|
||||
Data: &MsgServerData{
|
||||
Topic: msg.Topic,
|
||||
From: types.ParseUid(msg.From).UserId(),
|
||||
Timestamp: timestamp,
|
||||
SeqId: msg.SeqId,
|
||||
Head: msg.Head,
|
||||
Content: msg.Content,
|
||||
},
|
||||
}:
|
||||
default:
|
||||
logs.Warn.Printf("im message forward: hub route queue full topic=%s seq=%d", msg.Topic, msg.SeqId)
|
||||
}
|
||||
}
|
||||
|
||||
func forwardedMessageHead(msg *types.Message, clientMid string) types.KVMap {
|
||||
head := types.KVMap{}
|
||||
if msg != nil {
|
||||
|
||||
Reference in New Issue
Block a user