appview: implement GetFollowersFollowingCounts to query follow stats for multiple dids in one go #505

merged
opened by ptr.pet targeting master from [deleted fork]: followers-following-list
  • moves FollowStats type from timeline.go into follow.go
  • makes existing GetFollowersFollowingCount also use FollowStats

Signed-off-by: dusk y.bera003.06@protonmail.com

Changed files
+98 -29
appview
+79 -3
appview/db/follow.go
··· 55 55 return err 56 56 } 57 57 58 - func GetFollowerFollowingCount(e Execer, did string) (int, int, error) { 58 + type FollowStats struct { 59 + Followers int 60 + Following int 61 + } 62 + 63 + func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) { 59 64 followers, following := 0, 0 60 65 err := e.QueryRow( 61 66 `SELECT ··· 63 68 COUNT(CASE WHEN user_did = ? THEN 1 END) AS following 64 69 FROM follows;`, did, did).Scan(&followers, &following) 65 70 if err != nil { 66 - return 0, 0, err 71 + return FollowStats{}, err 72 + } 73 + return FollowStats{ 74 + Followers: followers, 75 + Following: following, 76 + }, nil 77 + } 78 + 79 + func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]FollowStats, error) { 80 + if len(dids) == 0 { 81 + return nil, nil 82 + } 83 + 84 + placeholders := make([]string, len(dids)) 85 + for i := range placeholders { 86 + placeholders[i] = "?" 67 87 } 68 - return followers, following, nil 88 + placeholderStr := strings.Join(placeholders, ",") 89 + 90 + args := make([]any, len(dids)*2) 91 + for i, did := range dids { 92 + args[i] = did 93 + args[i+len(dids)] = did 94 + } 95 + 96 + query := fmt.Sprintf(` 97 + select 98 + coalesce(f.did, g.did) as did, 99 + coalesce(f.followers, 0) as followers, 100 + coalesce(g.following, 0) as following 101 + from ( 102 + select subject_did as did, count(*) as followers 103 + from follows 104 + where subject_did in (%s) 105 + group by subject_did 106 + ) f 107 + full outer join ( 108 + select user_did as did, count(*) as following 109 + from follows 110 + where user_did in (%s) 111 + group by user_did 112 + ) g on f.did = g.did`, 113 + placeholderStr, placeholderStr) 114 + 115 + result := make(map[string]FollowStats) 116 + 117 + rows, err := e.Query(query, args...) 118 + if err != nil { 119 + return nil, err 120 + } 121 + defer rows.Close() 122 + 123 + for rows.Next() { 124 + var did string 125 + var followers, following int 126 + if err := rows.Scan(&did, &followers, &following); err != nil { 127 + return nil, err 128 + } 129 + result[did] = FollowStats{ 130 + Followers: followers, 131 + Following: following, 132 + } 133 + } 134 + 135 + for _, did := range dids { 136 + if _, exists := result[did]; !exists { 137 + result[did] = FollowStats{ 138 + Followers: 0, 139 + Following: 0, 140 + } 141 + } 142 + } 143 + 144 + return result, nil 69 145 } 70 146 71 147 func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) {
+3 -15
appview/db/timeline.go
··· 20 20 *FollowStats 21 21 } 22 22 23 - type FollowStats struct { 24 - Followers int 25 - Following int 26 - } 27 - 28 23 const Limit = 50 29 24 30 25 // TODO: this gathers heterogenous events from different sources and aggregates ··· 156 151 return nil, err 157 152 } 158 153 159 - followStatMap := make(map[string]FollowStats) 160 - for _, s := range subjects { 161 - followers, following, err := GetFollowerFollowingCount(e, s) 162 - if err != nil { 163 - return nil, err 164 - } 165 - followStatMap[s] = FollowStats{ 166 - Followers: followers, 167 - Following: following, 168 - } 154 + followStatMap, err := GetFollowerFollowingCounts(e, subjects) 155 + if err != nil { 156 + return nil, err 169 157 } 170 158 171 159 var events []TimelineEvent
+13 -8
appview/state/profile.go
··· 63 63 return nil 64 64 } 65 65 66 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) 66 + followStats, err := db.GetFollowerFollowingCount(s.db, did) 67 67 if err != nil { 68 68 log.Printf("getting follow stats for %s: %s", did, err) 69 69 } ··· 82 82 UserHandle: ident.Handle.String(), 83 83 Profile: profile, 84 84 FollowStatus: followStatus, 85 - FollowersCount: followersCount, 86 - FollowingCount: followingCount, 85 + FollowersCount: followStats.Followers, 86 + FollowingCount: followStats.Following, 87 87 }, 88 88 } 89 89 } ··· 241 241 return FollowsPageParams{}, err 242 242 } 243 243 244 + followStatsMap, err := db.GetFollowerFollowingCounts(s.db, followDids) 245 + if err != nil { 246 + log.Printf("getting follow counts for %s: %s", followDids, err) 247 + } 248 + 244 249 var loggedInUserFollowing map[string]struct{} 245 250 if loggedInUser != nil { 246 251 following, err := db.GetFollowing(s.db, loggedInUser.Did) ··· 257 262 258 263 followCards := make([]pages.FollowCard, 0, len(follows)) 259 264 for _, did := range followDids { 260 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) 261 - if err != nil { 262 - log.Printf("getting follow stats for %s: %s", did, err) 265 + followStats, exists := followStatsMap[did] 266 + if !exists { 267 + followStats = db.FollowStats{} 263 268 } 264 269 followStatus := db.IsNotFollowing 265 270 if loggedInUserFollowing != nil { ··· 279 284 followCards = append(followCards, pages.FollowCard{ 280 285 UserDid: did, 281 286 FollowStatus: followStatus, 282 - FollowersCount: followersCount, 283 - FollowingCount: followingCount, 287 + FollowersCount: followStats.Followers, 288 + FollowingCount: followStats.Following, 284 289 Profile: profile, 285 290 }) 286 291 }
+3 -3
appview/strings/strings.go
··· 202 202 followStatus = db.GetFollowStatus(s.Db, loggedInUser.Did, id.DID.String()) 203 203 } 204 204 205 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 205 + followStats, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 206 206 if err != nil { 207 207 l.Error("failed to get follow stats", "err", err) 208 208 } ··· 214 214 UserHandle: id.Handle.String(), 215 215 Profile: profile, 216 216 FollowStatus: followStatus, 217 - FollowersCount: followersCount, 218 - FollowingCount: followingCount, 217 + FollowersCount: followStats.Followers, 218 + FollowingCount: followStats.Following, 219 219 }, 220 220 Strings: all, 221 221 })