forked from jimmykuu/gopher
/
article.go
278 lines (216 loc) · 7.26 KB
/
article.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"./wtforms"
"github.com/gorilla/mux"
"html/template"
"labix.org/v2/mgo/bson"
"net/http"
"strconv"
"strings"
"time"
)
// URL: /article/new
// 新建文章
func newArticleHandler(w http.ResponseWriter, r *http.Request) {
if _, ok := currentUser(r); !ok {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
var categories []ArticleCategory
c := db.C("articlecategories")
c.Find(nil).All(&categories)
var choices []wtforms.Choice
for _, category := range categories {
choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
}
form := wtforms.NewForm(
wtforms.NewHiddenField("html", ""),
wtforms.NewTextField("title", "标题", "", wtforms.Required{}),
wtforms.NewTextArea("content", "内容", "", wtforms.Required{}),
wtforms.NewTextField("original_source", "原始出处", "", wtforms.Required{}),
wtforms.NewTextField("original_url", "原始链接", "", wtforms.URL{}),
wtforms.NewSelectField("category", "分类", choices, ""),
)
if r.Method == "POST" && form.Validate(r) {
session, _ := store.Get(r, "user")
username, _ := session.Values["username"]
username = username.(string)
user := User{}
c = db.C("users")
c.Find(bson.M{"username": username}).One(&user)
c = db.C("articles")
Id_ := bson.NewObjectId()
html := form.Value("html")
html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)
categoryId := bson.ObjectIdHex(form.Value("category"))
err := c.Insert(&Article{
Id_: Id_,
CategoryId: categoryId,
UserId: user.Id_,
Title: form.Value("title"),
Markdown: form.Value("content"),
Html: template.HTML(html),
OriginalSource: form.Value("original_source"),
OriginalUrl: form.Value("original_url"),
CreatedAt: time.Now(),
})
if err != nil {
panic(err)
}
http.Redirect(w, r, "/a/"+Id_.Hex(), http.StatusFound)
return
}
renderTemplate(w, r, "article/form.html", map[string]interface{}{"form": form, "title": "新建", "action": "/article/new"})
}
// URL: /articles
// 列出所有文章
func listArticlesHandler(w http.ResponseWriter, r *http.Request) {
p := r.FormValue("p")
page := 1
if p != "" {
var err error
page, err = strconv.Atoi(p)
if err != nil {
message(w, r, "页码错误", "页码错误", "error")
return
}
}
// var hotNodes []Node
// c := db.C("nodes")
// c.Find(bson.M{"topiccount": bson.M{"$gt": 0}}).Sort("-topiccount").Limit(10).All(&hotNodes)
// var status Status
// c = db.C("status")
// c.Find(nil).One(&status)
c := db.C("articles")
pagination := NewPagination(c.Find(nil).Sort("-createdat"), "/", PerPage)
var articles []Article
query, err := pagination.Page(page)
if err != nil {
message(w, r, "页码错误", "页码错误", "error")
return
}
query.All(&articles)
renderTemplate(w, r, "article/index.html", map[string]interface{}{"articles": articles, "pagination": pagination, "page": page})
}
// URL: /a/{articl_id}
// 显示文章
func showArticleHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
articleId := vars["articleId"]
c := db.C("articles")
article := Article{}
err := c.Find(bson.M{"_id": bson.ObjectIdHex(articleId)}).One(&article)
if err != nil {
println("err")
}
c = db.C("articles")
c.UpdateId(bson.ObjectIdHex(articleId), bson.M{"$inc": bson.M{"hits": 1}})
renderTemplate(w, r, "article/show.html", map[string]interface{}{"article": article})
}
// URL: /a/{articleId}/edit
// 编辑主题
func editArticleHandler(w http.ResponseWriter, r *http.Request) {
user, ok := currentUser(r)
if !ok {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
articleId := mux.Vars(r)["articleId"]
c := db.C("articles")
var article Article
err := c.Find(bson.M{"_id": bson.ObjectIdHex(articleId)}).One(&article)
if err != nil {
message(w, r, "没有该文章", "没有该文章,不能编辑", "error")
return
}
if !article.CanEdit(user.Username) {
message(w, r, "没用该权限", "对不起,你没有权限编辑该文章", "error")
return
}
var categorys []ArticleCategory
c = db.C("articlecategories")
c.Find(nil).All(&categorys)
var choices []wtforms.Choice
for _, category := range categorys {
choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
}
form := wtforms.NewForm(
wtforms.NewHiddenField("html", ""),
wtforms.NewTextField("title", "标题", article.Title, wtforms.Required{}),
wtforms.NewTextArea("content", "内容", article.Markdown, wtforms.Required{}),
wtforms.NewTextField("original_source", "原始出处", article.OriginalSource, wtforms.Required{}),
wtforms.NewTextField("original_url", "原始链接", article.OriginalUrl, wtforms.URL{}),
wtforms.NewSelectField("category", "分类", choices, article.CategoryId.Hex()),
)
content := article.Markdown
html := article.Html
if r.Method == "POST" {
if form.Validate(r) {
html := form.Value("html")
html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)
categoryId := bson.ObjectIdHex(form.Value("category"))
c = db.C("articles")
c.Update(bson.M{"_id": article.Id_}, bson.M{"$set": bson.M{
"categoryid": categoryId,
"title": form.Value("title"),
"originalsource": form.Value("original_source"),
"originalurl": form.Value("original_url"),
"markdown": form.Value("content"),
"html": template.HTML(html),
}})
http.Redirect(w, r, "/a/"+article.Id_.Hex(), http.StatusFound)
return
}
content = form.Value("content")
html = template.HTML(form.Value("html"))
}
renderTemplate(w, r, "article/form.html", map[string]interface{}{"form": form, "title": "编辑", "action": "/a/" + articleId + "/edit", "html": html, "content": content})
}
// URL: /a/{articleId}/comment
// 评论文章
func commentAnArticleHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
vars := mux.Vars(r)
articleId := vars["articleId"]
user, ok := currentUser(r)
if !ok {
http.Redirect(w, r, "/a/"+articleId, http.StatusFound)
return
}
content := r.FormValue("content")
html := r.FormValue("html")
html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)
Id_ := bson.NewObjectId()
now := time.Now()
comment := Comment{
Id_: Id_,
UserId: user.Id_,
Markdown: content,
Html: template.HTML(html),
CreatedAt: now,
}
c := db.C("articles")
c.Update(bson.M{"_id": bson.ObjectIdHex(articleId)}, bson.M{"$addToSet": bson.M{"comments": comment}})
http.Redirect(w, r, "/a/"+articleId, http.StatusFound)
}
}
// URL: /a/{articleId}/comment/{commentId}/delete
// 删除文章评论
func deleteArticleCommentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
articleId := vars["articleId"]
user, ok := currentUser(r)
if !ok {
http.Redirect(w, r, "/a/"+articleId, http.StatusFound)
return
}
if !user.IsSuperuser {
message(w, r, "没用该权限", "对不起,你没有权限删除该评论", "error")
return
}
var commentId string = vars["commentId"]
c := db.C("articles")
c.Update(bson.M{"_id": bson.ObjectIdHex(articleId)},
bson.M{"$pull": bson.M{"comments": bson.M{"_id": bson.ObjectIdHex(commentId)}}})
http.Redirect(w, r, "/a/"+articleId, http.StatusFound)
}