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
+110 -32
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
+25 -11
appview/state/profile.go
··· 57 57 log.Printf("getting profile data for %s: %s", did, err) 58 58 } 59 59 60 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) 60 + followStats, err := db.GetFollowerFollowingCount(s.db, did) 61 61 if err != nil { 62 62 log.Printf("getting follow stats for %s: %s", did, err) 63 63 } ··· 76 76 UserHandle: ident.Handle.String(), 77 77 Profile: profile, 78 78 FollowStatus: followStatus, 79 - FollowersCount: followersCount, 80 - FollowingCount: followingCount, 79 + FollowersCount: followStats.Followers, 80 + FollowingCount: followStats.Following, 81 81 }, 82 82 } 83 83 } ··· 208 208 } 209 209 210 210 id := pageWithProfile.Id 211 + loggedInUser := pageWithProfile.LoggedInUser 211 212 212 213 follows, err := fetchFollows(s.db, id.DID.String()) 213 214 if err != nil { ··· 215 216 } 216 217 217 218 if len(follows) == 0 { 218 - return nil 219 + return &FollowsPageParams{ 220 + LoggedInUser: loggedInUser, 221 + Follows: []pages.FollowCard{}, 222 + Card: pageWithProfile.Card, 223 + } 219 224 } 220 225 221 226 followDids := make([]string, 0, len(follows)) ··· 226 231 profiles, err := db.GetProfiles(s.db, db.FilterIn("did", followDids)) 227 232 if err != nil { 228 233 log.Printf("getting profile for %s: %s", followDids, err) 229 - return nil 230 234 } 231 235 232 - loggedInUser := pageWithProfile.LoggedInUser 236 + followStatsMap, err := db.GetFollowerFollowingCounts(s.db, followDids) 237 + if err != nil { 238 + log.Printf("getting follow counts for %s: %s", followDids, err) 239 + } 240 + 233 241 var loggedInUserFollowing map[string]struct{} 234 242 if loggedInUser != nil { 235 243 following, err := db.GetFollowing(s.db, loggedInUser.Did) ··· 246 254 247 255 followCards := make([]pages.FollowCard, 0, len(follows)) 248 256 for _, did := range followDids { 249 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) 250 - if err != nil { 251 - log.Printf("getting follow stats for %s: %s", did, err) 257 + followStats, exists := followStatsMap[did] 258 + if !exists { 259 + followStats = db.FollowStats{} 252 260 } 253 261 followStatus := db.IsNotFollowing 254 262 if loggedInUserFollowing != nil { ··· 268 276 followCards = append(followCards, pages.FollowCard{ 269 277 UserDid: did, 270 278 FollowStatus: followStatus, 271 - FollowersCount: followersCount, 272 - FollowingCount: followingCount, 279 + FollowersCount: followStats.Followers, 280 + FollowingCount: followStats.Following, 273 281 Profile: profile, 274 282 }) 275 283 } ··· 283 291 284 292 func (s *State) followersPage(w http.ResponseWriter, r *http.Request) { 285 293 followPage := s.followPage(w, r, db.GetFollowers, func(f db.Follow) string { return f.UserDid }) 294 + if followPage == nil { 295 + return 296 + } 286 297 287 298 s.pages.FollowersPage(w, pages.FollowersPageParams{ 288 299 LoggedInUser: followPage.LoggedInUser, ··· 293 304 294 305 func (s *State) followingPage(w http.ResponseWriter, r *http.Request) { 295 306 followPage := s.followPage(w, r, db.GetFollowing, func(f db.Follow) string { return f.SubjectDid }) 307 + if followPage == nil { 308 + return 309 + } 296 310 297 311 s.pages.FollowingPage(w, pages.FollowingPageParams{ 298 312 LoggedInUser: followPage.LoggedInUser,
+3 -3
appview/strings/strings.go
··· 182 182 followStatus = db.GetFollowStatus(s.Db, loggedInUser.Did, id.DID.String()) 183 183 } 184 184 185 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 185 + followStats, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 186 186 if err != nil { 187 187 l.Error("failed to get follow stats", "err", err) 188 188 } ··· 194 194 UserHandle: id.Handle.String(), 195 195 Profile: profile, 196 196 FollowStatus: followStatus, 197 - FollowersCount: followersCount, 198 - FollowingCount: followingCount, 197 + FollowersCount: followStats.Followers, 198 + FollowingCount: followStats.Following, 199 199 }, 200 200 Strings: all, 201 201 })