Fix conversation context topic fallback
This commit is contained in:
+36
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user