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 3 import ( 4 "fmt" 5 - "log" 6 "time" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" ··· 26 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 - } 54 - 55 // Remove a reaction 56 func DeleteReaction(e Execer, did syntax.DID, subjectAt syntax.ATURI, kind models.ReactionKind) ([]syntax.ATURI, error) { 57 var deleted []syntax.ATURI ··· 133 return reactionMap, rows.Err() 134 } 135 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 - } 142 } 143 144 - func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) map[models.ReactionKind]bool { 145 statusMap := map[models.ReactionKind]bool{} 146 for _, kind := range models.OrderedReactionKinds { 147 - count := GetReactionStatus(e, userDid, threadAt, kind) 148 - statusMap[kind] = count 149 } 150 - return statusMap 151 }
··· 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/bluesky-social/indigo/atproto/syntax" ··· 25 return err 26 } 27 28 // Remove a reaction 29 func DeleteReaction(e Execer, did syntax.DID, subjectAt syntax.ATURI, kind models.ReactionKind) ([]syntax.ATURI, error) { 30 var deleted []syntax.ATURI ··· 106 return reactionMap, rows.Err() 107 } 108 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 121 } 122 123 + func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) (map[models.ReactionKind]bool, error) { 124 statusMap := map[models.ReactionKind]bool{} 125 for _, kind := range models.OrderedReactionKinds { 126 + reacted, err := GetReactionStatus(e, userDid, threadAt, kind) 127 + if err != nil { 128 + return nil, err 129 + } 130 + statusMap[kind] = reacted 131 } 132 + return statusMap, nil 133 }
+4 -1
appview/issues/issues.go
··· 98 99 userReactions := map[models.ReactionKind]bool{} 100 if user != nil { 101 - userReactions = db.GetReactionStatusMap(rp.db, user.Active.Did, issue.AtUri()) 102 } 103 104 backlinks, err := db.GetBacklinks(rp.db, issue.AtUri())
··· 98 99 userReactions := map[models.ReactionKind]bool{} 100 if user != nil { 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 + } 105 } 106 107 backlinks, err := db.GetBacklinks(rp.db, issue.AtUri())
+5 -2
appview/pulls/pulls.go
··· 223 224 reactionMap, err := db.GetReactionMap(s.db, 20, pull.AtUri()) 225 if err != nil { 226 - log.Println("failed to get pull reactions") 227 s.pages.Notice(w, "pulls", "Failed to load pull. Try again later.") 228 } 229 230 userReactions := map[models.ReactionKind]bool{} 231 if user != nil { 232 - userReactions = db.GetReactionStatusMap(s.db, user.Active.Did, pull.AtUri()) 233 } 234 235 labelDefs, err := db.GetLabelDefinitions(
··· 223 224 reactionMap, err := db.GetReactionMap(s.db, 20, pull.AtUri()) 225 if err != nil { 226 + s.logger.Error("failed to get pull reaction status", "err", err) 227 s.pages.Notice(w, "pulls", "Failed to load pull. Try again later.") 228 } 229 230 userReactions := map[models.ReactionKind]bool{} 231 if user != nil { 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 + } 236 } 237 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