From 876e3d98259d455d83a2ddd92cb3f7173ad90db8 Mon Sep 17 00:00:00 2001 From: dusk Date: Wed, 13 Aug 2025 06:38:27 +0300 Subject: [PATCH] appview: db: follow: add GetFollowers and GetFollowing functions to fetch Follows Change-Id: vwmxzzsuuxmvxrxskqorsnsplnrqxkrv Signed-off-by: dusk --- appview/db/follow.go | 104 +++++++++++++++++++++++++---------------- appview/db/timeline.go | 2 +- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/appview/db/follow.go b/appview/db/follow.go index 3a5b97f..fc7b5aa 100644 --- a/appview/db/follow.go +++ b/appview/db/follow.go @@ -1,7 +1,9 @@ package db import ( + "fmt" "log" + "strings" "time" ) @@ -56,7 +58,7 @@ func DeleteFollowByRkey(e Execer, userDid, rkey string) error { func GetFollowerFollowingCount(e Execer, did string) (int, int, error) { followers, following := 0, 0 err := e.QueryRow( - `SELECT + `SELECT COUNT(CASE WHEN subject_did = ? THEN 1 END) AS followers, COUNT(CASE WHEN user_did = ? THEN 1 END) AS following FROM follows;`, did, did).Scan(&followers, &following) @@ -66,58 +68,47 @@ func GetFollowerFollowingCount(e Execer, did string) (int, int, error) { return followers, following, nil } -type FollowStatus int - -const ( - IsNotFollowing FollowStatus = iota - IsFollowing - IsSelf -) +func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) { + var follows []Follow -func (s FollowStatus) String() string { - switch s { - case IsNotFollowing: - return "IsNotFollowing" - case IsFollowing: - return "IsFollowing" - case IsSelf: - return "IsSelf" - default: - return "IsNotFollowing" + var conditions []string + var args []any + for _, filter := range filters { + conditions = append(conditions, filter.Condition()) + args = append(args, filter.Arg()...) } -} -func GetFollowStatus(e Execer, userDid, subjectDid string) FollowStatus { - if userDid == subjectDid { - return IsSelf - } else if _, err := GetFollow(e, userDid, subjectDid); err != nil { - return IsNotFollowing - } else { - return IsFollowing + whereClause := "" + if conditions != nil { + whereClause = " where " + strings.Join(conditions, " and ") + } + limitClause := "" + if limit > 0 { + limitClause = fmt.Sprintf(" limit %d", limit) } -} - -func GetAllFollows(e Execer, limit int) ([]Follow, error) { - var follows []Follow rows, err := e.Query(` select user_did, subject_did, followed_at, rkey from follows + %s order by followed_at desc - limit ?`, limit, - ) + %s + `, whereClause, limitClause) if err != nil { return nil, err } - defer rows.Close() - for rows.Next() { var follow Follow var followedAt string - if err := rows.Scan(&follow.UserDid, &follow.SubjectDid, &followedAt, &follow.Rkey); err != nil { + err := rows.Scan( + &follow.UserDid, + &follow.SubjectDid, + &followedAt, + &follow.Rkey, + ) + if err != nil { return nil, err } - followedAtTime, err := time.Parse(time.RFC3339, followedAt) if err != nil { log.Println("unable to determine followed at time") @@ -125,13 +116,46 @@ func GetAllFollows(e Execer, limit int) ([]Follow, error) { } else { follow.FollowedAt = followedAtTime } - follows = append(follows, follow) } + return follows, nil +} - if err := rows.Err(); err != nil { - return nil, err +func GetFollowers(e Execer, did string) ([]Follow, error) { + return GetFollows(e, 0, FilterEq("subject_did", did)) +} + +func GetFollowing(e Execer, did string) ([]Follow, error) { + return GetFollows(e, 0, FilterEq("user_did", did)) +} + +type FollowStatus int + +const ( + IsNotFollowing FollowStatus = iota + IsFollowing + IsSelf +) + +func (s FollowStatus) String() string { + switch s { + case IsNotFollowing: + return "IsNotFollowing" + case IsFollowing: + return "IsFollowing" + case IsSelf: + return "IsSelf" + default: + return "IsNotFollowing" } +} - return follows, nil +func GetFollowStatus(e Execer, userDid, subjectDid string) FollowStatus { + if userDid == subjectDid { + return IsSelf + } else if _, err := GetFollow(e, userDid, subjectDid); err != nil { + return IsNotFollowing + } else { + return IsFollowing + } } diff --git a/appview/db/timeline.go b/appview/db/timeline.go index b4b1638..0354b81 100644 --- a/appview/db/timeline.go +++ b/appview/db/timeline.go @@ -137,7 +137,7 @@ func getTimelineStars(e Execer) ([]TimelineEvent, error) { } func getTimelineFollows(e Execer) ([]TimelineEvent, error) { - follows, err := GetAllFollows(e, Limit) + follows, err := GetFollows(e, Limit) if err != nil { return nil, err } -- 2.43.0