use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class UserProcessor method exportPosts.
/**
* Exports posts(article/comment) to a file.
*
* @param context the specified context
* @param request the specified request
*/
@RequestProcessing(value = "/export/posts", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class })
public void exportPosts(final HTTPRequestContext context, final HttpServletRequest request) {
context.renderJSON();
final JSONObject user = (JSONObject) request.getAttribute(User.USER);
final String userId = user.optString(Keys.OBJECT_ID);
final String downloadURL = postExportService.exportPosts(userId);
if ("-1".equals(downloadURL)) {
context.renderJSONValue(Keys.MSG, langPropsService.get("insufficientBalanceLabel"));
} else if (StringUtils.isBlank(downloadURL)) {
return;
}
context.renderJSON(true).renderJSONValue("url", downloadURL);
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class UserProcessor method showLinkForge.
/**
* Shows user link forge.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @param userName the specified user name
* @throws Exception exception
*/
@RequestProcessing(value = "/member/{userName}/forge/link", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, UserBlockCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showLinkForge(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userName) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/home/link-forge.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final JSONObject user = (JSONObject) request.getAttribute(User.USER);
user.put(UserExt.USER_T_CREATE_TIME, new Date(user.getLong(Keys.OBJECT_ID)));
fillHomeUser(dataModel, user, roleQueryService);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
avatarQueryService.fillUserAvatarURL(avatarViewMode, user);
final String followingId = user.optString(Keys.OBJECT_ID);
dataModel.put(Follow.FOLLOWING_ID, followingId);
final boolean isLoggedIn = (Boolean) dataModel.get(Common.IS_LOGGED_IN);
if (isLoggedIn) {
final JSONObject currentUser = (JSONObject) dataModel.get(Common.CURRENT_USER);
final String followerId = currentUser.optString(Keys.OBJECT_ID);
final boolean isFollowing = followQueryService.isFollowing(followerId, followingId, Follow.FOLLOWING_TYPE_C_USER);
dataModel.put(Common.IS_FOLLOWING, isFollowing);
}
final List<JSONObject> tags = linkForgeQueryService.getUserForgedLinks(user.optString(Keys.OBJECT_ID));
dataModel.put(Tag.TAGS, (Object) tags);
dataModel.put(Common.TYPE, "linkForge");
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class UserProcessor method showHomeFollowingArticles.
/**
* Shows user home following articles page.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @param userName the specified user name
* @throws Exception exception
*/
@RequestProcessing(value = "/member/{userName}/following/articles", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class, UserBlockCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showHomeFollowingArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userName) throws Exception {
final JSONObject user = (JSONObject) request.getAttribute(User.USER);
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/home/following-articles.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModelService.fillHeaderAndFooter(request, response, dataModel);
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("userHomeFollowingArticlesCnt");
final int windowSize = Symphonys.getInt("userHomeFollowingArticlesWindowSize");
fillHomeUser(dataModel, user, roleQueryService);
final String followingId = user.optString(Keys.OBJECT_ID);
dataModel.put(Follow.FOLLOWING_ID, followingId);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
avatarQueryService.fillUserAvatarURL(avatarViewMode, user);
final JSONObject followingArticlesResult = followQueryService.getFollowingArticles(avatarViewMode, followingId, pageNum, pageSize);
final List<JSONObject> followingArticles = (List<JSONObject>) followingArticlesResult.opt(Keys.RESULTS);
dataModel.put(Common.USER_HOME_FOLLOWING_ARTICLES, followingArticles);
final boolean isLoggedIn = (Boolean) dataModel.get(Common.IS_LOGGED_IN);
if (isLoggedIn) {
final JSONObject currentUser = (JSONObject) dataModel.get(Common.CURRENT_USER);
final String followerId = currentUser.optString(Keys.OBJECT_ID);
final boolean isFollowing = followQueryService.isFollowing(followerId, followingId, Follow.FOLLOWING_TYPE_C_USER);
dataModel.put(Common.IS_FOLLOWING, isFollowing);
for (final JSONObject followingArticle : followingArticles) {
final String homeUserFollowingArticleId = followingArticle.optString(Keys.OBJECT_ID);
followingArticle.put(Common.IS_FOLLOWING, followQueryService.isFollowing(followerId, homeUserFollowingArticleId, Follow.FOLLOWING_TYPE_C_ARTICLE));
}
}
user.put(UserExt.USER_T_CREATE_TIME, new Date(user.getLong(Keys.OBJECT_ID)));
final int followingArticleCnt = followingArticlesResult.optInt(Pagination.PAGINATION_RECORD_COUNT);
final int pageCount = (int) Math.ceil(followingArticleCnt / (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(Pagination.PAGINATION_RECORD_COUNT, followingArticleCnt);
dataModel.put(Common.TYPE, "followingArticles");
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class VoteProcessor method voteDownArticle.
/**
* Votes down an article.
* <p>
* The request json object:
* <pre>
* {
* "dataId": ""
* }
* </pre>
* </p>
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/vote/down/article", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, PermissionCheck.class })
public void voteDownArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
context.renderJSON();
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
final String dataId = requestJSONObject.optString(Common.DATA_ID);
final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
final String userId = currentUser.optString(Keys.OBJECT_ID);
if (!Role.ROLE_ID_C_ADMIN.equals(currentUser.optString(User.USER_ROLE)) && voteQueryService.isOwn(userId, dataId, Vote.DATA_TYPE_C_ARTICLE)) {
context.renderFalseResult().renderMsg(langPropsService.get("cantVoteSelfLabel"));
return;
}
final int vote = voteQueryService.isVoted(userId, dataId);
if (Vote.TYPE_C_DOWN == vote) {
voteMgmtService.voteCancel(userId, dataId, Vote.DATA_TYPE_C_ARTICLE);
} else {
voteMgmtService.voteDown(userId, dataId, Vote.DATA_TYPE_C_ARTICLE);
final JSONObject article = articleQueryService.getArticle(dataId);
final String articleAuthorId = article.optString(Article.ARTICLE_AUTHOR_ID);
if (!VOTES.contains(userId + dataId) && !userId.equals(articleAuthorId)) {
final JSONObject notification = new JSONObject();
notification.put(Notification.NOTIFICATION_USER_ID, articleAuthorId);
notification.put(Notification.NOTIFICATION_DATA_ID, dataId + "-" + userId);
notificationMgmtService.addArticleVoteDownNotification(notification);
}
VOTES.add(userId + dataId);
}
context.renderTrueResult().renderJSONValue(Vote.TYPE, vote);
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class VoteProcessor method voteUpComment.
/**
* Votes up a comment.
* <p>
* The request json object:
* <pre>
* {
* "dataId": ""
* }
* </pre>
* </p>
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/vote/up/comment", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, PermissionCheck.class })
public void voteUpComment(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
context.renderJSON();
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
final String dataId = requestJSONObject.optString(Common.DATA_ID);
final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
final String userId = currentUser.optString(Keys.OBJECT_ID);
if (!Role.ROLE_ID_C_ADMIN.equals(currentUser.optString(User.USER_ROLE)) && voteQueryService.isOwn(userId, dataId, Vote.DATA_TYPE_C_COMMENT)) {
context.renderFalseResult().renderMsg(langPropsService.get("cantVoteSelfLabel"));
return;
}
final int vote = voteQueryService.isVoted(userId, dataId);
if (Vote.TYPE_C_UP == vote) {
voteMgmtService.voteCancel(userId, dataId, Vote.DATA_TYPE_C_COMMENT);
} else {
voteMgmtService.voteUp(userId, dataId, Vote.DATA_TYPE_C_COMMENT);
final JSONObject comment = commentQueryService.getComment(dataId);
final String commenterId = comment.optString(Comment.COMMENT_AUTHOR_ID);
if (!VOTES.contains(userId + dataId) && !userId.equals(commenterId)) {
final JSONObject notification = new JSONObject();
notification.put(Notification.NOTIFICATION_USER_ID, commenterId);
notification.put(Notification.NOTIFICATION_DATA_ID, dataId + "-" + userId);
notificationMgmtService.addCommentVoteUpNotification(notification);
}
VOTES.add(userId + dataId);
}
context.renderTrueResult().renderJSONValue(Vote.TYPE, vote);
}
Aggregations