-
Notifications
You must be signed in to change notification settings - Fork 234
/
views.go
145 lines (116 loc) · 2.79 KB
/
views.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gopher
import (
"fmt"
"html/template"
"net/http"
"regexp"
"sort"
"strconv"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/jimmykuu/gopher/models"
)
const (
PerPage = 20
)
var (
fileVersion map[string]string = make(map[string]string) // {path: version}
utils *Utils
usersJson []byte
)
type Utils struct {
}
func (u *Utils) UserInfo(username string, db *mgo.Database) template.HTML {
c := db.C(models.USERS)
user := models.User{}
// 检查用户名
c.Find(bson.M{"username": username}).One(&user)
format := `<div>
<a href="/member/%s"><img class="gravatar img-rounded" src="%s" class="gravatar"></a>
<h4><a href="/member/%s">%s</a><br><small>%s</small></h4>
<div class="clearfix">
</div>
</div>`
return template.HTML(fmt.Sprintf(format, username, user.AvatarImgSrc(48), username, username, user.Tagline))
}
func (u *Utils) News(username string, db *mgo.Database) template.HTML {
c := db.C(models.USERS)
user := models.User{}
//检查用户名
c.Find(bson.M{"username": username}).One(&user)
format := `<div>
<hr>
<a href="/member/%s/news#topic">新回复 <span class="badge pull-right">%d</span></a>
<br>
<a href="/member/%s/news#at">AT<span class="badge pull-right">%d</span></a>
</div>
`
return template.HTML(fmt.Sprintf(format, username, len(user.RecentReplies), username, len(user.RecentAts)))
}
func (u *Utils) AssertUser(i interface{}) *models.User {
v, _ := i.(models.User)
return &v
}
func (u *Utils) AssertNode(i interface{}) *models.Node {
v, _ := i.(models.Node)
return &v
}
func (u *Utils) AssertTopic(i interface{}) *models.Topic {
v, _ := i.(models.Topic)
return &v
}
func (u *Utils) AssertArticle(i interface{}) *models.Article {
v, _ := i.(models.Article)
return &v
}
func (u *Utils) AssertPackage(i interface{}) *models.Package {
v, _ := i.(models.Package)
return &v
}
// 获取链接的页码,默认"?p=1"这种类型
func Page(r *http.Request) (int, error) {
p := r.FormValue("p")
page := 1
if p != "" {
var err error
page, err = strconv.Atoi(p)
if err != nil {
return 0, err
}
}
return page, nil
}
// 检查一个string元素是否在数组里面
func stringInArray(a []string, x string) bool {
sort.Strings(a)
index := sort.SearchStrings(a, x)
if index == 0 {
if a[0] == x {
return true
}
return false
} else if index > len(a)-1 {
return false
}
return true
}
func getPage(r *http.Request) (page int, err error) {
p := r.FormValue("p")
page = 1
if p != "" {
page, err = strconv.Atoi(p)
if err != nil {
return
}
}
return
}
// 提取评论中被at的用户名
func findAts(content string) []string {
allAts := regexp.MustCompile(`@(\S*) `).FindAllStringSubmatch(content, -1)
var users []string
for _, v := range allAts {
users = append(users, v[1])
}
return users
}