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 return err 56 } 57 58 - func GetFollowerFollowingCount(e Execer, did string) (int, int, error) { 59 followers, following := 0, 0 60 err := e.QueryRow( 61 `SELECT ··· 63 COUNT(CASE WHEN user_did = ? THEN 1 END) AS following 64 FROM follows;`, did, did).Scan(&followers, &following) 65 if err != nil { 66 - return 0, 0, err 67 } 68 - return followers, following, nil 69 } 70 71 func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) {
··· 55 return err 56 } 57 58 + type FollowStats struct { 59 + Followers int 60 + Following int 61 + } 62 + 63 + func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) { 64 followers, following := 0, 0 65 err := e.QueryRow( 66 `SELECT ··· 68 COUNT(CASE WHEN user_did = ? THEN 1 END) AS following 69 FROM follows;`, did, did).Scan(&followers, &following) 70 if err != nil { 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] = "?" 87 } 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 145 } 146 147 func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) {
+3 -15
appview/db/timeline.go
··· 20 *FollowStats 21 } 22 23 - type FollowStats struct { 24 - Followers int 25 - Following int 26 - } 27 - 28 const Limit = 50 29 30 // TODO: this gathers heterogenous events from different sources and aggregates ··· 156 return nil, err 157 } 158 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 - } 169 } 170 171 var events []TimelineEvent
··· 20 *FollowStats 21 } 22 23 const Limit = 50 24 25 // TODO: this gathers heterogenous events from different sources and aggregates ··· 151 return nil, err 152 } 153 154 + followStatMap, err := GetFollowerFollowingCounts(e, subjects) 155 + if err != nil { 156 + return nil, err 157 } 158 159 var events []TimelineEvent
+25 -11
appview/state/profile.go
··· 57 log.Printf("getting profile data for %s: %s", did, err) 58 } 59 60 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) 61 if err != nil { 62 log.Printf("getting follow stats for %s: %s", did, err) 63 } ··· 76 UserHandle: ident.Handle.String(), 77 Profile: profile, 78 FollowStatus: followStatus, 79 - FollowersCount: followersCount, 80 - FollowingCount: followingCount, 81 }, 82 } 83 } ··· 208 } 209 210 id := pageWithProfile.Id 211 212 follows, err := fetchFollows(s.db, id.DID.String()) 213 if err != nil { ··· 215 } 216 217 if len(follows) == 0 { 218 - return nil 219 } 220 221 followDids := make([]string, 0, len(follows)) ··· 226 profiles, err := db.GetProfiles(s.db, db.FilterIn("did", followDids)) 227 if err != nil { 228 log.Printf("getting profile for %s: %s", followDids, err) 229 - return nil 230 } 231 232 - loggedInUser := pageWithProfile.LoggedInUser 233 var loggedInUserFollowing map[string]struct{} 234 if loggedInUser != nil { 235 following, err := db.GetFollowing(s.db, loggedInUser.Did) ··· 246 247 followCards := make([]pages.FollowCard, 0, len(follows)) 248 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) 252 } 253 followStatus := db.IsNotFollowing 254 if loggedInUserFollowing != nil { ··· 268 followCards = append(followCards, pages.FollowCard{ 269 UserDid: did, 270 FollowStatus: followStatus, 271 - FollowersCount: followersCount, 272 - FollowingCount: followingCount, 273 Profile: profile, 274 }) 275 } ··· 283 284 func (s *State) followersPage(w http.ResponseWriter, r *http.Request) { 285 followPage := s.followPage(w, r, db.GetFollowers, func(f db.Follow) string { return f.UserDid }) 286 287 s.pages.FollowersPage(w, pages.FollowersPageParams{ 288 LoggedInUser: followPage.LoggedInUser, ··· 293 294 func (s *State) followingPage(w http.ResponseWriter, r *http.Request) { 295 followPage := s.followPage(w, r, db.GetFollowing, func(f db.Follow) string { return f.SubjectDid }) 296 297 s.pages.FollowingPage(w, pages.FollowingPageParams{ 298 LoggedInUser: followPage.LoggedInUser,
··· 57 log.Printf("getting profile data for %s: %s", did, err) 58 } 59 60 + followStats, err := db.GetFollowerFollowingCount(s.db, did) 61 if err != nil { 62 log.Printf("getting follow stats for %s: %s", did, err) 63 } ··· 76 UserHandle: ident.Handle.String(), 77 Profile: profile, 78 FollowStatus: followStatus, 79 + FollowersCount: followStats.Followers, 80 + FollowingCount: followStats.Following, 81 }, 82 } 83 } ··· 208 } 209 210 id := pageWithProfile.Id 211 + loggedInUser := pageWithProfile.LoggedInUser 212 213 follows, err := fetchFollows(s.db, id.DID.String()) 214 if err != nil { ··· 216 } 217 218 if len(follows) == 0 { 219 + return &FollowsPageParams{ 220 + LoggedInUser: loggedInUser, 221 + Follows: []pages.FollowCard{}, 222 + Card: pageWithProfile.Card, 223 + } 224 } 225 226 followDids := make([]string, 0, len(follows)) ··· 231 profiles, err := db.GetProfiles(s.db, db.FilterIn("did", followDids)) 232 if err != nil { 233 log.Printf("getting profile for %s: %s", followDids, err) 234 } 235 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 + 241 var loggedInUserFollowing map[string]struct{} 242 if loggedInUser != nil { 243 following, err := db.GetFollowing(s.db, loggedInUser.Did) ··· 254 255 followCards := make([]pages.FollowCard, 0, len(follows)) 256 for _, did := range followDids { 257 + followStats, exists := followStatsMap[did] 258 + if !exists { 259 + followStats = db.FollowStats{} 260 } 261 followStatus := db.IsNotFollowing 262 if loggedInUserFollowing != nil { ··· 276 followCards = append(followCards, pages.FollowCard{ 277 UserDid: did, 278 FollowStatus: followStatus, 279 + FollowersCount: followStats.Followers, 280 + FollowingCount: followStats.Following, 281 Profile: profile, 282 }) 283 } ··· 291 292 func (s *State) followersPage(w http.ResponseWriter, r *http.Request) { 293 followPage := s.followPage(w, r, db.GetFollowers, func(f db.Follow) string { return f.UserDid }) 294 + if followPage == nil { 295 + return 296 + } 297 298 s.pages.FollowersPage(w, pages.FollowersPageParams{ 299 LoggedInUser: followPage.LoggedInUser, ··· 304 305 func (s *State) followingPage(w http.ResponseWriter, r *http.Request) { 306 followPage := s.followPage(w, r, db.GetFollowing, func(f db.Follow) string { return f.SubjectDid }) 307 + if followPage == nil { 308 + return 309 + } 310 311 s.pages.FollowingPage(w, pages.FollowingPageParams{ 312 LoggedInUser: followPage.LoggedInUser,
+3 -3
appview/strings/strings.go
··· 182 followStatus = db.GetFollowStatus(s.Db, loggedInUser.Did, id.DID.String()) 183 } 184 185 - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 186 if err != nil { 187 l.Error("failed to get follow stats", "err", err) 188 } ··· 194 UserHandle: id.Handle.String(), 195 Profile: profile, 196 FollowStatus: followStatus, 197 - FollowersCount: followersCount, 198 - FollowingCount: followingCount, 199 }, 200 Strings: all, 201 })
··· 182 followStatus = db.GetFollowStatus(s.Db, loggedInUser.Did, id.DID.String()) 183 } 184 185 + followStats, err := db.GetFollowerFollowingCount(s.Db, id.DID.String()) 186 if err != nil { 187 l.Error("failed to get follow stats", "err", err) 188 } ··· 194 UserHandle: id.Handle.String(), 195 Profile: profile, 196 FollowStatus: followStatus, 197 + FollowersCount: followStats.Followers, 198 + FollowingCount: followStats.Following, 199 }, 200 Strings: all, 201 })