Skip to content

Commit

Permalink
Small improvement for reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Jul 28, 2024
1 parent f6a6ec2 commit 8e46de0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
7 changes: 6 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import (
"github.com/stretchr/testify/require"
)

func createDefaultTestConfig(t *testing.T) *config {
type testConfigParam interface {
*testing.T | *testing.B
TempDir() string
}

func createDefaultTestConfig[V testConfigParam](t V) *config {
c := createDefaultConfig()
dir := t.TempDir()
c.Db.File = filepath.Join(dir, "blog.db")
Expand Down
6 changes: 1 addition & 5 deletions markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ func Benchmark_markdown(b *testing.B) {
mdExp := string(markdownExample)

app := &goBlog{
cfg: &config{
Server: &configServer{
PublicAddress: "https://example.com",
},
},
cfg: createDefaultTestConfig(b),
}

app.initMarkdown()
Expand Down
32 changes: 8 additions & 24 deletions reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import (
"errors"
"io"
"net/http"
"strings"
"time"

"github.com/dgraph-io/ristretto"
"github.com/samber/lo"
"go.goblog.app/app/pkgs/builderpool"
"go.goblog.app/app/pkgs/contenttype"
)

const reactionsCacheTTL = 6 * time.Hour

// Hardcoded for now
var allowedReactions = []string{
"❤️",
"👍",
"🎉",
"😂",
"😱",
"❤️", "👍", "🎉", "😂", "😱",
}
var allowedReactionsStr = strings.Join(allowedReactions, "")

func (a *goBlog) reactionsEnabled() bool {
return a.cfg.Reactions != nil && a.cfg.Reactions.Enabled
Expand Down Expand Up @@ -98,6 +95,10 @@ func (a *goBlog) getReactions(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, reactions)
}

const reactionsQuery = "select json_group_object(reaction, count) as json_result from (" +
"select reaction, count from reactions where path = ? and instr(?, reaction) > 0 " +
"and path not in (select path from post_parameters where parameter=? and value=?) and count > 0)"

func (a *goBlog) getReactionsFromDatabase(path string) (string, error) {
// Init
a.initReactions()
Expand All @@ -108,24 +109,7 @@ func (a *goBlog) getReactionsFromDatabase(path string) (string, error) {
}
// Get reactions
res, err, _ := a.reactionsSfg.Do(path, func() (any, error) {
// Build query
sqlBuf := builderpool.Get()
defer builderpool.Put(sqlBuf)
sqlArgs := []any{}
sqlBuf.WriteString("select json_group_object(reaction, count) as json_result from (")
sqlBuf.WriteString("select reaction, count from reactions where path=? and reaction in (")
sqlArgs = append(sqlArgs, path)
for i, reaction := range allowedReactions {
if i > 0 {
sqlBuf.WriteString(",")
}
sqlBuf.WriteString("?")
sqlArgs = append(sqlArgs, reaction)
}
sqlBuf.WriteString(") and path not in (select path from post_parameters where parameter=? and value=?) and count > 0)")
sqlArgs = append(sqlArgs, reactionsPostParam, "false")
// Execute query
row, err := a.db.QueryRow(sqlBuf.String(), sqlArgs...)
row, err := a.db.QueryRow(reactionsQuery, path, allowedReactionsStr, reactionsPostParam, "false")
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions reactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func Test_reactionsLowLevel(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
app.cfg.Reactions = &configReactions{Enabled: true}

_ = app.initConfig(false)
_ = app.initCache()
Expand Down Expand Up @@ -94,6 +95,7 @@ func Test_reactionsHighLevel(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
app.cfg.Reactions = &configReactions{Enabled: true}

_ = app.initConfig(false)
app.initMarkdown()
Expand Down

0 comments on commit 8e46de0

Please sign in to comment.