Search in sources :

Example 76 with Before

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);
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 77 with Before

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");
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Date(java.util.Date) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 78 with Before

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");
}
Also used : AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Date(java.util.Date) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 79 with Before

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);
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 80 with Before

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);
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

Before (org.b3log.latke.servlet.annotation.Before)169 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)169 JSONObject (org.json.JSONObject)166 After (org.b3log.latke.servlet.annotation.After)135 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)105 ServiceException (org.b3log.latke.service.ServiceException)37 Date (java.util.Date)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 ParseException (java.text.ParseException)6 List (java.util.List)6 ServletException (javax.servlet.ServletException)3 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1