diff --git a/appview/db/reaction.go b/appview/db/reaction.go index 96db0901..50831528 100644 --- a/appview/db/reaction.go +++ b/appview/db/reaction.go @@ -5,73 +5,24 @@ import ( "time" "github.com/bluesky-social/indigo/atproto/syntax" + "tangled.org/core/appview/models" ) -type ReactionKind string - -const ( - Like ReactionKind = "👍" - Unlike ReactionKind = "👎" - Laugh ReactionKind = "😆" - Celebration ReactionKind = "🎉" - Confused ReactionKind = "🫤" - Heart ReactionKind = "❤️" - Rocket ReactionKind = "🚀" - Eyes ReactionKind = "👀" -) - -func (rk ReactionKind) String() string { - return string(rk) -} - -var OrderedReactionKinds = []ReactionKind{ - Like, - Unlike, - Laugh, - Celebration, - Confused, - Heart, - Rocket, - Eyes, -} - -func ParseReactionKind(raw string) (ReactionKind, bool) { - k, ok := (map[string]ReactionKind{ - "👍": Like, - "👎": Unlike, - "😆": Laugh, - "🎉": Celebration, - "🫤": Confused, - "❤️": Heart, - "🚀": Rocket, - "👀": Eyes, - })[raw] - return k, ok -} - -type Reaction struct { - ReactedByDid string - ThreadAt syntax.ATURI - Created time.Time - Rkey string - Kind ReactionKind -} - -func AddReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind ReactionKind, rkey string) error { +func AddReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind, rkey string) error { query := `insert or ignore into reactions (reacted_by_did, thread_at, kind, rkey) values (?, ?, ?, ?)` _, err := e.Exec(query, reactedByDid, threadAt, kind, rkey) return err } // Get a reaction record -func GetReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind ReactionKind) (*Reaction, error) { +func GetReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind) (*models.Reaction, error) { query := ` select reacted_by_did, thread_at, created, rkey from reactions where reacted_by_did = ? and thread_at = ? and kind = ?` row := e.QueryRow(query, reactedByDid, threadAt, kind) - var reaction Reaction + var reaction models.Reaction var created string err := row.Scan(&reaction.ReactedByDid, &reaction.ThreadAt, &created, &reaction.Rkey) if err != nil { @@ -90,7 +41,7 @@ func GetReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind Reac } // Remove a reaction -func DeleteReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind ReactionKind) error { +func DeleteReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind) error { _, err := e.Exec(`delete from reactions where reacted_by_did = ? and thread_at = ? and kind = ?`, reactedByDid, threadAt, kind) return err } @@ -101,7 +52,7 @@ func DeleteReactionByRkey(e Execer, reactedByDid string, rkey string) error { return err } -func GetReactionCount(e Execer, threadAt syntax.ATURI, kind ReactionKind) (int, error) { +func GetReactionCount(e Execer, threadAt syntax.ATURI, kind models.ReactionKind) (int, error) { count := 0 err := e.QueryRow( `select count(reacted_by_did) from reactions where thread_at = ? and kind = ?`, threadAt, kind).Scan(&count) @@ -111,19 +62,19 @@ func GetReactionCount(e Execer, threadAt syntax.ATURI, kind ReactionKind) (int, return count, nil } -func GetReactionCountMap(e Execer, threadAt syntax.ATURI) (map[ReactionKind]int, error) { - countMap := map[ReactionKind]int{} - for _, kind := range OrderedReactionKinds { +func GetReactionCountMap(e Execer, threadAt syntax.ATURI) (map[models.ReactionKind]int, error) { + countMap := map[models.ReactionKind]int{} + for _, kind := range models.OrderedReactionKinds { count, err := GetReactionCount(e, threadAt, kind) if err != nil { - return map[ReactionKind]int{}, nil + return map[models.ReactionKind]int{}, nil } countMap[kind] = count } return countMap, nil } -func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind ReactionKind) bool { +func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind models.ReactionKind) bool { if _, err := GetReaction(e, userDid, threadAt, kind); err != nil { return false } else { @@ -131,9 +82,9 @@ func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind Rea } } -func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) map[ReactionKind]bool { - statusMap := map[ReactionKind]bool{} - for _, kind := range OrderedReactionKinds { +func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) map[models.ReactionKind]bool { + statusMap := map[models.ReactionKind]bool{} + for _, kind := range models.OrderedReactionKinds { count := GetReactionStatus(e, userDid, threadAt, kind) statusMap[kind] = count } diff --git a/appview/issues/issues.go b/appview/issues/issues.go index 1ee936e3..87c301e3 100644 --- a/appview/issues/issues.go +++ b/appview/issues/issues.go @@ -88,7 +88,7 @@ func (rp *Issues) RepoSingleIssue(w http.ResponseWriter, r *http.Request) { l.Error("failed to get issue reactions", "err", err) } - userReactions := map[db.ReactionKind]bool{} + userReactions := map[models.ReactionKind]bool{} if user != nil { userReactions = db.GetReactionStatusMap(rp.db, user.Did, issue.AtUri()) } @@ -114,7 +114,7 @@ func (rp *Issues) RepoSingleIssue(w http.ResponseWriter, r *http.Request) { RepoInfo: f.RepoInfo(user), Issue: issue, CommentList: issue.CommentList(), - OrderedReactionKinds: db.OrderedReactionKinds, + OrderedReactionKinds: models.OrderedReactionKinds, Reactions: reactionCountMap, UserReacted: userReactions, LabelDefs: defs, diff --git a/appview/models/reaction.go b/appview/models/reaction.go new file mode 100644 index 00000000..c1898076 --- /dev/null +++ b/appview/models/reaction.go @@ -0,0 +1,57 @@ +package models + +import ( + "time" + + "github.com/bluesky-social/indigo/atproto/syntax" +) + +type ReactionKind string + +const ( + Like ReactionKind = "👍" + Unlike ReactionKind = "👎" + Laugh ReactionKind = "😆" + Celebration ReactionKind = "🎉" + Confused ReactionKind = "🫤" + Heart ReactionKind = "❤️" + Rocket ReactionKind = "🚀" + Eyes ReactionKind = "👀" +) + +func (rk ReactionKind) String() string { + return string(rk) +} + +var OrderedReactionKinds = []ReactionKind{ + Like, + Unlike, + Laugh, + Celebration, + Confused, + Heart, + Rocket, + Eyes, +} + +func ParseReactionKind(raw string) (ReactionKind, bool) { + k, ok := (map[string]ReactionKind{ + "👍": Like, + "👎": Unlike, + "😆": Laugh, + "🎉": Celebration, + "🫤": Confused, + "❤️": Heart, + "🚀": Rocket, + "👀": Eyes, + })[raw] + return k, ok +} + +type Reaction struct { + ReactedByDid string + ThreadAt syntax.ATURI + Created time.Time + Rkey string + Kind ReactionKind +} diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 8677e4d1..d5040440 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -908,9 +908,9 @@ type RepoSingleIssueParams struct { CommentList []models.CommentListItem LabelDefs map[string]*models.LabelDefinition - OrderedReactionKinds []db.ReactionKind - Reactions map[db.ReactionKind]int - UserReacted map[db.ReactionKind]bool + OrderedReactionKinds []models.ReactionKind + Reactions map[models.ReactionKind]int + UserReacted map[models.ReactionKind]bool } func (p *Pages) RepoSingleIssue(w io.Writer, params RepoSingleIssueParams) error { @@ -932,7 +932,7 @@ func (p *Pages) EditIssueFragment(w io.Writer, params EditIssueParams) error { type ThreadReactionFragmentParams struct { ThreadAt syntax.ATURI - Kind db.ReactionKind + Kind models.ReactionKind Count int IsReacted bool } @@ -1060,9 +1060,9 @@ type RepoSinglePullParams struct { ResubmitCheck ResubmitResult Pipelines map[string]models.Pipeline - OrderedReactionKinds []db.ReactionKind - Reactions map[db.ReactionKind]int - UserReacted map[db.ReactionKind]bool + OrderedReactionKinds []models.ReactionKind + Reactions map[models.ReactionKind]int + UserReacted map[models.ReactionKind]bool } func (p *Pages) RepoSinglePull(w io.Writer, params RepoSinglePullParams) error { @@ -1078,7 +1078,7 @@ type RepoPullPatchParams struct { Diff *types.NiceDiff Round int Submission *models.PullSubmission - OrderedReactionKinds []db.ReactionKind + OrderedReactionKinds []models.ReactionKind DiffOpts types.DiffOpts } @@ -1093,7 +1093,7 @@ type RepoPullInterdiffParams struct { Pull *models.Pull Round int Interdiff *patchutil.InterdiffResult - OrderedReactionKinds []db.ReactionKind + OrderedReactionKinds []models.ReactionKind DiffOpts types.DiffOpts } diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index 936f1a74..72b5c3f0 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -195,7 +195,7 @@ func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) { s.pages.Notice(w, "pulls", "Failed to load pull. Try again later.") } - userReactions := map[db.ReactionKind]bool{} + userReactions := map[models.ReactionKind]bool{} if user != nil { userReactions = db.GetReactionStatusMap(s.db, user.Did, pull.PullAt()) } @@ -210,7 +210,7 @@ func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) { ResubmitCheck: resubmitResult, Pipelines: m, - OrderedReactionKinds: db.OrderedReactionKinds, + OrderedReactionKinds: models.OrderedReactionKinds, Reactions: reactionCountMap, UserReacted: userReactions, }) diff --git a/appview/state/reaction.go b/appview/state/reaction.go index 03caac7a..7443b8f4 100644 --- a/appview/state/reaction.go +++ b/appview/state/reaction.go @@ -11,6 +11,7 @@ import ( lexutil "github.com/bluesky-social/indigo/lex/util" "tangled.org/core/api/tangled" "tangled.org/core/appview/db" + "tangled.org/core/appview/models" "tangled.org/core/appview/pages" "tangled.org/core/tid" ) @@ -30,7 +31,7 @@ func (s *State) React(w http.ResponseWriter, r *http.Request) { return } - reactionKind, ok := db.ParseReactionKind(r.URL.Query().Get("kind")) + reactionKind, ok := models.ParseReactionKind(r.URL.Query().Get("kind")) if !ok { log.Println("invalid reaction kind") return