Monorepo for Tangled tangled.org

appview: remove `db.GetReaction()`

After refactoring record deletion logic, we only need
`db.GetReactionStatus`

Signed-off-by: Seongmin Lee <git@boltless.me>

boltless.me 16eea59b 74ae4fcc

verified
+28 -40
+19 -37
appview/db/reaction.go
··· 2 2 3 3 import ( 4 4 "fmt" 5 - "log" 6 5 "time" 7 6 8 7 "github.com/bluesky-social/indigo/atproto/syntax" ··· 24 23 reaction.Created.Format(time.RFC3339), 25 24 ) 26 25 return err 27 - } 28 - 29 - // Get a reaction record 30 - func GetReaction(e Execer, did string, subjectAt syntax.ATURI, kind models.ReactionKind) (*models.Reaction, error) { 31 - query := ` 32 - select did, subject_at, created, rkey 33 - from reactions 34 - where did = ? and subject_at = ? and kind = ?` 35 - row := e.QueryRow(query, did, subjectAt, kind) 36 - 37 - var reaction models.Reaction 38 - var created string 39 - err := row.Scan(&reaction.ReactedByDid, &reaction.ThreadAt, &created, &reaction.Rkey) 40 - if err != nil { 41 - return nil, err 42 - } 43 - 44 - createdAtTime, err := time.Parse(time.RFC3339, created) 45 - if err != nil { 46 - log.Println("unable to determine followed at time") 47 - reaction.Created = time.Now() 48 - } else { 49 - reaction.Created = createdAtTime 50 - } 51 - 52 - return &reaction, nil 53 26 } 54 27 55 28 // Remove a reaction ··· 133 106 return reactionMap, rows.Err() 134 107 } 135 108 136 - func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind models.ReactionKind) bool { 137 - if _, err := GetReaction(e, userDid, threadAt, kind); err != nil { 138 - return false 139 - } else { 140 - return true 141 - } 109 + func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind models.ReactionKind) (bool, error) { 110 + var exists bool 111 + err := e.QueryRow( 112 + `select exists ( 113 + select 1 from reactions 114 + where did = ? and subject_at = ? and kind = ? 115 + )`, 116 + userDid, 117 + threadAt, 118 + kind, 119 + ).Scan(&exists) 120 + return exists, err 142 121 } 143 122 144 - func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) map[models.ReactionKind]bool { 123 + func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) (map[models.ReactionKind]bool, error) { 145 124 statusMap := map[models.ReactionKind]bool{} 146 125 for _, kind := range models.OrderedReactionKinds { 147 - count := GetReactionStatus(e, userDid, threadAt, kind) 148 - statusMap[kind] = count 126 + reacted, err := GetReactionStatus(e, userDid, threadAt, kind) 127 + if err != nil { 128 + return nil, err 129 + } 130 + statusMap[kind] = reacted 149 131 } 150 - return statusMap 132 + return statusMap, nil 151 133 }
+4 -1
appview/issues/issues.go
··· 99 99 100 100 userReactions := map[models.ReactionKind]bool{} 101 101 if user != nil { 102 - userReactions = db.GetReactionStatusMap(rp.db, user.Active.Did, issue.AtUri()) 102 + userReactions, err = db.GetReactionStatusMap(rp.db, user.Active.Did, issue.AtUri()) 103 + if err != nil { 104 + l.Error("failed to get issue reaction status", "err", err) 105 + } 103 106 } 104 107 105 108 backlinks, err := db.GetBacklinks(rp.db, issue.AtUri())
+5 -2
appview/pulls/pulls.go
··· 227 227 228 228 reactionMap, err := db.GetReactionMap(s.db, 20, pull.AtUri()) 229 229 if err != nil { 230 - log.Println("failed to get pull reactions") 230 + s.logger.Error("failed to get pull reaction status", "err", err) 231 231 } 232 232 233 233 userReactions := map[models.ReactionKind]bool{} 234 234 if user != nil { 235 - userReactions = db.GetReactionStatusMap(s.db, user.Active.Did, pull.AtUri()) 235 + userReactions, err = db.GetReactionStatusMap(s.db, user.Active.Did, pull.AtUri()) 236 + if err != nil { 237 + s.logger.Error("failed to get pull reaction status", "err", err) 238 + } 236 239 } 237 240 238 241 labelDefs, err := db.GetLabelDefinitions(