fix: preserve p2p topic for IM system notices

This commit is contained in:
richjohnrambo
2026-09-13 16:00:58 +08:00
parent ccbd385d2f
commit f1279ac607
4 changed files with 116 additions and 20 deletions
+64 -1
View File
@@ -7894,7 +7894,7 @@ func TestPublishIMConversationSystemNoticeDeliversToOnlineP2PSessions(t *testing
if !ok || msg.Data == nil {
t.Fatalf("unexpected outbound for %s: %#v", uid.UserId(), raw)
}
if msg.Data.Topic != mustP2PNameForUser(t, uid, topic) || msg.Data.SeqId != 1 {
if msg.Data.Topic != topic || msg.Data.SeqId != 1 {
t.Fatalf("unexpected p2p data for %s: %#v", uid.UserId(), msg.Data)
}
if msg.Data.Head["x-im-template"] != "friend_added" || msg.Data.Head["x-im-type"] != "system_notice" {
@@ -7906,6 +7906,69 @@ func TestPublishIMConversationSystemNoticeDeliversToOnlineP2PSessions(t *testing
}
}
func TestPublishIMConversationSystemNoticePresenceUsesP2PTopic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
oldStore := store.Store
oldTopics := store.Topics
oldMessages := store.Messages
oldHub := globals.hub
mockStore := mock_store.NewMockPersistentStorageInterface(ctrl)
topics := mock_store.NewMockTopicsPersistenceInterface(ctrl)
messages := mock_store.NewMockMessagesPersistenceInterface(ctrl)
store.Store = mockStore
store.Topics = topics
store.Messages = messages
globals.hub = &Hub{routeSrv: make(chan *ServerComMessage, 4)}
defer func() {
store.Store = oldStore
store.Topics = oldTopics
store.Messages = oldMessages
globals.hub = oldHub
}()
mockStore.EXPECT().IsOpen().Return(true)
applicant := types.Uid(1)
target := types.Uid(2)
topic := applicant.P2PName(target)
topics.EXPECT().Get(types.TenantID(7), topic).Return(&types.Topic{TenantID: 7, SeqId: 16}, nil)
messages.EXPECT().Save(types.TenantID(7), gomock.Any(), nil, false).DoAndReturn(
func(_ types.TenantID, msg *types.Message, _ []string, _ bool) (*types.MessageSaveResult, error) {
if msg.Topic != topic || msg.SeqId != 17 {
t.Fatalf("unexpected saved message: %#v", msg)
}
return &types.MessageSaveResult{SeqId: msg.SeqId}, nil
})
publishIMConversationSystemNotice(types.TenantID(7), topic,
"system_notice", "friend_added", "你们已成为好友,现在可以开始聊天了。",
[]imSystemMessageActor{
{UserID: applicant.UserId(), Click: map[string]any{"type": "user_profile", "target_id": applicant.UserId()}},
{UserID: target.UserId(), Click: map[string]any{"type": "user_profile", "target_id": target.UserId()}},
}, nil, map[string]any{"request_id": "frq_1"})
select {
case routed := <-globals.hub.routeSrv:
if routed.Data == nil || routed.Data.Topic != topic || !routed.PreserveTopicName {
t.Fatalf("unexpected system data broadcast: %#v", routed)
}
default:
t.Fatal("expected system data broadcast")
}
for _, uid := range []types.Uid{applicant, target} {
select {
case routed := <-globals.hub.routeSrv:
if routed.RcptTo != uid.UserId() || routed.Pres == nil || routed.Pres.Topic != "me" ||
routed.Pres.Src != topic || routed.Pres.SeqId != 17 || routed.Pres.SkipTopic != topic ||
!routed.PreserveTopicName {
t.Fatalf("unexpected system presence for %s: %#v", uid.UserId(), routed)
}
default:
t.Fatalf("expected system presence for %s", uid.UserId())
}
}
}
func mustP2PNameForUser(t *testing.T, uid types.Uid, topic string) string {
t.Helper()
name, err := types.P2PNameForUser(uid, topic)