Monorepo for Tangled tangled.org

appview: db: follow: add GetFollowers and GetFollowing functions to fetch Follows #483

merged opened by ptr.pet targeting master from [deleted fork]: followers-following-list
Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:dfl62fgb7wtjj3fcbb72naae/sh.tangled.repo.pull/3lwawsuk3dd22
+36 -76
Interdiff #0 #1
+35 -75
appview/db/follow.go
··· 1 package db 2 3 import ( 4 "log" 5 "time" 6 ) 7 ··· 66 return followers, following, nil 67 } 68 69 - func GetFollowers(e Execer, did string) ([]Follow, error) { 70 - var followers []Follow 71 - rows, err := e.Query(` 72 - select user_did, subject_did, followed_at, rkey from follows where subject_did = ? 73 - `, did) 74 - if err != nil { 75 - return nil, err 76 } 77 - for rows.Next() { 78 - var follow Follow 79 - var followedAt string 80 - err := rows.Scan( 81 - &follow.UserDid, 82 - &follow.SubjectDid, 83 - &followedAt, 84 - &follow.Rkey, 85 - ) 86 - if err != nil { 87 - return nil, err 88 - } 89 - followedAtTime, err := time.Parse(time.RFC3339, followedAt) 90 - if err != nil { 91 - log.Println("unable to determine followed at time") 92 - follow.FollowedAt = time.Now() 93 - } else { 94 - follow.FollowedAt = followedAtTime 95 - } 96 - followers = append(followers, follow) 97 } 98 - return followers, nil 99 - } 100 101 - func GetFollowing(e Execer, did string) ([]Follow, error) { 102 - var following []Follow 103 rows, err := e.Query(` 104 - select user_did, subject_did, followed_at, rkey from follows where user_did = ? 105 - `, did) 106 if err != nil { 107 return nil, err 108 } ··· 121 followedAtTime, err := time.Parse(time.RFC3339, followedAt) 122 if err != nil { 123 log.Println("unable to determine followed at time") 124 - follow.FollowedAt = time.Now() 125 } else { 126 follow.FollowedAt = followedAtTime 127 } 128 - following = append(following, follow) 129 } 130 - return following, nil 131 } 132 133 type FollowStatus int ··· 160 return IsFollowing 161 } 162 } 163 - 164 - func GetAllFollows(e Execer, limit int) ([]Follow, error) { 165 - var follows []Follow 166 - 167 - rows, err := e.Query(` 168 - select user_did, subject_did, followed_at, rkey 169 - from follows 170 - order by followed_at desc 171 - limit ?`, limit, 172 - ) 173 - if err != nil { 174 - return nil, err 175 - } 176 - defer rows.Close() 177 - 178 - for rows.Next() { 179 - var follow Follow 180 - var followedAt string 181 - if err := rows.Scan(&follow.UserDid, &follow.SubjectDid, &followedAt, &follow.Rkey); err != nil { 182 - return nil, err 183 - } 184 - 185 - followedAtTime, err := time.Parse(time.RFC3339, followedAt) 186 - if err != nil { 187 - log.Println("unable to determine followed at time") 188 - 189 - } else { 190 - follow.FollowedAt = followedAtTime 191 - } 192 - 193 - follows = append(follows, follow) 194 - } 195 - 196 - if err := rows.Err(); err != nil { 197 - return nil, err 198 - } 199 - 200 - return follows, nil 201 - }
··· 1 package db 2 3 import ( 4 + "fmt" 5 "log" 6 + "strings" 7 "time" 8 ) 9 ··· 68 return followers, following, nil 69 } 70 71 + func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) { 72 + var follows []Follow 73 + 74 + var conditions []string 75 + var args []any 76 + for _, filter := range filters { 77 + conditions = append(conditions, filter.Condition()) 78 + args = append(args, filter.Arg()...) 79 } 80 + 81 + whereClause := "" 82 + if conditions != nil { 83 + whereClause = " where " + strings.Join(conditions, " and ") 84 + } 85 + limitClause := "" 86 + if limit > 0 { 87 + limitClause = fmt.Sprintf(" limit %d", limit) 88 } 89 90 rows, err := e.Query(` 91 + select user_did, subject_did, followed_at, rkey 92 + from follows 93 + %s 94 + order by followed_at desc 95 + %s 96 + `, whereClause, limitClause) 97 if err != nil { 98 return nil, err 99 } ··· 112 followedAtTime, err := time.Parse(time.RFC3339, followedAt) 113 if err != nil { 114 log.Println("unable to determine followed at time") 115 + 116 } else { 117 follow.FollowedAt = followedAtTime 118 } 119 + follows = append(follows, follow) 120 } 121 + return follows, nil 122 + } 123 + 124 + func GetFollowers(e Execer, did string) ([]Follow, error) { 125 + return GetFollows(e, 0, FilterEq("subject_did", did)) 126 + } 127 + 128 + func GetFollowing(e Execer, did string) ([]Follow, error) { 129 + return GetFollows(e, 0, FilterEq("user_did", did)) 130 } 131 132 type FollowStatus int ··· 159 return IsFollowing 160 } 161 }
+1 -1
appview/db/timeline.go
··· 137 } 138 139 func getTimelineFollows(e Execer) ([]TimelineEvent, error) { 140 - follows, err := GetAllFollows(e, Limit) 141 if err != nil { 142 return nil, err 143 }
··· 137 } 138 139 func getTimelineFollows(e Execer) ([]TimelineEvent, error) { 140 + follows, err := GetFollows(e, Limit) 141 if err != nil { 142 return nil, err 143 }

History

3 rounds 3 comments
sign up or login to add to the discussion
1 commit
expand
1934494f
appview: db: follow: add GetFollowers and GetFollowing functions to fetch Follows
expand 1 comment

oops T.T actually fixed it

pull request successfully merged
1 commit
expand
876e3d98
appview: db: follow: add GetFollowers and GetFollowing functions to fetch Follows
expand 1 comment

made a GetFollows, and made the GetFollowers and GetFollowing use that (and also removed GetAllFollows since its not necessary with GetFollows)

ptr.pet submitted #0
1 commit
expand
b6358b5b
appview: db: follow: add GetFollowers and GetFollowing functions to fetch Follows
expand 1 comment

this is pretty cool! couple of points:

  • the GetFollow* methods are a bit repetitive (in part due to the database/sql APIs), can we dedup them like we do in the rest of the codebase? the approach we use here is to write GetItem(e Execer, filters ...filter) ([]Item, error) and then use filters to create more specific getters. you can look at db/spindles for an example
  • we could do something similar for GetFollowStatus i think