fix: 兼容历史收藏源避免列表崩溃
This commit is contained in:
+14
-3
@@ -7912,6 +7912,12 @@ func favoritePayloadForPrincipal(principal *imPrincipal, item types.FavoriteItem
|
|||||||
}
|
}
|
||||||
sourceTopic, ok := normalizeIMFavoriteSourceTopic(principal.Uid, item.SourceTopic)
|
sourceTopic, ok := normalizeIMFavoriteSourceTopic(principal.Uid, item.SourceTopic)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
if legacyFavoriteSnapshotOnly(item) {
|
||||||
|
if item.Availability == "" {
|
||||||
|
item.Availability = "available"
|
||||||
|
}
|
||||||
|
return favoritePayload(item), true
|
||||||
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if err := requireTopicReader(principal, sourceTopic); err != nil {
|
if err := requireTopicReader(principal, sourceTopic); err != nil {
|
||||||
@@ -7934,6 +7940,11 @@ func favoritePayloadForPrincipal(principal *imPrincipal, item types.FavoriteItem
|
|||||||
return favoritePayload(item), true
|
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 {
|
func messageSearchPayload(msg types.Message, query string) map[string]any {
|
||||||
text := messageText(msg)
|
text := messageText(msg)
|
||||||
snippet := textSnippet(text, query)
|
snippet := textSnippet(text, query)
|
||||||
@@ -9586,15 +9597,15 @@ func normalizeIMUserVisibleTopic(viewer types.Uid, topic string) (string, bool)
|
|||||||
if len(topic) < 3 {
|
if len(topic) < 3 {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
switch types.GetTopicCat(topic) {
|
switch topic[:3] {
|
||||||
case types.TopicCatMe:
|
case "usr":
|
||||||
peer := types.ParseUserId(topic)
|
peer := types.ParseUserId(topic)
|
||||||
if peer.IsZero() {
|
if peer.IsZero() {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
p2p := viewer.P2PName(peer)
|
p2p := viewer.P2PName(peer)
|
||||||
return p2p, p2p != ""
|
return p2p, p2p != ""
|
||||||
case types.TopicCatP2P, types.TopicCatGrp, types.TopicCatSlf:
|
case "p2p", "grp", "slf":
|
||||||
return topic, true
|
return topic, true
|
||||||
default:
|
default:
|
||||||
return "", false
|
return "", false
|
||||||
|
|||||||
@@ -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) {
|
func TestNormalizeIMUserVisibleTopicSelf(t *testing.T) {
|
||||||
uid := types.Uid(42)
|
uid := types.Uid(42)
|
||||||
got, ok := normalizeIMUserVisibleTopic(uid, "slf")
|
got, ok := normalizeIMUserVisibleTopic(uid, "slf")
|
||||||
|
|||||||
Reference in New Issue
Block a user