fix: expose friend online status

This commit is contained in:
richjohnrambo
2026-09-08 10:35:42 +08:00
parent b295170eee
commit d239bd9ad0
+14 -3
View File
@@ -4463,7 +4463,7 @@ func serveIMFriends(wrt http.ResponseWriter, req *http.Request) {
if item.Friendship.PeerID == principal.Uid { if item.Friendship.PeerID == principal.Uid {
continue continue
} }
payload = append(payload, friendListItemPayload(item)) payload = append(payload, friendListItemPayload(principal.TenantID, principal.Uid, item))
} }
writeIMSuccess(wrt, req, map[string]any{"items": payload, "next_cursor": nextCursor}) writeIMSuccess(wrt, req, map[string]any{"items": payload, "next_cursor": nextCursor})
} }
@@ -8967,13 +8967,20 @@ func friendRequestListItemPayload(item types.FriendRequestListItem) map[string]a
return data return data
} }
func friendListItemPayload(item types.FriendListItem) map[string]any { func friendListItemPayload(tenantID types.TenantID, viewer types.Uid, item types.FriendListItem) map[string]any {
online := false
onlineVisibility := "hidden"
if item.Friendship.Status == types.RelationshipFriend && imCanViewLastSeen(tenantID, viewer, item.Friendship.PeerID) {
onlineVisibility = "visible"
online = redisUserOnline(tenantID, item.Friendship.PeerID, false)
}
data := map[string]any{ data := map[string]any{
"user_id": item.Friendship.PeerID.UserId(), "user_id": item.Friendship.PeerID.UserId(),
"relationship": item.Friendship.Status, "relationship": item.Friendship.Status,
"remark": item.Friendship.Remark, "remark": item.Friendship.Remark,
"topic": item.Friendship.Topic, "topic": item.Friendship.Topic,
"online_visibility": "unknown", "online": online,
"online_visibility": onlineVisibility,
"created_at": item.Friendship.CreatedAt.UTC().Format(time.RFC3339Nano), "created_at": item.Friendship.CreatedAt.UTC().Format(time.RFC3339Nano),
"updated_at": item.Friendship.UpdatedAt.UTC().Format(time.RFC3339Nano), "updated_at": item.Friendship.UpdatedAt.UTC().Format(time.RFC3339Nano),
"capabilities": map[string]any{"can_send": item.Friendship.Status == types.RelationshipFriend, "can_call": imCanCall(item.Friendship.Status == types.RelationshipFriend)}, "capabilities": map[string]any{"can_send": item.Friendship.Status == types.RelationshipFriend, "can_call": imCanCall(item.Friendship.Status == types.RelationshipFriend)},
@@ -8990,6 +8997,10 @@ func friendListItemPayload(item types.FriendListItem) map[string]any {
data[key] = value data[key] = value
} }
} }
data["peer"] = userSummaryPayload(item.Peer, item.Friendship.Remark)
if peer, ok := data["peer"].(map[string]any); ok {
peer["online"] = online
}
} }
if item.Friendship.Remark == "" && data["display_name"] == data["user_id"] { if item.Friendship.Remark == "" && data["display_name"] == data["user_id"] {
data["display_name_source"] = "user_id" data["display_name_source"] = "user_id"