This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathIndexController.java
More file actions
275 lines (242 loc) · 8.81 KB
/
IndexController.java
File metadata and controls
275 lines (242 loc) · 8.81 KB
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
package com.tale.controller;
import com.blade.ioc.annotation.Inject;
import com.blade.jdbc.core.OrderBy;
import com.blade.jdbc.page.Page;
import com.blade.kit.StringKit;
import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.http.Session;
import com.blade.mvc.ui.RestResponse;
import com.blade.security.web.csrf.CsrfToken;
import com.blade.validator.annotation.Valid;
import com.tale.exception.TipException;
import com.tale.extension.Commons;
import com.tale.init.TaleConst;
import com.tale.model.dto.Archive;
import com.tale.model.dto.ErrorCode;
import com.tale.model.dto.Types;
import com.tale.model.entity.Comments;
import com.tale.model.entity.Contents;
import com.tale.service.CommentsService;
import com.tale.service.ContentsService;
import com.tale.service.MetasService;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import java.net.URLEncoder;
import java.util.List;
import java.util.Optional;
/**
* 首页、归档、Feed、评论
*
* @author biezhi
* @since 1.3.1
*/
@Slf4j
@Path
public class IndexController extends BaseController {
@Inject
private ContentsService contentsService;
@Inject
private MetasService metasService;
@Inject
private CommentsService commentsService;
@Inject
private SiteService siteService;
/**
* 首页
*
* @return
*/
@GetRoute
public String index(Request request, @Param(defaultValue = "12") int limit) {
return this.index(request, 1, limit);
}
/**
* 自定义页面
*/
@CsrfToken(newToken = true)
@GetRoute(value = {"/:cid", "/:cid.html"})
public String page(@PathParam String cid, Request request) {
Optional<Contents> contentsOptional = contentsService.getContents(cid);
if (!contentsOptional.isPresent()) {
return this.render_404();
}
Contents contents = contentsOptional.get();
if (contents.getAllowComment()) {
int cp = request.queryInt("cp", 1);
request.attribute("cp", cp);
}
request.attribute("article", contents);
Contents temp = new Contents();
temp.setHits(contents.getHits() + 1);
temp.update(contents.getCid());
if (Types.ARTICLE.equals(contents.getType())) {
return this.render("post");
}
if (Types.PAGE.equals(contents.getType())) {
return this.render("page");
}
return this.render_404();
}
/**
* 首页分页
*
* @param request
* @param page
* @param limit
* @return
*/
@GetRoute(value = {"page/:page", "page/:page.html"})
public String index(Request request, @PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
// Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).page(page, limit, "created desc");
// request.attribute("articles", articles);
if (page > 1) {
this.title(request, "第" + page + "页");
}
request.attribute("page_num", page);
request.attribute("limit", limit);
request.attribute("is_home", true);
request.attribute("page_prefix", "/page");
return this.render("index");
}
/**
* 文章页
*/
@CsrfToken(newToken = true)
@GetRoute(value = {"article/:cid", "article/:cid.html"})
public String post(Request request, @PathParam String cid) {
Optional<Contents> contentsOptional = contentsService.getContents(cid);
if (!contentsOptional.isPresent()) {
return this.render_404();
}
Contents contents = contentsOptional.get();
if (Types.DRAFT.equals(contents.getStatus())) {
return this.render_404();
}
request.attribute("article", contents);
request.attribute("is_post", true);
if (contents.getAllowComment()) {
int cp = request.queryInt("cp", 1);
request.attribute("cp", cp);
}
Contents temp = new Contents();
temp.setHits(contents.getHits() + 1);
temp.update(contents.getCid());
return this.render("post");
}
/**
* 搜索页
*
* @param keyword
* @return
*/
@GetRoute(value = {"search/:keyword", "search/:keyword.html"})
public String search(Request request, @PathParam String keyword, @Param(defaultValue = "12") int limit) {
return this.search(request, keyword, 1, limit);
}
@GetRoute(value = {"search", "search.html"})
public String search(Request request, @Param(defaultValue = "12") int limit) {
String keyword = request.query("s").orElse("");
return this.search(request, keyword, 1, limit);
}
@GetRoute(value = {"search/:keyword/:page", "search/:keyword/:page.html"})
public String search(Request request, @PathParam String keyword, @PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH)
.like("title", "%" + keyword + "%").page(page, limit, "created desc");
request.attribute("articles", articles);
request.attribute("type", "搜索");
request.attribute("keyword", keyword);
request.attribute("page_prefix", "/search/" + keyword);
return this.render("page-category");
}
/**
* 归档页
*
* @return
*/
@GetRoute(value = {"archives", "archives.html"})
public String archives(Request request) {
List<Archive> archives = siteService.getArchives();
request.attribute("archives", archives);
request.attribute("is_archive", true);
return this.render("archives");
}
/**
* feed页
*
* @return
*/
@GetRoute(value = {"feed", "feed.xml", "atom.xml", "sitemap.xml"})
public void feed(Response response) {
List<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH)
.and("allow_feed", true)
.findAll(OrderBy.desc("created"));
try {
String xml = TaleUtils.getRssXml(articles);
response.contentType("text/xml; charset=utf-8");
response.body(xml);
} catch (Exception e) {
log.error("生成RSS失败", e);
}
}
/**
* 注销
*
* @param session
* @param response
*/
@Route(value = "logout")
public void logout(Session session, Response response) {
TaleUtils.logout(session, response);
}
/**
* 评论操作
*/
@CsrfToken(valid = true)
@PostRoute(value = "comment")
@JSON
public RestResponse comment(Request request, Response response,
@HeaderParam String Referer, @Valid Comments comments) {
if (StringKit.isBlank(Referer)) {
return RestResponse.fail(ErrorCode.BAD_REQUEST);
}
if (!Referer.startsWith(Commons.site_url())) {
return RestResponse.fail("非法评论来源");
}
String val = request.address() + ":" + comments.getCid();
Integer count = cache.hget(Types.COMMENTS_FREQUENCY, val);
if (null != count && count > 0) {
return RestResponse.fail("您发表评论太快了,请过会再试");
}
comments.setAuthor(TaleUtils.cleanXSS(comments.getAuthor()));
comments.setContent(TaleUtils.cleanXSS(comments.getContent()));
comments.setAuthor(EmojiParser.parseToAliases(comments.getAuthor()));
comments.setContent(EmojiParser.parseToAliases(comments.getContent()));
comments.setIp(request.address());
try {
commentsService.saveComment(comments);
response.cookie("tale_remember_author", URLEncoder.encode(comments.getAuthor(), "UTF-8"), 7 * 24 * 60 * 60);
response.cookie("tale_remember_mail", URLEncoder.encode(comments.getMail(), "UTF-8"), 7 * 24 * 60 * 60);
if (StringKit.isNotBlank(comments.getUrl())) {
response.cookie("tale_remember_url", URLEncoder.encode(comments.getUrl(), "UTF-8"), 7 * 24 * 60 * 60);
}
// 设置对每个文章30秒可以评论一次
cache.hset(Types.COMMENTS_FREQUENCY, val, 1, 30);
siteService.cleanCache(Types.C_STATISTICS);
return RestResponse.ok();
} catch (Exception e) {
String msg = "评论发布失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
log.error(msg, e);
}
return RestResponse.fail(msg);
}
}
}