Fix conversation context topic fallback

This commit is contained in:
richjohnrambo
2026-09-03 11:47:45 +08:00
parent 2e4e1f0df3
commit 871e0ba735
2 changed files with 69 additions and 1 deletions
+36 -1
View File
@@ -4088,7 +4088,11 @@ func serveIMConversationContext(wrt http.ResponseWriter, req *http.Request) {
writeIMError(wrt, req, http.StatusMethodNotAllowed, 30002, imReasonMethodNotAllowed, "method not allowed", nil)
return
}
topic, peer := normalizeP2PConversationTopic(topic, principal.Uid)
topic, peer, err := resolveConversationContextTopic(principal, topic)
if err != nil {
writeIMRelationError(wrt, req, err)
return
}
if topic == "" || peer.IsZero() {
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "topic"})
return
@@ -9996,6 +10000,37 @@ func normalizeP2PConversationTopic(topic string, uid types.Uid) (string, types.U
return topic, peer
}
func resolveConversationContextTopic(principal *imPrincipal, topic string) (string, types.Uid, error) {
normalized, peer := normalizeP2PConversationTopic(topic, principal.Uid)
if normalized != "" && !peer.IsZero() {
return normalized, peer, nil
}
topic = strings.TrimSpace(topic)
if types.GetTopicCat(topic) != types.TopicCatP2P {
return "", types.ZeroUid, nil
}
if _, _, err := types.ParseP2P(topic); err != nil {
return "", types.ZeroUid, nil
}
cursor := ""
for {
items, nextCursor, err := store.Relations.ListFriends(principal.TenantID, principal.Uid, "", "", cursor, 100)
if err != nil {
return "", types.ZeroUid, err
}
for _, item := range items {
if strings.TrimSpace(item.Friendship.Topic) == topic && !item.Friendship.PeerID.IsZero() {
return topic, item.Friendship.PeerID, nil
}
}
if nextCursor == "" {
break
}
cursor = nextCursor
}
return "", types.ZeroUid, nil
}
func normalizeConversationSettingsTopic(topic string, uid types.Uid) string {
if normalized, peer := normalizeP2PConversationTopic(topic, uid); !peer.IsZero() {
return normalized