use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ArticleProcessor method showArticle.
/**
* Shows article with the specified article id.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @param articleId the specified article id
* @throws Exception exception
*/
@RequestProcessing(value = "/article/{articleId}", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String articleId) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/article.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
final JSONObject article = articleQueryService.getArticleById(avatarViewMode, articleId);
if (null == article) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final HttpSession session = request.getSession(false);
if (null != session) {
session.setAttribute(Article.ARTICLE_T_ID, articleId);
}
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final String authorId = article.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject author = userQueryService.getUser(authorId);
Escapes.escapeHTML(author);
if (Article.ARTICLE_ANONYMOUS_C_PUBLIC == article.optInt(Article.ARTICLE_ANONYMOUS)) {
article.put(Article.ARTICLE_T_AUTHOR_NAME, author.optString(User.USER_NAME));
article.put(Article.ARTICLE_T_AUTHOR_URL, author.optString(User.USER_URL));
article.put(Article.ARTICLE_T_AUTHOR_INTRO, author.optString(UserExt.USER_INTRO));
} else {
article.put(Article.ARTICLE_T_AUTHOR_NAME, UserExt.ANONYMOUS_USER_NAME);
article.put(Article.ARTICLE_T_AUTHOR_URL, "");
article.put(Article.ARTICLE_T_AUTHOR_INTRO, "");
}
dataModel.put(Article.ARTICLE, article);
article.put(Common.IS_MY_ARTICLE, false);
article.put(Article.ARTICLE_T_AUTHOR, author);
article.put(Common.REWARDED, false);
article.put(Common.REWARED_COUNT, rewardQueryService.rewardedCount(articleId, Reward.TYPE_C_ARTICLE));
article.put(Article.ARTICLE_REVISION_COUNT, revisionQueryService.count(articleId, Revision.DATA_TYPE_C_ARTICLE));
articleQueryService.processArticleContent(article, request);
String cmtViewModeStr = request.getParameter("m");
JSONObject currentUser;
String currentUserId = null;
final boolean isLoggedIn = (Boolean) dataModel.get(Common.IS_LOGGED_IN);
if (isLoggedIn) {
currentUser = (JSONObject) dataModel.get(Common.CURRENT_USER);
currentUserId = currentUser.optString(Keys.OBJECT_ID);
article.put(Common.IS_MY_ARTICLE, currentUserId.equals(article.optString(Article.ARTICLE_AUTHOR_ID)));
final boolean isFollowing = followQueryService.isFollowing(currentUserId, articleId, Follow.FOLLOWING_TYPE_C_ARTICLE);
dataModel.put(Common.IS_FOLLOWING, isFollowing);
final boolean isWatching = followQueryService.isFollowing(currentUserId, articleId, Follow.FOLLOWING_TYPE_C_ARTICLE_WATCH);
dataModel.put(Common.IS_WATCHING, isWatching);
final int articleVote = voteQueryService.isVoted(currentUserId, articleId);
article.put(Article.ARTICLE_T_VOTE, articleVote);
if (currentUserId.equals(author.optString(Keys.OBJECT_ID))) {
article.put(Common.REWARDED, true);
} else {
article.put(Common.REWARDED, rewardQueryService.isRewarded(currentUserId, articleId, Reward.TYPE_C_ARTICLE));
}
if (Strings.isEmptyOrNull(cmtViewModeStr) || !Strings.isNumeric(cmtViewModeStr)) {
cmtViewModeStr = currentUser.optString(UserExt.USER_COMMENT_VIEW_MODE);
}
} else if (Strings.isEmptyOrNull(cmtViewModeStr) || !Strings.isNumeric(cmtViewModeStr)) {
cmtViewModeStr = "0";
}
final int cmtViewMode = Integer.valueOf(cmtViewModeStr);
dataModel.put(UserExt.USER_COMMENT_VIEW_MODE, cmtViewMode);
if (!(Boolean) request.getAttribute(Keys.HttpRequest.IS_SEARCH_ENGINE_BOT)) {
articleMgmtService.incArticleViewCount(articleId);
}
final JSONObject viewer = (JSONObject) request.getAttribute(User.USER);
if (null != viewer) {
livenessMgmtService.incLiveness(viewer.optString(Keys.OBJECT_ID), Liveness.LIVENESS_PV);
}
dataModelService.fillRelevantArticles(avatarViewMode, dataModel, article);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
// Qiniu file upload authenticate
final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
final String uploadToken = auth.uploadToken(Symphonys.get("qiniu.bucket"));
dataModel.put("qiniuUploadToken", uploadToken);
dataModel.put("qiniuDomain", Symphonys.get("qiniu.domain"));
if (!Symphonys.getBoolean("qiniu.enabled")) {
dataModel.put("qiniuUploadToken", "");
}
final long imgMaxSize = Symphonys.getLong("upload.img.maxSize");
dataModel.put("imgMaxSize", imgMaxSize);
final long fileMaxSize = Symphonys.getLong("upload.file.maxSize");
dataModel.put("fileMaxSize", fileMaxSize);
// Fill article thank
Stopwatchs.start("Fills article thank");
try {
article.put(Common.THANKED, rewardQueryService.isRewarded(currentUserId, articleId, Reward.TYPE_C_THANK_ARTICLE));
article.put(Common.THANKED_COUNT, rewardQueryService.rewardedCount(articleId, Reward.TYPE_C_THANK_ARTICLE));
} finally {
Stopwatchs.end();
}
// Fill previous/next article
final JSONObject previous = articleQueryService.getPreviousPermalink(articleId);
final JSONObject next = articleQueryService.getNextPermalink(articleId);
dataModel.put(Article.ARTICLE_T_PREVIOUS, previous);
dataModel.put(Article.ARTICLE_T_NEXT, next);
String stickConfirmLabel = langPropsService.get("stickConfirmLabel");
stickConfirmLabel = stickConfirmLabel.replace("{point}", Symphonys.get("pointStickArticle"));
dataModel.put("stickConfirmLabel", stickConfirmLabel);
dataModel.put("pointThankArticle", Symphonys.get("pointThankArticle"));
String pageNumStr = request.getParameter("p");
if (Strings.isEmptyOrNull(pageNumStr) || !Strings.isNumeric(pageNumStr)) {
pageNumStr = "1";
}
final int pageNum = Integer.valueOf(pageNumStr);
final int pageSize = Symphonys.getInt("articleCommentsPageSize");
final int windowSize = Symphonys.getInt("articleCommentsWindowSize");
final int commentCnt = article.getInt(Article.ARTICLE_COMMENT_CNT);
final int pageCount = (int) Math.ceil((double) commentCnt / (double) pageSize);
final List<Integer> pageNums = Paginator.paginate(pageNum, pageSize, pageCount, windowSize);
if (!pageNums.isEmpty()) {
dataModel.put(Pagination.PAGINATION_FIRST_PAGE_NUM, pageNums.get(0));
dataModel.put(Pagination.PAGINATION_LAST_PAGE_NUM, pageNums.get(pageNums.size() - 1));
}
dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
dataModel.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
dataModel.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
dataModel.put(Common.ARTICLE_COMMENTS_PAGE_SIZE, pageSize);
dataModel.put(Common.DISCUSSION_VIEWABLE, article.optBoolean(Common.DISCUSSION_VIEWABLE));
if (!article.optBoolean(Common.DISCUSSION_VIEWABLE)) {
article.put(Article.ARTICLE_T_COMMENTS, (Object) Collections.emptyList());
article.put(Article.ARTICLE_T_NICE_COMMENTS, (Object) Collections.emptyList());
return;
}
final List<JSONObject> niceComments = commentQueryService.getNiceComments(avatarViewMode, cmtViewMode, articleId, 3);
article.put(Article.ARTICLE_T_NICE_COMMENTS, (Object) niceComments);
double niceCmtScore = Double.MAX_VALUE;
if (!niceComments.isEmpty()) {
niceCmtScore = niceComments.get(niceComments.size() - 1).optDouble(Comment.COMMENT_SCORE, 0D);
for (final JSONObject comment : niceComments) {
String thankTemplate = langPropsService.get("thankConfirmLabel");
thankTemplate = thankTemplate.replace("{point}", String.valueOf(Symphonys.getInt("pointThankComment"))).replace("{user}", comment.optJSONObject(Comment.COMMENT_T_COMMENTER).optString(User.USER_NAME));
comment.put(Comment.COMMENT_T_THANK_LABEL, thankTemplate);
final String commentId = comment.optString(Keys.OBJECT_ID);
if (isLoggedIn) {
comment.put(Common.REWARDED, rewardQueryService.isRewarded(currentUserId, commentId, Reward.TYPE_C_COMMENT));
final int commentVote = voteQueryService.isVoted(currentUserId, commentId);
comment.put(Comment.COMMENT_T_VOTE, commentVote);
}
comment.put(Common.REWARED_COUNT, rewardQueryService.rewardedCount(commentId, Reward.TYPE_C_COMMENT));
}
}
// Load comments
final List<JSONObject> articleComments = commentQueryService.getArticleComments(avatarViewMode, articleId, pageNum, pageSize, cmtViewMode);
article.put(Article.ARTICLE_T_COMMENTS, (Object) articleComments);
// Fill comment thank
Stopwatchs.start("Fills comment thank");
try {
final String thankTemplate = langPropsService.get("thankConfirmLabel");
for (final JSONObject comment : articleComments) {
comment.put(Comment.COMMENT_T_NICE, comment.optDouble(Comment.COMMENT_SCORE, 0D) >= niceCmtScore);
final String thankStr = thankTemplate.replace("{point}", String.valueOf(Symphonys.getInt("pointThankComment"))).replace("{user}", comment.optJSONObject(Comment.COMMENT_T_COMMENTER).optString(User.USER_NAME));
comment.put(Comment.COMMENT_T_THANK_LABEL, thankStr);
final String commentId = comment.optString(Keys.OBJECT_ID);
if (isLoggedIn) {
comment.put(Common.REWARDED, rewardQueryService.isRewarded(currentUserId, commentId, Reward.TYPE_C_COMMENT));
final int commentVote = voteQueryService.isVoted(currentUserId, commentId);
comment.put(Comment.COMMENT_T_VOTE, commentVote);
}
comment.put(Common.REWARED_COUNT, rewardQueryService.rewardedCount(commentId, Reward.TYPE_C_COMMENT));
}
} finally {
Stopwatchs.end();
}
// Referral statistic
final String referralUserName = request.getParameter("r");
if (!UserRegisterValidation.invalidUserName(referralUserName)) {
final JSONObject referralUser = userQueryService.getUserByName(referralUserName);
if (null == referralUser) {
return;
}
final String viewerIP = Requests.getRemoteAddr(request);
final JSONObject referral = new JSONObject();
referral.put(Referral.REFERRAL_CLICK, 1);
referral.put(Referral.REFERRAL_DATA_ID, articleId);
referral.put(Referral.REFERRAL_IP, viewerIP);
referral.put(Referral.REFERRAL_TYPE, Referral.REFERRAL_TYPE_C_ARTICLE);
referral.put(Referral.REFERRAL_USER, referralUserName);
referralMgmtService.updateReferral(referral);
}
if (StringUtils.isBlank(article.optString(Article.ARTICLE_AUDIO_URL))) {
final String uid = StringUtils.isBlank(currentUserId) ? "visitor" : currentUserId;
articleMgmtService.genArticleAudio(article, uid);
}
if (StringUtils.isNotBlank(Symphonys.get("ipfs.dir"))) {
articleMgmtService.saveMarkdown(article);
}
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ArticleProcessor method showUpdateArticle.
/**
* Shows update article.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/update", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showUpdateArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final String articleId = request.getParameter("id");
if (Strings.isEmptyOrNull(articleId)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject article = articleQueryService.getArticle(articleId);
if (null == article) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
if (null == currentUser || !currentUser.optString(Keys.OBJECT_ID).equals(article.optString(Article.ARTICLE_AUTHOR_ID))) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/home/post.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put(Article.ARTICLE, article);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
// Qiniu file upload authenticate
final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
final String uploadToken = auth.uploadToken(Symphonys.get("qiniu.bucket"));
dataModel.put("qiniuUploadToken", uploadToken);
dataModel.put("qiniuDomain", Symphonys.get("qiniu.domain"));
if (!Symphonys.getBoolean("qiniu.enabled")) {
dataModel.put("qiniuUploadToken", "");
}
final long imgMaxSize = Symphonys.getLong("upload.img.maxSize");
dataModel.put("imgMaxSize", imgMaxSize);
final long fileMaxSize = Symphonys.getLong("upload.file.maxSize");
dataModel.put("fileMaxSize", fileMaxSize);
fillDomainsWithTags(dataModel);
String rewardEditorPlaceholderLabel = langPropsService.get("rewardEditorPlaceholderLabel");
rewardEditorPlaceholderLabel = rewardEditorPlaceholderLabel.replace("{point}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_ADD_ARTICLE_REWARD));
dataModel.put("rewardEditorPlaceholderLabel", rewardEditorPlaceholderLabel);
dataModel.put(Common.BROADCAST_POINT, Pointtransfer.TRANSFER_SUM_C_ADD_ARTICLE_BROADCAST);
final String b3logKey = currentUser.optString(UserExt.USER_B3_KEY);
dataModel.put("hasB3Key", !Strings.isEmptyOrNull(b3logKey));
fillPostArticleRequisite(dataModel, currentUser);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ArticleProcessor method showPreAddArticle.
/**
* Shows pre-add article.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/pre-post", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showPreAddArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/home/pre-post.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put(Common.BROADCAST_POINT, Pointtransfer.TRANSFER_SUM_C_ADD_ARTICLE_BROADCAST);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ChargeProcessor method showChargePoint.
/**
* Shows charge point.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/charge/point", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showChargePoint(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("charge-point.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class StatisticProcessor method showStatistic.
/**
* Shows data statistic.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/statistic", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showStatistic(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("statistic.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put("monthDays", monthDays);
dataModel.put("userCnts", userCnts);
dataModel.put("articleCnts", articleCnts);
dataModel.put("commentCnts", commentCnts);
dataModel.put("months", months);
dataModel.put("historyUserCnts", historyUserCnts);
dataModel.put("historyArticleCnts", historyArticleCnts);
dataModel.put("historyCommentCnts", historyCommentCnts);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
dataModel.put(Common.ONLINE_VISITOR_CNT, optionQueryService.getOnlineVisitorCount());
dataModel.put(Common.ONLINE_MEMBER_CNT, optionQueryService.getOnlineMemberCount());
final JSONObject statistic = optionQueryService.getStatistic();
dataModel.put(Option.CATEGORY_C_STATISTIC, statistic);
}
Aggregations