fix: keep friend request success when event publish fails

This commit is contained in:
richjohnrambo
2026-09-14 23:29:33 +08:00
parent 40510a616e
commit fca0a5baf9
2 changed files with 43 additions and 2 deletions
+41
View File
@@ -8,6 +8,7 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
@@ -299,6 +300,7 @@ type testMailboxStore struct {
type testUserSecurityStore struct {
unreadByPrefix map[string]int
notifications []types.SystemNotification
err error
}
type testProfilesStore struct {
@@ -1017,6 +1019,9 @@ func (s *testUserSecurityStore) SetYouthMode(_ types.TenantID, uid types.Uid, en
}
func (s *testUserSecurityStore) CreateSystemNotification(_ types.TenantID, notification *types.SystemNotification) error {
if s.err != nil {
return s.err
}
if notification != nil {
s.notifications = append(s.notifications, *notification)
}
@@ -8015,6 +8020,42 @@ func TestServeIMFriendRequestCreate(t *testing.T) {
}
}
func TestServeIMFriendRequestCreateIgnoresRelationEventFailure(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockStore := expectIMBearer(t, ctrl, "token-1", 7, 1)
mockStore.EXPECT().GetUidString().Return("request-1")
oldRelations := store.Relations
oldProfiles := store.Profiles
oldSecurity := store.Security
relations := &testRelationsStore{context: &types.RelationshipContext{Relationship: types.RelationshipNone}}
store.Relations = relations
store.Profiles = &testProfilesStore{}
store.Security = &testUserSecurityStore{err: errors.New("notification unavailable")}
defer func() {
store.Relations = oldRelations
store.Profiles = oldProfiles
store.Security = oldSecurity
}()
req := httptest.NewRequest(http.MethodPost, "/v1/im/friend-requests",
bytes.NewBufferString(`{"target_user_id":"`+types.Uid(2).UserId()+`","message":"你好","source":"search"}`))
req = req.WithContext(context.WithValue(req.Context(), imHTTPProtocolValidated, true))
req.Header.Set("Authorization", "Bearer token-1")
req.Header.Set("Idempotency-Key", "fr-1")
req.Header.Set("X-Request-ID", "req-event-fail")
rec := httptest.NewRecorder()
serveIMFriendRequests(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status: got %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
if relations.created == nil || relations.created.RequestID == "" {
t.Fatalf("friend request was not created: %#v", relations.created)
}
}
func TestPublishIMConversationSystemNoticeDeliversToOnlineP2PSessions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()