From 98b6b249ee12be789596692079d9930f9625bec7 Mon Sep 17 00:00:00 2001 From: hawklin2017 <32898629+hawklin2017@users.noreply.github.com> Date: Wed, 1 Apr 2026 16:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=85=B1=E5=90=8C=E6=89=80?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E7=BE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/openim-rpc-group.yml | 1 + internal/rpc/group/group.go | 21 ++++++++++++++++++++- pkg/common/config/config.go | 1 + scripts/get_common_group.sh | 4 ++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/config/openim-rpc-group.yml b/config/openim-rpc-group.yml index a8c2d5ec1..0f42f7661 100644 --- a/config/openim-rpc-group.yml +++ b/config/openim-rpc-group.yml @@ -18,3 +18,4 @@ prometheus: enableHistoryForNewMembers: true +commonGroupsLimitWithFriend: 3 diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 3f97927f3..c7880d360 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -19,6 +19,7 @@ import ( "fmt" "math/big" "math/rand" + "sort" "strconv" "strings" "time" @@ -371,6 +372,7 @@ func (g *groupServer) GetCommonGroupsWithFriend(ctx context.Context, req *pbgrou if err != nil { return nil, err } + if len(selfGroupIDs) == 0 { return &pbgroup.GetCommonGroupsWithFriendResp{ Total: 0, @@ -382,6 +384,7 @@ func (g *groupServer) GetCommonGroupsWithFriend(ctx context.Context, req *pbgrou if err != nil { return nil, err } + if len(friendMembers) == 0 { return &pbgroup.GetCommonGroupsWithFriendResp{ Total: 0, @@ -392,12 +395,28 @@ func (g *groupServer) GetCommonGroupsWithFriend(ctx context.Context, req *pbgrou commonGroupIDs := datautil.Distinct(datautil.Slice(friendMembers, func(e *model.GroupMember) string { return e.GroupID })) + groups, err := g.getGroupsInfo(ctx, commonGroupIDs) if err != nil { return nil, err } + + // Keep response deterministic by sorting common groups with member count descending. + sort.SliceStable(groups, func(i, j int) bool { + return groups[i].MemberCount > groups[j].MemberCount + }) + total := len(groups) + + limit := g.config.RpcConfig.CommonGroupsLimitWithFriend + if limit <= 0 { + limit = 3 + } + if len(groups) > limit { + groups = groups[:limit] + } + return &pbgroup.GetCommonGroupsWithFriendResp{ - Total: uint32(len(groups)), + Total: uint32(total), Groups: groups, }, nil } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 4cd202db4..df92eed4c 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -278,6 +278,7 @@ type Group struct { } `mapstructure:"rpc"` Prometheus Prometheus `mapstructure:"prometheus"` EnableHistoryForNewMembers bool `mapstructure:"enableHistoryForNewMembers"` + CommonGroupsLimitWithFriend int `mapstructure:"commonGroupsLimitWithFriend"` } type Msg struct { diff --git a/scripts/get_common_group.sh b/scripts/get_common_group.sh index 5936ec612..8fc0ac22f 100755 --- a/scripts/get_common_group.sh +++ b/scripts/get_common_group.sh @@ -3,9 +3,9 @@ set -euo pipefail # ====== 按需修改 ====== API_BASE="${API_BASE:-http://127.0.0.1:10002}" # 你的 open-im-api 地址 -SELF_USER_ID="${SELF_USER_ID:-4642714021}" # 当前登录用户(拿 token 的用户) +SELF_USER_ID="${SELF_USER_ID:-5694418935}" # 当前登录用户(拿 token 的用户) #FRIEND_USER_ID="${FRIEND_USER_ID:-1971806090}" # 要查询共同群的好友 -FRIEND_USER_ID="${FRIEND_USER_ID:-3870738564}" # 要查询共同群的好友 +FRIEND_USER_ID="${FRIEND_USER_ID:-1011009748}" # 要查询共同群的好友 PLATFORM_ID="${PLATFORM_ID:-2}" # 1=iOS, 2=Android, 3=Windows... ADMIN_USER_ID="${ADMIN_USER_ID:-imAdmin}" # 管理员账号(用于签发用户 token) ADMIN_SECRET="${ADMIN_SECRET:-openIM123}" # 配置中的 share.secret