From 49add5497225687114185e75de7414d55f4a145f Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Fri, 17 Jul 2026 23:32:58 +0900 Subject: [PATCH] appview/db: query distinct follow rows on query We keep duplicated follow records in db to delete them on unfollow. So instead of having unique constraint, we deduplicate them on query. Signed-off-by: Seongmin Lee --- appview/db/follow.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/appview/db/follow.go b/appview/db/follow.go index e4d44efc..758d5250 100644 --- a/appview/db/follow.go +++ b/appview/db/follow.go @@ -60,7 +60,7 @@ func DeleteFollowByRkey(e Execer, userDid, rkey string) error { // GetMostFollowed returns the DIDs with the most followers, most-followed first. func GetMostFollowed(e Execer, limit int) ([]string, error) { query := ` - select subject_did, count(*) as followers + select subject_did, count(distinct did) as followers from follows group by subject_did order by followers desc @@ -88,8 +88,8 @@ func GetFollowerFollowingCount(e Execer, did string) (models.FollowStats, error) var followers, following int64 err := e.QueryRow( `SELECT - COUNT(CASE WHEN subject_did = ? THEN 1 END) AS followers, - COUNT(CASE WHEN did = ? THEN 1 END) AS following + COUNT(DISTINCT CASE WHEN subject_did = ? THEN did END) AS followers, + COUNT(DISTINCT CASE WHEN did = ? THEN subject_did END) AS following FROM follows;`, did, did).Scan(&followers, &following) if err != nil { return models.FollowStats{}, err @@ -123,13 +123,13 @@ func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]models.Foll coalesce(f.followers, 0) as followers, coalesce(g.following, 0) as following from ( - select subject_did as did, count(*) as followers + select subject_did as did, count(distinct did) as followers from follows where subject_did in (%s) group by subject_did ) f full outer join ( - select did as did, count(*) as following + select did as did, count(distinct subject_did) as following from follows where did in (%s) group by did -- 2.51.2