-
-
Notifications
You must be signed in to change notification settings - Fork 638
/
common.go
87 lines (79 loc) · 2.28 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package postgres
import (
"context"
"net/url"
"regexp"
"strings"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/pkg/web"
)
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), "|")
}
// SanitizeString converts input to another string that only contains utf-8 characters and not-null characters
func SanitizeString(input string) string {
input = strings.Replace(input, "\u0000", "", -1)
return strings.ToValidUTF8(input, "")
}
func getViewData(view string) (string, []enum.PostStatus, string) {
var (
condition string
sort string
)
statuses := []enum.PostStatus{
enum.PostOpen,
enum.PostStarted,
enum.PostPlanned,
}
switch view {
case "recent":
sort = "id"
case "my-votes":
condition = "AND has_voted = true"
sort = "id"
case "most-wanted":
sort = "votes_count"
case "most-discussed":
sort = "comments_count"
case "planned":
sort = "response_date"
statuses = []enum.PostStatus{enum.PostPlanned}
case "started":
sort = "response_date"
statuses = []enum.PostStatus{enum.PostStarted}
case "completed":
sort = "response_date"
statuses = []enum.PostStatus{enum.PostCompleted}
case "declined":
sort = "response_date"
statuses = []enum.PostStatus{enum.PostDeclined}
case "all":
sort = "id"
statuses = []enum.PostStatus{
enum.PostOpen,
enum.PostStarted,
enum.PostPlanned,
enum.PostCompleted,
enum.PostDeclined,
}
case "trending":
fallthrough
default:
sort = "((COALESCE(recent_votes_count, 0)*5 + COALESCE(recent_comments_count, 0) *3)-1) / pow((EXTRACT(EPOCH FROM current_timestamp - created_at)/3600) + 2, 1.4)"
}
return condition, statuses, sort
}
func buildAvatarURL(ctx context.Context, avatarType enum.AvatarType, id int, name, avatarBlobKey string) string {
if name == "" {
name = "-"
}
if avatarType == enum.AvatarTypeCustom {
return web.AssetsURL(ctx, "/static/images/%s", avatarBlobKey)
} else {
return web.AssetsURL(ctx, "/static/avatars/%s/%d/%s", avatarType.String(), id, url.PathEscape(name))
}
}