Yōten: A social tracker for your language learning journey built on the atproto.

fix: follow status

brookjeynes.dev 6ccbe4bd bbfbd817

verified
+28 -8
+28 -8
internal/db/follow.go
··· 1 1 package db 2 2 3 3 import ( 4 + "database/sql" 4 5 "fmt" 6 + "log" 5 7 "strings" 6 8 "time" 7 9 ) ··· 76 78 return IsSelf 77 79 } 78 80 79 - var follows, isFollowed bool 80 81 query := ` 81 - select 82 - exists(select 1 from follows where user_did = ? and subject_did = ?), 83 - exists(select 1 from follows where user_did = ? and subject_did = ?) 84 - ` 85 - err := e.QueryRow(query, userDid, subjectDid, subjectDid, userDid).Scan(&follows, &isFollowed) 82 + SELECT 83 + -- Count of rows where the user follows the subject 84 + COUNT(CASE WHEN user_did = ? AND subject_did = ? THEN 1 END), 85 + -- Count of rows where the subject follows the user 86 + COUNT(CASE WHEN user_did = ? AND subject_did = ? THEN 1 END) 87 + FROM 88 + follows 89 + WHERE 90 + (user_did = ? AND subject_did = ?) OR (user_did = ? AND subject_did = ?); 91 + ` 92 + 93 + var userFollowsSubject, subjectFollowsUser int 94 + err := e.QueryRow( 95 + query, 96 + userDid, subjectDid, 97 + subjectDid, userDid, 98 + userDid, subjectDid, 99 + subjectDid, userDid, 100 + ).Scan(&userFollowsSubject, &subjectFollowsUser) 101 + 86 102 if err != nil { 103 + if err == sql.ErrNoRows { 104 + return IsNotFollowing 105 + } 106 + log.Printf("failed to query follow status: %v", err) 87 107 return IsNotFollowing 88 108 } 89 109 90 - if follows && isFollowed { 110 + if userFollowsSubject > 0 && subjectFollowsUser > 0 { 91 111 return IsMutual 92 - } else if follows { 112 + } else if userFollowsSubject > 0 { 93 113 return IsFollowing 94 114 } else { 95 115 return IsNotFollowing