Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview/models: move db.Follow into models

Signed-off-by: oppiliappan <me@oppi.li>

+98 -85
+26 -57
appview/db/follow.go
··· 5 5 "log" 6 6 "strings" 7 7 "time" 8 + 9 + "tangled.org/core/appview/models" 8 10 ) 9 11 10 - type Follow struct { 11 - UserDid string 12 - SubjectDid string 13 - FollowedAt time.Time 14 - Rkey string 15 - } 16 - 17 - func AddFollow(e Execer, follow *Follow) error { 12 + func AddFollow(e Execer, follow *models.Follow) error { 18 13 query := `insert or ignore into follows (user_did, subject_did, rkey) values (?, ?, ?)` 19 14 _, err := e.Exec(query, follow.UserDid, follow.SubjectDid, follow.Rkey) 20 15 return err 21 16 } 22 17 23 18 // Get a follow record 24 - func GetFollow(e Execer, userDid, subjectDid string) (*Follow, error) { 19 + func GetFollow(e Execer, userDid, subjectDid string) (*models.Follow, error) { 25 20 query := `select user_did, subject_did, followed_at, rkey from follows where user_did = ? and subject_did = ?` 26 21 row := e.QueryRow(query, userDid, subjectDid) 27 22 28 - var follow Follow 23 + var follow models.Follow 29 24 var followedAt string 30 25 err := row.Scan(&follow.UserDid, &follow.SubjectDid, &followedAt, &follow.Rkey) 31 26 if err != nil { ··· 50 55 return err 51 56 } 52 57 53 - type FollowStats struct { 54 - Followers int64 55 - Following int64 56 - } 57 - 58 - func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) { 58 + func GetFollowerFollowingCount(e Execer, did string) (models.FollowStats, error) { 59 59 var followers, following int64 60 60 err := e.QueryRow( 61 61 `SELECT ··· 58 68 COUNT(CASE WHEN user_did = ? THEN 1 END) AS following 59 69 FROM follows;`, did, did).Scan(&followers, &following) 60 70 if err != nil { 61 - return FollowStats{}, err 71 + return models.FollowStats{}, err 62 72 } 63 - return FollowStats{ 73 + return models.FollowStats{ 64 74 Followers: followers, 65 75 Following: following, 66 76 }, nil 67 77 } 68 78 69 - func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]FollowStats, error) { 79 + func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]models.FollowStats, error) { 70 80 if len(dids) == 0 { 71 81 return nil, nil 72 82 } ··· 102 112 ) g on f.did = g.did`, 103 113 placeholderStr, placeholderStr) 104 114 105 - result := make(map[string]FollowStats) 115 + result := make(map[string]models.FollowStats) 106 116 107 117 rows, err := e.Query(query, args...) 108 118 if err != nil { ··· 116 126 if err := rows.Scan(&did, &followers, &following); err != nil { 117 127 return nil, err 118 128 } 119 - result[did] = FollowStats{ 129 + result[did] = models.FollowStats{ 120 130 Followers: followers, 121 131 Following: following, 122 132 } ··· 124 134 125 135 for _, did := range dids { 126 136 if _, exists := result[did]; !exists { 127 - result[did] = FollowStats{ 137 + result[did] = models.FollowStats{ 128 138 Followers: 0, 129 139 Following: 0, 130 140 } ··· 134 144 return result, nil 135 145 } 136 146 137 - func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) { 138 - var follows []Follow 147 + func GetFollows(e Execer, limit int, filters ...filter) ([]models.Follow, error) { 148 + var follows []models.Follow 139 149 140 150 var conditions []string 141 151 var args []any ··· 167 177 return nil, err 168 178 } 169 179 for rows.Next() { 170 - var follow Follow 180 + var follow models.Follow 171 181 var followedAt string 172 182 err := rows.Scan( 173 183 &follow.UserDid, ··· 190 200 return follows, nil 191 201 } 192 202 193 - func GetFollowers(e Execer, did string) ([]Follow, error) { 203 + func GetFollowers(e Execer, did string) ([]models.Follow, error) { 194 204 return GetFollows(e, 0, FilterEq("subject_did", did)) 195 205 } 196 206 197 - func GetFollowing(e Execer, did string) ([]Follow, error) { 207 + func GetFollowing(e Execer, did string) ([]models.Follow, error) { 198 208 return GetFollows(e, 0, FilterEq("user_did", did)) 199 209 } 200 210 201 - type FollowStatus int 202 - 203 - const ( 204 - IsNotFollowing FollowStatus = iota 205 - IsFollowing 206 - IsSelf 207 - ) 208 - 209 - func (s FollowStatus) String() string { 210 - switch s { 211 - case IsNotFollowing: 212 - return "IsNotFollowing" 213 - case IsFollowing: 214 - return "IsFollowing" 215 - case IsSelf: 216 - return "IsSelf" 217 - default: 218 - return "IsNotFollowing" 219 - } 220 - } 221 - 222 - func getFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]FollowStatus, error) { 211 + func getFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]models.FollowStatus, error) { 223 212 if len(subjectDids) == 0 || userDid == "" { 224 - return make(map[string]FollowStatus), nil 213 + return make(map[string]models.FollowStatus), nil 225 214 } 226 215 227 - result := make(map[string]FollowStatus) 216 + result := make(map[string]models.FollowStatus) 228 217 229 218 for _, subjectDid := range subjectDids { 230 219 if userDid == subjectDid { 231 - result[subjectDid] = IsSelf 220 + result[subjectDid] = models.IsSelf 232 221 } else { 233 - result[subjectDid] = IsNotFollowing 222 + result[subjectDid] = models.IsNotFollowing 234 223 } 235 224 } 236 225 ··· 250 281 if err := rows.Scan(&subjectDid); err != nil { 251 282 return nil, err 252 283 } 253 - result[subjectDid] = IsFollowing 284 + result[subjectDid] = models.IsFollowing 254 285 } 255 286 256 287 return result, nil 257 288 } 258 289 259 - func GetFollowStatus(e Execer, userDid, subjectDid string) FollowStatus { 290 + func GetFollowStatus(e Execer, userDid, subjectDid string) models.FollowStatus { 260 291 statuses, err := getFollowStatuses(e, userDid, []string{subjectDid}) 261 292 if err != nil { 262 - return IsNotFollowing 293 + return models.IsNotFollowing 263 294 } 264 295 return statuses[subjectDid] 265 296 } 266 297 267 - func GetFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]FollowStatus, error) { 298 + func GetFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]models.FollowStatus, error) { 268 299 return getFollowStatuses(e, userDid, subjectDids) 269 300 }
+6 -5
appview/db/timeline.go
··· 5 5 "time" 6 6 7 7 "github.com/bluesky-social/indigo/atproto/syntax" 8 + "tangled.org/core/appview/models" 8 9 ) 9 10 10 11 type TimelineEvent struct { 11 12 *Repo 12 - *Follow 13 + *models.Follow 13 14 *Star 14 15 15 16 EventAt time.Time ··· 20 19 21 20 // optional: populate only if event is Follow 22 21 *Profile 23 - *FollowStats 24 - *FollowStatus 22 + *models.FollowStats 23 + *models.FollowStatus 25 24 26 25 // optional: populate only if event is Repo 27 26 IsStarred bool ··· 212 211 return nil, err 213 212 } 214 213 215 - var followStatuses map[string]FollowStatus 214 + var followStatuses map[string]models.FollowStatus 216 215 if loggedInUserDid != "" { 217 216 followStatuses, err = GetFollowStatuses(e, loggedInUserDid, subjects) 218 217 if err != nil { ··· 225 224 profile, _ := profiles[f.SubjectDid] 226 225 followStatMap, _ := followStatMap[f.SubjectDid] 227 226 228 - followStatus := IsNotFollowing 227 + followStatus := models.IsNotFollowing 229 228 if followStatuses != nil { 230 229 followStatus = followStatuses[f.SubjectDid] 231 230 }
+1 -1
appview/ingester.go
··· 149 149 return err 150 150 } 151 151 152 - err = db.AddFollow(i.Db, &db.Follow{ 152 + err = db.AddFollow(i.Db, &models.Follow{ 153 153 UserDid: did, 154 154 SubjectDid: record.Subject, 155 155 Rkey: e.Commit.RKey,
+38
appview/models/follow.go
··· 1 + package models 2 + 3 + import ( 4 + "time" 5 + ) 6 + 7 + type Follow struct { 8 + UserDid string 9 + SubjectDid string 10 + FollowedAt time.Time 11 + Rkey string 12 + } 13 + 14 + type FollowStats struct { 15 + Followers int64 16 + Following int64 17 + } 18 + 19 + type FollowStatus int 20 + 21 + const ( 22 + IsNotFollowing FollowStatus = iota 23 + IsFollowing 24 + IsSelf 25 + ) 26 + 27 + func (s FollowStatus) String() string { 28 + switch s { 29 + case IsNotFollowing: 30 + return "IsNotFollowing" 31 + case IsFollowing: 32 + return "IsFollowing" 33 + case IsSelf: 34 + return "IsSelf" 35 + default: 36 + return "IsNotFollowing" 37 + } 38 + }
+3 -2
appview/notify/merged_notifier.go
··· 4 4 "context" 5 5 6 6 "tangled.org/core/appview/db" 7 + "tangled.org/core/appview/models" 7 8 ) 8 9 9 10 type mergedNotifier struct { ··· 40 39 } 41 40 } 42 41 43 - func (m *mergedNotifier) NewFollow(ctx context.Context, follow *db.Follow) { 42 + func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 44 43 for _, notifier := range m.notifiers { 45 44 notifier.NewFollow(ctx, follow) 46 45 } 47 46 } 48 - func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) { 47 + func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 49 48 for _, notifier := range m.notifiers { 50 49 notifier.DeleteFollow(ctx, follow) 51 50 }
+5 -4
appview/notify/notifier.go
··· 4 4 "context" 5 5 6 6 "tangled.org/core/appview/db" 7 + "tangled.org/core/appview/models" 7 8 ) 8 9 9 10 type Notifier interface { ··· 15 14 16 15 NewIssue(ctx context.Context, issue *db.Issue) 17 16 18 - NewFollow(ctx context.Context, follow *db.Follow) 19 - DeleteFollow(ctx context.Context, follow *db.Follow) 17 + NewFollow(ctx context.Context, follow *models.Follow) 18 + DeleteFollow(ctx context.Context, follow *models.Follow) 20 19 21 20 NewPull(ctx context.Context, pull *db.Pull) 22 21 NewPullComment(ctx context.Context, comment *db.PullComment) ··· 40 39 41 40 func (m *BaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) {} 42 41 43 - func (m *BaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) {} 44 - func (m *BaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {} 42 + func (m *BaseNotifier) NewFollow(ctx context.Context, follow *models.Follow) {} 43 + func (m *BaseNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {} 45 44 46 45 func (m *BaseNotifier) NewPull(ctx context.Context, pull *db.Pull) {} 47 46 func (m *BaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {}
+3 -3
appview/pages/pages.go
··· 411 411 type ProfileCard struct { 412 412 UserDid string 413 413 UserHandle string 414 - FollowStatus db.FollowStatus 414 + FollowStatus models.FollowStatus 415 415 Punchcard *db.Punchcard 416 416 Profile *db.Profile 417 417 Stats ProfileStats ··· 489 489 490 490 type FollowCard struct { 491 491 UserDid string 492 - FollowStatus db.FollowStatus 492 + FollowStatus models.FollowStatus 493 493 FollowersCount int64 494 494 FollowingCount int64 495 495 Profile *db.Profile ··· 521 521 522 522 type FollowFragmentParams struct { 523 523 UserDid string 524 - FollowStatus db.FollowStatus 524 + FollowStatus models.FollowStatus 525 525 } 526 526 527 527 func (p *Pages) FollowFragment(w io.Writer, params FollowFragmentParams) error {
+3 -2
appview/posthog/notifier.go
··· 6 6 7 7 "github.com/posthog/posthog-go" 8 8 "tangled.org/core/appview/db" 9 + "tangled.org/core/appview/models" 9 10 "tangled.org/core/appview/notify" 10 11 ) 11 12 ··· 99 98 } 100 99 } 101 100 102 - func (n *posthogNotifier) NewFollow(ctx context.Context, follow *db.Follow) { 101 + func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 103 102 err := n.client.Enqueue(posthog.Capture{ 104 103 DistinctId: follow.UserDid, 105 104 Event: "follow", ··· 110 109 } 111 110 } 112 111 113 - func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) { 112 + func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 114 113 err := n.client.Enqueue(posthog.Capture{ 115 114 DistinctId: follow.UserDid, 116 115 Event: "unfollow",
+4 -3
appview/state/follow.go
··· 9 9 lexutil "github.com/bluesky-social/indigo/lex/util" 10 10 "tangled.org/core/api/tangled" 11 11 "tangled.org/core/appview/db" 12 + "tangled.org/core/appview/models" 12 13 "tangled.org/core/appview/pages" 13 14 "tangled.org/core/tid" 14 15 ) ··· 60 59 61 60 log.Println("created atproto record: ", resp.Uri) 62 61 63 - follow := &db.Follow{ 62 + follow := &models.Follow{ 64 63 UserDid: currentUser.Did, 65 64 SubjectDid: subjectIdent.DID.String(), 66 65 Rkey: rkey, ··· 76 75 77 76 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 78 77 UserDid: subjectIdent.DID.String(), 79 - FollowStatus: db.IsFollowing, 78 + FollowStatus: models.IsFollowing, 80 79 }) 81 80 82 81 return ··· 107 106 108 107 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 109 108 UserDid: subjectIdent.DID.String(), 110 - FollowStatus: db.IsNotFollowing, 109 + FollowStatus: models.IsNotFollowing, 111 110 }) 112 111 113 112 s.notifier.DeleteFollow(r.Context(), follow)
+9 -8
appview/state/profile.go
··· 17 17 "github.com/gorilla/feeds" 18 18 "tangled.org/core/api/tangled" 19 19 "tangled.org/core/appview/db" 20 + "tangled.org/core/appview/models" 20 21 "tangled.org/core/appview/pages" 21 22 ) 22 23 ··· 77 76 } 78 77 79 78 loggedInUser := s.oauth.GetUser(r) 80 - followStatus := db.IsNotFollowing 79 + followStatus := models.IsNotFollowing 81 80 if loggedInUser != nil { 82 81 followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did) 83 82 } ··· 272 271 273 272 func (s *State) followPage( 274 273 r *http.Request, 275 - fetchFollows func(db.Execer, string) ([]db.Follow, error), 276 - extractDid func(db.Follow) string, 274 + fetchFollows func(db.Execer, string) ([]models.Follow, error), 275 + extractDid func(models.Follow) string, 277 276 ) (*FollowsPageParams, error) { 278 277 l := s.logger.With("handler", "reposPage") 279 278 ··· 330 329 followCards := make([]pages.FollowCard, len(follows)) 331 330 for i, did := range followDids { 332 331 followStats := followStatsMap[did] 333 - followStatus := db.IsNotFollowing 332 + followStatus := models.IsNotFollowing 334 333 if _, exists := loggedInUserFollowing[did]; exists { 335 - followStatus = db.IsFollowing 334 + followStatus = models.IsFollowing 336 335 } else if loggedInUser != nil && loggedInUser.Did == did { 337 - followStatus = db.IsSelf 336 + followStatus = models.IsSelf 338 337 } 339 338 340 339 var profile *db.Profile ··· 359 358 } 360 359 361 360 func (s *State) followersPage(w http.ResponseWriter, r *http.Request) { 362 - followPage, err := s.followPage(r, db.GetFollowers, func(f db.Follow) string { return f.UserDid }) 361 + followPage, err := s.followPage(r, db.GetFollowers, func(f models.Follow) string { return f.UserDid }) 363 362 if err != nil { 364 363 s.pages.Notice(w, "all-followers", "Failed to load followers") 365 364 return ··· 373 372 } 374 373 375 374 func (s *State) followingPage(w http.ResponseWriter, r *http.Request) { 376 - followPage, err := s.followPage(r, db.GetFollowing, func(f db.Follow) string { return f.SubjectDid }) 375 + followPage, err := s.followPage(r, db.GetFollowing, func(f models.Follow) string { return f.SubjectDid }) 377 376 if err != nil { 378 377 s.pages.Notice(w, "all-following", "Failed to load following") 379 378 return