forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview/models: move db.Reaction into models

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 9c5547d8 a56c8a2a

verified
Changed files
+86 -77
appview
db
issues
models
pages
pulls
state
+14 -63
appview/db/reaction.go
···
"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 = "👀"
+
"tangled.org/core/appview/models"
)
-
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 {
···
}
// 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
}
···
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)
···
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 {
···
}
}
-
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
}
+2 -2
appview/issues/issues.go
···
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())
}
···
RepoInfo: f.RepoInfo(user),
Issue: issue,
CommentList: issue.CommentList(),
-
OrderedReactionKinds: db.OrderedReactionKinds,
+
OrderedReactionKinds: models.OrderedReactionKinds,
Reactions: reactionCountMap,
UserReacted: userReactions,
LabelDefs: defs,
+57
appview/models/reaction.go
···
+
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
+
}
+9 -9
appview/pages/pages.go
···
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 {
···
type ThreadReactionFragmentParams struct {
ThreadAt syntax.ATURI
-
Kind db.ReactionKind
+
Kind models.ReactionKind
Count int
IsReacted bool
}
···
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 {
···
Diff *types.NiceDiff
Round int
Submission *models.PullSubmission
-
OrderedReactionKinds []db.ReactionKind
+
OrderedReactionKinds []models.ReactionKind
DiffOpts types.DiffOpts
···
Pull *models.Pull
Round int
Interdiff *patchutil.InterdiffResult
-
OrderedReactionKinds []db.ReactionKind
+
OrderedReactionKinds []models.ReactionKind
DiffOpts types.DiffOpts
+2 -2
appview/pulls/pulls.go
···
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())
}
···
ResubmitCheck: resubmitResult,
Pipelines: m,
-
OrderedReactionKinds: db.OrderedReactionKinds,
+
OrderedReactionKinds: models.OrderedReactionKinds,
Reactions: reactionCountMap,
UserReacted: userReactions,
})
+2 -1
appview/state/reaction.go
···
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"
)
···
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