Monorepo for Tangled tangled.org

appview: remove db.GetReaction() #1000

open opened by boltless.me targeting master from sl/sqkrqopzkvoo

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

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

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:xasnlahkri4ewmbuzly2rlc5/sh.tangled.repo.pull/3mcsfwwhu3h22
+28 -40
Diff #0
+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" ··· 26 25 return err 27 26 } 28 27 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 - } 54 - 55 28 // Remove a reaction 56 29 func DeleteReaction(e Execer, did syntax.DID, subjectAt syntax.ATURI, kind models.ReactionKind) ([]syntax.ATURI, error) { 57 30 var deleted []syntax.ATURI ··· 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
··· 98 98 99 99 userReactions := map[models.ReactionKind]bool{} 100 100 if user != nil { 101 - userReactions = db.GetReactionStatusMap(rp.db, user.Active.Did, issue.AtUri()) 101 + userReactions, err = db.GetReactionStatusMap(rp.db, user.Active.Did, issue.AtUri()) 102 + if err != nil { 103 + l.Error("failed to get issue reaction status", "err", err) 104 + } 102 105 } 103 106 104 107 backlinks, err := db.GetBacklinks(rp.db, issue.AtUri())
+5 -2
appview/pulls/pulls.go
··· 223 223 224 224 reactionMap, err := db.GetReactionMap(s.db, 20, pull.AtUri()) 225 225 if err != nil { 226 - log.Println("failed to get pull reactions") 226 + s.logger.Error("failed to get pull reaction status", "err", err) 227 227 s.pages.Notice(w, "pulls", "Failed to load pull. Try again later.") 228 228 } 229 229 230 230 userReactions := map[models.ReactionKind]bool{} 231 231 if user != nil { 232 - userReactions = db.GetReactionStatusMap(s.db, user.Active.Did, pull.AtUri()) 232 + userReactions, err = db.GetReactionStatusMap(s.db, user.Active.Did, pull.AtUri()) 233 + if err != nil { 234 + s.logger.Error("failed to get pull reaction status", "err", err) 235 + } 233 236 } 234 237 235 238 labelDefs, err := db.GetLabelDefinitions(

Submissions

sign up or login to add to the discussion
boltless.me submitted #0
1 commit
expand
appview: remove db.GetReaction()
3/3 success
expand
merge conflicts detected
expand
  • appview/pulls/pulls.go:223