fix im heartbeat and friend display name

This commit is contained in:
richjohnrambo
2026-09-10 15:23:27 +08:00
parent 14ee3d937f
commit 1f3a98605e
8 changed files with 248 additions and 29 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ const (
pongWait = idleSessionTimeout
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
pingPeriod = 30 * time.Second
)
func (sess *Session) closeWS() {
+18 -7
View File
@@ -9206,12 +9206,23 @@ func relationshipContextPayload(ctx *types.RelationshipContext) map[string]any {
if ctx.RequestID != "" {
data["request_id"] = ctx.RequestID
}
if ctx.Remark != "" {
data["remark"] = ctx.Remark
}
if ctx.Peer != nil {
data["peer"] = map[string]any{
"user_id": ctx.PeerID.UserId(),
"public": ctx.Peer.Public,
"trusted": ctx.Peer.Trusted,
peer := userSummaryPayload(ctx.Peer, ctx.Remark)
peer["public"] = ctx.Peer.Public
peer["trusted"] = ctx.Peer.Trusted
data["peer"] = peer
if displayName, ok := peer["display_name"]; ok {
data["display_name"] = displayName
}
if avatarFileID, ok := peer["avatar_file_id"]; ok {
data["avatar_file_id"] = avatarFileID
}
}
if _, ok := data["display_name"]; !ok {
data["display_name"] = ctx.PeerID.UserId()
}
return data
}
@@ -9366,14 +9377,14 @@ func userSummaryPayload(user *types.User, remark string) map[string]any {
userID = user.Id
}
data := map[string]any{"user_id": userID}
if remark != "" {
data["display_name"] = remark
}
if public, ok := user.Public.(map[string]any); ok {
copyUserPublicSummary(data, public)
} else if public, ok := user.Public.(types.KVMap); ok {
copyUserPublicSummary(data, map[string]any(public))
}
if remark != "" {
data["display_name"] = remark
}
if _, ok := data["display_name"]; !ok {
data["display_name"] = userID
}
+19 -1
View File
@@ -1250,7 +1250,18 @@ func (s *testRelationsStore) DeleteFriend(tenantID types.TenantID, userID, peerI
}
func (s *testRelationsStore) UpdateFriendRemark(tenantID types.TenantID, userID, peerID types.Uid, remark string, now time.Time) (*types.Friendship, error) {
return &types.Friendship{TenantID: tenantID, UserID: userID, PeerID: peerID, Status: types.RelationshipFriend, Remark: remark, Topic: userID.P2PName(peerID), CreatedAt: now, UpdatedAt: now}, nil
friendship := &types.Friendship{TenantID: tenantID, UserID: userID, PeerID: peerID, Status: types.RelationshipFriend, Remark: remark, Topic: userID.P2PName(peerID), CreatedAt: now, UpdatedAt: now}
s.context = &types.RelationshipContext{
TenantID: tenantID,
UserID: userID,
PeerID: peerID,
Topic: friendship.Topic,
Relationship: types.RelationshipFriend,
CanSend: true,
Remark: remark,
Peer: &types.User{ObjHeader: types.ObjHeader{Id: peerID.String()}, TenantID: tenantID, Public: map[string]any{"display_name": "Ming"}},
}
return friendship, nil
}
func (s *testRelationsStore) SearchUsers(_ types.TenantID, _ types.Uid, _, _, _ string, _ int) ([]types.UserSearchItem, string, error) {
@@ -7203,6 +7214,13 @@ func TestServeIMFriendRemark(t *testing.T) {
if data["relationship"] != types.RelationshipFriend || data["topic"] == "" {
t.Fatalf("unexpected friend remark response: %#v", data)
}
if data["display_name"] != "小明" || data["remark"] != "小明" {
t.Fatalf("friend remark should drive display name: %#v", data)
}
peerData := data["peer"].(map[string]any)
if peerData["display_name"] != "小明" {
t.Fatalf("peer display name should use remark: %#v", data)
}
}
func TestServeIMFriendDeleteReturnsCannotSendContext(t *testing.T) {
+1 -1
View File
@@ -71,7 +71,7 @@ const (
minSupportedVersion = "0.20"
// idleSessionTimeout defines duration of being idle before terminating a session.
idleSessionTimeout = time.Second * 55
idleSessionTimeout = time.Second * 45
// idleMasterTopicTimeout defines now long to keep master topic alive after the last session detached.
idleMasterTopicTimeout = time.Second * 4
// Same as above but shut down the proxy topic sooner. Otherwise master topic would be kept alive for too long.
+9
View File
@@ -3411,6 +3411,14 @@ func relationshipContext(tenantAdp adapter.TenantBusinessAdapter, tenantID types
if cachedFriend, err := cachedFriendContains(tenantID, userID, peerID); err != nil {
logs.Warn.Printf("cache friend[%s:%s]: read failed: %v", userID.UserId(), peerID.UserId(), err)
} else if cachedFriend {
if friendship, err := tenantAdp.TenantFriendshipGet(tenantID, userID, peerID); err != nil {
logs.Warn.Printf("friendship[%s:%s]: read failed after cache hit: %v", userID.UserId(), peerID.UserId(), err)
} else if friendship != nil && friendship.Status == types.RelationshipFriend {
ctx.Remark = friendship.Remark
if friendship.Topic != "" {
ctx.Topic = friendship.Topic
}
}
ctx.Relationship = types.RelationshipFriend
ctx.CanSend = true
if ctx.Topic == "" {
@@ -3424,6 +3432,7 @@ func relationshipContext(tenantAdp adapter.TenantBusinessAdapter, tenantID types
cacheFriendOneWay(tenantID, userID, peerID)
ctx.Relationship = types.RelationshipFriend
ctx.CanSend = true
ctx.Remark = friendship.Remark
if ctx.Topic == "" {
ctx.Topic = friendship.Topic
}
+1
View File
@@ -59,6 +59,7 @@ type RelationshipContext struct {
Topic string `json:"topic"`
Relationship string `json:"relationship"`
CanSend bool `json:"can_send"`
Remark string `json:"remark,omitempty"`
RequestID string `json:"request_id,omitempty"`
PendingStatus string `json:"pending_status,omitempty"`
Peer *User `json:"peer,omitempty"`