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
+65 -1
Diff #0
+65 -1
appview/db/follow.go
··· 56 56 func GetFollowerFollowingCount(e Execer, did string) (int, int, error) { 57 57 followers, following := 0, 0 58 58 err := e.QueryRow( 59 - `SELECT 59 + `SELECT 60 60 COUNT(CASE WHEN subject_did = ? THEN 1 END) AS followers, 61 61 COUNT(CASE WHEN user_did = ? THEN 1 END) AS following 62 62 FROM follows;`, did, did).Scan(&followers, &following) ··· 66 66 return followers, following, nil 67 67 } 68 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 + } 109 + for rows.Next() { 110 + var follow Follow 111 + var followedAt string 112 + err := rows.Scan( 113 + &follow.UserDid, 114 + &follow.SubjectDid, 115 + &followedAt, 116 + &follow.Rkey, 117 + ) 118 + if err != nil { 119 + return nil, err 120 + } 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 + 69 133 type FollowStatus int 70 134 71 135 const (

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