forked from philsong/ohlala
/
link.go
343 lines (302 loc) · 8.82 KB
/
link.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package controllers
import (
"bytes"
"fmt"
"github.com/QLeelulu/goku"
"github.com/philsong/ohlala/golink"
"github.com/philsong/ohlala/golink/filters"
"github.com/philsong/ohlala/golink/forms"
"github.com/philsong/ohlala/golink/models"
"github.com/philsong/ohlala/golink/utils"
"html/template"
"net/url"
"strconv"
"strings"
"time"
)
var _ = goku.Controller("link").
/**
* 查看某评论
*/
Get("permacoment", link_permacoment).
/**
* 查看一个链接的评论
*/
Get("show", link_show).
/**
* 删除link
*/
Post("ajax-del", link_ajaxDel).Filters(filters.NewRequireLoginFilter(), filters.NewAjaxFilter()).
/**
* 提交一个链接并保存到数据库
*/
Post("submit", link_submit).Filters(filters.NewRequireLoginFilter()).
/**
* 提交评论并保存到数据库
*/
Post("ajax-comment", link_ajax_comment).Filters(filters.NewRequireLoginFilter(), filters.NewAjaxFilter()).
/**
* 提交评论并保存到数据库
*/
Post("inc-click", link_incClick).Filters(filters.NewAjaxFilter()).
/**
* 提交链接的表单页面
*/
Get("submit", func(ctx *goku.HttpContext) goku.ActionResulter {
ctx.ViewData["Values"] = map[string]string{
"title": ctx.Get("t"),
"context": ctx.Get("u"),
}
return ctx.View(nil)
}).Filters(filters.NewRequireLoginFilter()).
/**
* 提交链接的表单页面
*/
Get("search", link_search).
Get("searchmorelink", link_search_loadMore).
Filters(filters.NewAjaxFilter())
//
/**
* 提交评论并保存到数据库
*/
func link_ajax_comment(ctx *goku.HttpContext) goku.ActionResulter {
f := forms.NewCommentSubmitForm()
f.FillByRequest(ctx.Request)
var success bool
var errorMsgs, commentHTML string
var commentId int64
if ctx.RouteData.Params["id"] != f.Values()["link_id"] {
errorMsgs = "参数错误"
} else {
var errors []string
user := ctx.Data["user"].(*models.User)
success, commentId, errors = models.Comment_SaveForm(f, user.Id)
if errors != nil {
errorMsgs = strings.Join(errors, "\n")
} else {
linkId, _ := strconv.ParseInt(ctx.RouteData.Params["id"], 10, 64)
m := f.CleanValues()
cn := models.CommentNode{}
cn.Id = commentId
cn.LinkId = linkId
cn.UserId = user.Id
cn.Status = 1
cn.Content = m["content"].(string)
cn.ParentId = m["parent_id"].(int64)
cn.ChildrenCount = 0
cn.VoteUp = 0
cn.CreateTime = time.Now()
cn.UserName = user.Name
sortType := ""
var b *bytes.Buffer = new(bytes.Buffer)
cn.RenderSelfOnly(b, sortType)
commentHTML = b.String()
//models.GetPermalinkComment(linkId, commentId, "")
}
}
r := map[string]interface{}{
"success": success,
"errors": errorMsgs,
"commentHTML": commentHTML,
}
return ctx.Json(r)
}
// 增加链接的点击统计数
func link_incClick(ctx *goku.HttpContext) goku.ActionResulter {
var success bool
var errorMsgs string
id := ctx.Get("id")
if id == "" {
errorMsgs = "参数错误"
} else {
linkId, err := strconv.ParseInt(id, 10, 64)
if err == nil && linkId > 0 {
_, err = models.Link_IncClickCount(linkId, 1)
if err == nil {
success = true
}
}
if err != nil {
goku.Logger().Error(err.Error())
errorMsgs = err.Error()
}
}
r := map[string]interface{}{
"success": success,
"errors": errorMsgs,
}
return ctx.Json(r)
}
/**
* 提交一个链接并保存到数据库
*/
func link_submit(ctx *goku.HttpContext) goku.ActionResulter {
f := forms.CreateLinkSubmitForm()
f.FillByRequest(ctx.Request)
var resubmit bool
if ctx.Get("resubmit") == "true" {
resubmit = true
}
user := ctx.Data["user"].(*models.User)
success, linkId, errorMsgs, _ := models.Link_SaveForm(f, user.Id, resubmit)
if success {
go addLinkForSearch(0, f.CleanValues(), linkId, user.Name) //contextType:0: url, 1:文本 TODO:
return ctx.Redirect(fmt.Sprintf("/link/%d", linkId))
} else if linkId > 0 {
return ctx.Redirect(fmt.Sprintf("/link/%d?already_submitted=true", linkId))
} else {
ctx.ViewData["Errors"] = errorMsgs
ctx.ViewData["Values"] = f.Values()
}
return ctx.View(nil)
}
//添加link到es搜索; contextType:0: url, 1:文本
func addLinkForSearch(contextType int, m map[string]interface{}, linkId int64, userName string) {
m["id"] = linkId
m["username"] = userName
if contextType == 0 {
m["host"] = utils.GetUrlHost(m["context"].(string))
m["context"] = ""
} else {
m["host"] = ""
}
ls := utils.LinkSearch{}
ls.AddLink(m)
}
// 删除link
func link_ajaxDel(ctx *goku.HttpContext) goku.ActionResulter {
var errs string
var ok = false
linkId, err := strconv.ParseInt(ctx.RouteData.Params["id"], 10, 64)
if err == nil {
user := ctx.Data["user"].(*models.User)
link, err := models.Link_GetById(linkId)
if err == nil {
// 只可以删除自己的链接
if link.UserId == user.Id {
err = models.Link_DelById(linkId)
if err == nil {
ok = true
}
} else {
errs = "不允许的操作"
}
}
}
if err != nil {
errs = err.Error()
}
r := map[string]interface{}{
"success": ok,
"errors": errs,
}
return ctx.Json(r)
}
// 查看link
func link_show(ctx *goku.HttpContext) goku.ActionResulter {
if ctx.Get("already_submitted") == "true" {
ctx.ViewData["AlreadySubmitted"] = true
}
return link_showWithComments(ctx, ctx.RouteData.Params["id"], "0")
}
func link_permacoment(ctx *goku.HttpContext) goku.ActionResulter {
return link_showWithComments(ctx,
ctx.RouteData.Params["id"], ctx.RouteData.Params["cid"])
}
var ORDER_NAMES map[string]string = map[string]string{
"hot": "最佳",
"hotc": "热议",
"time": "最新",
"vote": "得分",
"ctvl": "争议",
}
func link_showWithComments(ctx *goku.HttpContext, slinkId, scommentId string) goku.ActionResulter {
linkId, err1 := strconv.ParseInt(slinkId, 10, 64)
commentId, err2 := strconv.ParseInt(scommentId, 10, 64)
if err1 != nil || err2 != nil {
ctx.ViewData["errorMsg"] = "参数错误"
return ctx.Render("error", nil)
}
link, err := models.Link_GetById(linkId)
if err != nil {
ctx.ViewData["errorMsg"] = "服务器开小差了 >_<!!"
return ctx.Render("error", nil)
}
if link == nil {
ctx.ViewData["errorMsg"] = "内容不存在,去首页逛逛吧"
return ctx.Render("error", nil)
}
if link.Deleted() {
ctx.ViewData["errorMsg"] = "内容已被摧毁,去首页逛逛吧"
return ctx.Render("error", nil)
}
if !utils.IsSpider(ctx.Request.UserAgent()) {
// 更新链接的评论查看计数
models.Link_IncViewCount(link.Id, 1)
}
vlink := models.Link_ToVLink([]models.Link{*link}, ctx)
sortType := strings.ToLower(ctx.Get("cm_order")) //"hot":热门;"hotc":热议;"time":最新;"vote":得分;"ctvl":"争议"
if sortType == "" {
sortType = "hot"
}
var comments string
if commentId > 0 {
comments = models.GetPermalinkComment(linkId, commentId, sortType)
ctx.ViewData["SubLinkUrl"] = fmt.Sprintf("permacoment/%d/%d/", linkId, commentId)
} else {
comments = models.GetSortComments("", "/", int64(0), linkId, sortType, "", false) //models.Comment_SortForLink(link.Id, "hot")
ctx.ViewData["SubLinkUrl"] = linkId
}
ctx.ViewData["Comments"] = template.HTML(comments)
ctx.ViewData["SortType"] = sortType
ctx.ViewData["SortTypeName"] = ORDER_NAMES[sortType]
return ctx.Render("/link/show", vlink[0])
}
//link搜索界面
func link_search(ctx *goku.HttpContext) goku.ActionResulter {
ls := utils.LinkSearch{}
searchResult, err := ls.SearchLink(ctx.Get("term"), 1, golink.PAGE_SIZE)
if err == nil && searchResult.TimedOut == false && searchResult.HitResult.HitArray != nil && len(searchResult.HitResult.HitArray) > 0 {
links, _ := models.Link_GetByIdList(searchResult.HitResult.HitArray)
ctx.ViewData["Links"] = models.Link_ToVLink(links, ctx)
ctx.ViewData["HasMoreLink"] = len(links) >= golink.PAGE_SIZE
} else {
ctx.ViewData["Links"] = nil
ctx.ViewData["HasMoreLink"] = false
}
ctx.ViewData["Term"] = ctx.Get("term")
return ctx.Render("/link/search", nil)
}
// 加载更多的搜索link
func link_search_loadMore(ctx *goku.HttpContext) goku.ActionResulter {
term, _ := url.QueryUnescape(ctx.Get("term"))
page, err := strconv.Atoi(ctx.Get("page"))
success, hasmore := false, false
errorMsgs, html := "", ""
if err == nil && page > 1 {
ls := utils.LinkSearch{}
searchResult, err := ls.SearchLink(term, page, golink.PAGE_SIZE)
if err == nil && searchResult.TimedOut == false && searchResult.HitResult.HitArray != nil {
if len(searchResult.HitResult.HitArray) > 0 {
links, _ := models.Link_GetByIdList(searchResult.HitResult.HitArray)
if links != nil && len(links) > 0 {
ctx.ViewData["Links"] = models.Link_ToVLink(links, ctx)
vr := ctx.RenderPartial("loadmorelink", nil)
vr.Render(ctx, vr.Body)
html = vr.Body.String()
hasmore = len(links) >= golink.PAGE_SIZE
}
}
success = true
}
} else {
errorMsgs = "参数错误"
}
r := map[string]interface{}{
"success": success,
"errors": errorMsgs,
"html": html,
"hasmore": hasmore,
}
return ctx.Json(r)
}