fix: 兼容历史收藏源避免列表崩溃

This commit is contained in:
richjohnrambo
2026-09-09 17:40:11 +08:00
parent e0a6ee27bc
commit 98234db263
2 changed files with 49 additions and 3 deletions
+14 -3
View File
@@ -7912,6 +7912,12 @@ func favoritePayloadForPrincipal(principal *imPrincipal, item types.FavoriteItem
}
sourceTopic, ok := normalizeIMFavoriteSourceTopic(principal.Uid, item.SourceTopic)
if !ok {
if legacyFavoriteSnapshotOnly(item) {
if item.Availability == "" {
item.Availability = "available"
}
return favoritePayload(item), true
}
return nil, false
}
if err := requireTopicReader(principal, sourceTopic); err != nil {
@@ -7934,6 +7940,11 @@ func favoritePayloadForPrincipal(principal *imPrincipal, item types.FavoriteItem
return favoritePayload(item), true
}
func legacyFavoriteSnapshotOnly(item types.FavoriteItem) bool {
source := strings.TrimSpace(item.SourceTopic)
return strings.HasPrefix(source, "favSeed") && item.Snapshot != nil
}
func messageSearchPayload(msg types.Message, query string) map[string]any {
text := messageText(msg)
snippet := textSnippet(text, query)
@@ -9586,15 +9597,15 @@ func normalizeIMUserVisibleTopic(viewer types.Uid, topic string) (string, bool)
if len(topic) < 3 {
return "", false
}
switch types.GetTopicCat(topic) {
case types.TopicCatMe:
switch topic[:3] {
case "usr":
peer := types.ParseUserId(topic)
if peer.IsZero() {
return "", false
}
p2p := viewer.P2PName(peer)
return p2p, p2p != ""
case types.TopicCatP2P, types.TopicCatGrp, types.TopicCatSlf:
case "p2p", "grp", "slf":
return topic, true
default:
return "", false
+35
View File
@@ -4964,6 +4964,41 @@ func TestServeIMFavoritesListSupportsRefOnlyAttachment(t *testing.T) {
}
}
func TestServeIMFavoritesListKeepsLegacySeedSnapshots(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
expectIMBearer(t, ctrl, "token-1", 7, 42)
oldFavorites := store.Favorites
oldSubs := store.Subs
oldMessages := store.Messages
now := time.Date(2026, 9, 9, 9, 0, 0, 0, time.UTC)
store.Favorites = &testFavoritesStore{items: []types.FavoriteItem{{
TenantID: 7, FavoriteID: "fav_seed_19179719768_text", UserID: 42, ItemType: "text",
SourceTopic: "favSeed19179719768", SourceSeq: 7001, Availability: "available", CollectedAt: now,
Snapshot: map[string]any{"text": "seed favorite"},
}}}
store.Subs = mock_store.NewMockSubsPersistenceInterface(ctrl)
store.Messages = nil
defer func() {
store.Favorites = oldFavorites
store.Subs = oldSubs
store.Messages = oldMessages
}()
req := httptest.NewRequest(http.MethodGet, "/v1/im/favorites?type=all", nil)
req.Header.Set("Authorization", "Bearer token-1")
rec := httptest.NewRecorder()
serveIMFavorites(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status: got %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
items := data["items"].([]any)
if len(items) != 1 || items[0].(map[string]any)["favorite_id"] != "fav_seed_19179719768_text" {
t.Fatalf("unexpected legacy seed favorite response: %#v", data)
}
}
func TestNormalizeIMUserVisibleTopicSelf(t *testing.T) {
uid := types.Uid(42)
got, ok := normalizeIMUserVisibleTopic(uid, "slf")