-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
common.go
60 lines (55 loc) · 1.42 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
}