feat: add im relationship flows

This commit is contained in:
richjohnrambo
2026-08-12 11:31:16 +08:00
parent 5bd35f97a6
commit d02041659e
11 changed files with 1100 additions and 12 deletions
+123
View File
@@ -33,6 +33,70 @@ type testVerificationStore struct {
tokenHash string
}
type testRelationsStore struct {
context *types.RelationshipContext
created *types.FriendRequest
blocked *types.UserBlock
deleted bool
canSend *bool
reason string
}
func (s *testRelationsStore) CreateFriendRequest(_ types.TenantID, req *types.FriendRequest) (*types.FriendRequest, error) {
s.created = req
return req, nil
}
func (s *testRelationsStore) ResolveFriendRequest(_ types.TenantID, requestID string, actor types.Uid, status string, handledAt time.Time) (*types.FriendRequest, error) {
return &types.FriendRequest{
TenantID: 7,
RequestID: requestID,
ApplicantID: 1,
TargetID: actor,
Status: status,
HandledAt: &handledAt,
CreatedAt: handledAt,
UpdatedAt: handledAt,
ExpiresAt: handledAt.Add(time.Hour),
}, nil
}
func (s *testRelationsStore) GetRelationshipContext(_ types.TenantID, userID, peerID types.Uid, topic string) (*types.RelationshipContext, error) {
if s.context != nil {
return s.context, nil
}
return &types.RelationshipContext{
TenantID: 7,
UserID: userID,
PeerID: peerID,
Topic: topic,
Relationship: types.RelationshipFriend,
CanSend: true,
}, nil
}
func (s *testRelationsStore) CanSendP2P(_ types.TenantID, _, _ types.Uid) (bool, string, error) {
if s.canSend != nil {
return *s.canSend, s.reason, nil
}
return true, types.RelationshipFriend, nil
}
func (s *testRelationsStore) BlockUser(tenantID types.TenantID, blockerID, blockedID types.Uid, now time.Time) (*types.UserBlock, error) {
s.blocked = &types.UserBlock{TenantID: tenantID, BlockerID: blockerID, BlockedID: blockedID, CreatedAt: now, UpdatedAt: now}
return s.blocked, nil
}
func (s *testRelationsStore) UnblockUser(_ types.TenantID, _, _ types.Uid) error {
s.blocked = nil
return nil
}
func (s *testRelationsStore) DeleteFriend(_ types.TenantID, _, _ types.Uid, _ time.Time) error {
s.deleted = true
return nil
}
func (s *testVerificationStore) Create(_ types.TenantID, vc *types.VerificationCode) error {
s.created = vc
return s.err
@@ -861,6 +925,65 @@ func TestServeIMMessageRevision(t *testing.T) {
}
}
func TestServeIMConversationContext(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
expectIMBearer(t, ctrl, "token-1", 7, 1)
oldRelations := store.Relations
peer := types.Uid(2)
topic := types.Uid(1).P2PName(peer)
store.Relations = &testRelationsStore{context: &types.RelationshipContext{
TenantID: 7,
UserID: 1,
PeerID: peer,
Topic: topic,
Relationship: types.RelationshipFriend,
CanSend: true,
}}
defer func() { store.Relations = oldRelations }()
req := httptest.NewRequest(http.MethodGet, "/v1/im/conversations/"+topic+"/context", nil)
req.Header.Set("Authorization", "Bearer token-1")
rec := httptest.NewRecorder()
serveIMConversationContext(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)
if data["relationship"] != types.RelationshipFriend || data["can_send"] != true {
t.Fatalf("unexpected context response: %#v", data)
}
}
func TestServeIMFriendRequestCreate(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
relations := &testRelationsStore{}
store.Relations = relations
defer func() { store.Relations = oldRelations }()
req := httptest.NewRequest(http.MethodPost, "/v1/im/friend-requests",
bytes.NewBufferString(`{"target_user_id":"`+types.Uid(2).UserId()+`","message":"你好","source":"search"}`))
req.Header.Set("Authorization", "Bearer token-1")
req.Header.Set("Idempotency-Key", "fr-1")
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())
}
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
if data["status"] != types.FriendRequestPending || relations.created == nil || relations.created.IdempotencyKey != "fr-1" {
t.Fatalf("unexpected friend request response: %#v created=%#v", data, relations.created)
}
}
func ptrTime(t time.Time) *time.Time {
return &t
}