Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix: remove special chars before using ts_query (#270)

Guilherme Oenning9043d8409b87
diff --git a/app/storage/postgres/common.go b/app/storage/postgres/common.go
new file mode 100644
index 000000000..1f7dc6856
--- /dev/null
+++ b/app/storage/postgres/common.go
@@ -0,0 +1,60 @@
+package postgres
+
+import (
+ "regexp"
+ "strings"
+
+ "github.com/getfider/fider/app/models"
+)
+
+var onlyalphanumeric = regexp.MustCompile("[^a-zA-Z0-9 |]+")
+var replaceOr = strings.NewReplacer("|", " ")
+
+// ToTSQuery converts input to another string that can be safely used for ts_query
+func ToTSQuery(input string) string {
+ input = replaceOr.Replace(onlyalphanumeric.ReplaceAllString(input, ""))
+ return strings.Join(strings.Fields(input), "|")
+}
+
+func getFilterData(filter string) ([]int, string) {
+ var sort string
+ statuses := []int{
+ models.IdeaOpen,
+ models.IdeaStarted,
+ models.IdeaPlanned,
+ }
+ switch filter {
+ case "recent":
+ sort = "id"
+ case "most-wanted":
+ sort = "supporters"
+ case "most-discussed":
+ sort = "comments"
+ case "planned":
+ sort = "response_date"
+ statuses = []int{models.IdeaPlanned}
+ case "started":
+ sort = "response_date"
+ statuses = []int{models.IdeaStarted}
+ case "completed":
+ sort = "response_date"
+ statuses = []int{models.IdeaCompleted}
+ case "declined":
+ sort = "response_date"
+ statuses = []int{models.IdeaDeclined}
+ case "all":
+ sort = "id"
+ statuses = []int{
+ models.IdeaOpen,
+ models.IdeaStarted,
+ models.IdeaPlanned,
+ models.IdeaCompleted,
+ models.IdeaDeclined,
+ }
+ case "trending":
+ fallthrough
+ default:
+ sort = "((COALESCE(recent_supporters, 0)*5 + COALESCE(recent_comments, 0) *3)-1) / pow((EXTRACT(EPOCH FROM current_timestamp - created_on)/3600) + 2, 1.4)"
+ }
+ return statuses, sort
+}
diff --git a/app/storage/postgres/common_test.go b/app/storage/postgres/common_test.go
new file mode 100644
index 000000000..313bebeb6
--- /dev/null
+++ b/app/storage/postgres/common_test.go
@@ -0,0 +1,33 @@
+package postgres_test
+
+import (
+ "testing"
+
+ "github.com/getfider/fider/app/storage/postgres"
+ . "github.com/onsi/gomega"
+)
+
+func TestToTSQuery(t *testing.T) {
+ RegisterTestingT(t)
+
+ var testcases = []struct {
+ input string
+ expected string
+ }{
+ {"", ""},
+ {"123 hello", "123|hello"},
+ {" hello ", "hello"},
+ {" hello$ world$ ", "hello|world"},
+ {" yes, please ", "yes|please"},
+ {" yes / please ", "yes|please"},
+ {" hello 'world' ", "hello|world"},
+ {"hello|world", "hello|world"},
+ {"hello | world", "hello|world"},
+ {"hello & world", "hello|world"},
+ }
+
+ for _, testcase := range testcases {
+ output := postgres.ToTSQuery(testcase.input)
+ Expect(output).To(Equal(testcase.expected))
+ }
+}
diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.go
index b79afbc21..16a732a05 100644
--- a/app/storage/postgres/idea.go
+++ b/app/storage/postgres/idea.go
@@ -3,7 +3,6 @@ package postgres
import (
"errors"
"fmt"
- "strings"
"time"
"database/sql"
@@ -244,49 +243,6 @@ func (s *IdeaStorage) CountPerStatus() (map[int]int, error) {
return result, nil
}
-func getFilterData(filter string) ([]int, string) {
- var sort string
- statuses := []int{
- models.IdeaOpen,
- models.IdeaStarted,
- models.IdeaPlanned,
- }
- switch filter {
- case "recent":
- sort = "id"
- case "most-wanted":
- sort = "supporters"
- case "most-discussed":
- sort = "comments"
- case "planned":
- sort = "response_date"
- statuses = []int{models.IdeaPlanned}
- case "started":
- sort = "response_date"
- statuses = []int{models.IdeaStarted}
- case "completed":
- sort = "response_date"
- statuses = []int{models.IdeaCompleted}
- case "declined":
- sort = "response_date"
- statuses = []int{models.IdeaDeclined}
- case "all":
- sort = "id"
- statuses = []int{
- models.IdeaOpen,
- models.IdeaStarted,
- models.IdeaPlanned,
- models.IdeaCompleted,
- models.IdeaDeclined,
- }
- case "trending":
- fallthrough
- default:
- sort = "((COALESCE(recent_supporters, 0)*5 + COALESCE(recent_comments, 0) *3)-1) / pow((EXTRACT(EPOCH FROM current_timestamp - created_on)/3600) + 2, 1.4)"
- }
- return statuses, sort
-}
-
// Search existing ideas based on input
func (s *IdeaStorage) Search(query, filter string, tags []string) ([]*models.Idea, error) {
innerQuery := s.getIdeaQuery("i.tenant_id = $1 AND i.status = ANY($2)")
@@ -303,14 +259,13 @@ func (s *IdeaStorage) Search(query, filter string, tags []string) ([]*models.Ide
AND tags @> $3
ORDER BY %s DESC
`, innerQuery, scoreField, scoreField)
- tsQuery := strings.Join(strings.Fields(query), "|")
err = s.trx.Select(&ideas, sql, s.tenant.ID, pq.Array([]int{
models.IdeaOpen,
models.IdeaStarted,
models.IdeaPlanned,
models.IdeaCompleted,
models.IdeaDeclined,
- }), pq.Array(tags), tsQuery, query)
+ }), pq.Array(tags), ToTSQuery(query), query)
} else {
statuses, sort := getFilterData(filter)
sql := fmt.Sprintf(`
diff --git a/public/pages/site/HomePage/IdeasView.tsx b/public/pages/site/HomePage/IdeasView.tsx
index c6af105fc..d79fbad5b 100644
--- a/public/pages/site/HomePage/IdeasView.tsx
+++ b/public/pages/site/HomePage/IdeasView.tsx
@@ -44,7 +44,7 @@ export class IdeasView extends React.Component<IdeasViewProps, IdeasViewState> {
} else if (this.state.query || this.state.filter || this.state.tags.length > 0) {
this.searchIdeas(this.state.query, this.state.filter, this.state.tags);
} else {
- this.setState({ ideas: nextProps.ideas });
+ this.setState({ loading: false, ideas: nextProps.ideas });
}
}
@@ -66,7 +66,9 @@ export class IdeasView extends React.Component<IdeasViewProps, IdeasViewState> {
this.setState({ loading: true });
this.timer = setTimeout(() => {
actions.searchIdeas(query, filter, tags).then((response) => {
- this.setState({ loading: false, ideas: response.data });
+ if (this.state.loading) {
+ this.setState({ loading: false, ideas: response.data });
+ }
});
}, delay);
}