use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class NotificationProcessor method makeNotificationRead.
/**
* Makes article/comment read.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/notification/read", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = StopwatchEndAdvice.class)
public void makeNotificationRead(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
final String userId = currentUser.optString(Keys.OBJECT_ID);
final String articleId = requestJSONObject.optString(Article.ARTICLE_T_ID);
final List<String> commentIds = Arrays.asList(requestJSONObject.optString(Comment.COMMENT_T_IDS).split(","));
notificationMgmtService.makeRead(userId, articleId, commentIds);
context.renderJSON(true);
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class NotificationProcessor method navigateNotifications.
/**
* Navigates notifications.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/notifications", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = StopwatchEndAdvice.class)
public void navigateNotifications(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final JSONObject currentUser = userQueryService.getCurrentUser(request);
if (null == currentUser) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final String userId = currentUser.optString(Keys.OBJECT_ID);
final int unreadCommentedNotificationCnt = notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_COMMENTED);
if (unreadCommentedNotificationCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/commented");
return;
}
final int unreadReplyNotificationCnt = notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_REPLY);
if (unreadReplyNotificationCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/reply");
return;
}
final int unreadAtNotificationCnt = notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_AT) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_ARTICLE_NEW_FOLLOWER) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_ARTICLE_NEW_WATCHER) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_COMMENT_VOTE_UP) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_COMMENT_VOTE_DOWN) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_ARTICLE_VOTE_UP) + notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_ARTICLE_VOTE_DOWN);
if (unreadAtNotificationCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/at");
return;
}
final int unreadPointNotificationCnt = notificationQueryService.getUnreadPointNotificationCount(userId);
if (unreadPointNotificationCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/point");
return;
}
final int unreadFollowingNotificationCnt = notificationQueryService.getUnreadFollowingNotificationCount(userId);
if (unreadFollowingNotificationCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/following");
return;
}
final int unreadBroadcastCnt = notificationQueryService.getUnreadNotificationCountByType(userId, Notification.DATA_TYPE_C_BROADCAST);
if (unreadBroadcastCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/broadcast");
return;
}
final int unreadSysAnnounceCnt = notificationQueryService.getUnreadSysAnnounceNotificationCount(userId);
if (unreadSysAnnounceCnt > 0) {
response.sendRedirect(Latkes.getServePath() + "/notifications/sys-announce");
return;
}
response.sendRedirect(Latkes.getServePath() + "/notifications/commented");
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class NotificationProcessor method showBroadcastNotifications.
/**
* Shows [broadcast] notifications.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/notifications/broadcast", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showBroadcastNotifications(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final JSONObject currentUser = userQueryService.getCurrentUser(request);
if (null == currentUser) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/home/notifications/broadcast.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final String userId = currentUser.optString(Keys.OBJECT_ID);
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("broadcastNotificationsCnt");
final int windowSize = Symphonys.getInt("broadcastNotificationsWindowSize");
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
final JSONObject result = notificationQueryService.getBroadcastNotifications(avatarViewMode, userId, pageNum, pageSize);
final List<JSONObject> broadcastNotifications = (List<JSONObject>) result.get(Keys.RESULTS);
dataModel.put(Common.BROADCAST_NOTIFICATIONS, broadcastNotifications);
fillNotificationCount(userId, dataModel);
final int recordCnt = result.getInt(Pagination.PAGINATION_RECORD_COUNT);
final int pageCount = (int) Math.ceil((double) recordCnt / (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);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class SearchProcessor method search.
/**
* Searches.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/search", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void search(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("search-articles.ftl");
if (!Symphonys.getBoolean("es.enabled") && !Symphonys.getBoolean("algolia.enabled")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final Map<String, Object> dataModel = renderer.getDataModel();
String keyword = request.getParameter("key");
if (StringUtils.isBlank(keyword)) {
keyword = "";
}
dataModel.put(Common.KEY, Escapes.escapeHTML(keyword));
final String p = request.getParameter("p");
int pageNum = 1;
if (StringUtils.isNotBlank(p) && Strings.isNumeric(p)) {
pageNum = Integer.valueOf(p);
}
int pageSize = Symphonys.getInt("indexArticlesCnt");
final JSONObject user = userQueryService.getCurrentUser(request);
if (null != user) {
pageSize = user.optInt(UserExt.USER_LIST_PAGE_SIZE);
}
final List<JSONObject> articles = new ArrayList<>();
int total = 0;
if (Symphonys.getBoolean("es.enabled")) {
final JSONObject result = searchQueryService.searchElasticsearch(Article.ARTICLE, keyword, pageNum, pageSize);
if (null == result || 0 != result.optInt("status")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject hitsResult = result.optJSONObject("hits");
final JSONArray hits = hitsResult.optJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
final JSONObject article = hits.optJSONObject(i).optJSONObject("_source");
articles.add(article);
}
total = result.optInt("total");
}
if (Symphonys.getBoolean("algolia.enabled")) {
final JSONObject result = searchQueryService.searchAlgolia(keyword, pageNum, pageSize);
if (null == result) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONArray hits = result.optJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
final JSONObject article = hits.optJSONObject(i);
articles.add(article);
}
total = result.optInt("nbHits");
if (total > 1000) {
// Algolia limits the maximum number of search results to 1000
total = 1000;
}
}
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
articleQueryService.organizeArticles(avatarViewMode, articles);
final Integer participantsCnt = Symphonys.getInt("latestArticleParticipantsCnt");
articleQueryService.genParticipants(avatarViewMode, articles, participantsCnt);
dataModel.put(Article.ARTICLES, articles);
final int pageCount = (int) Math.ceil(total / (double) pageSize);
final List<Integer> pageNums = Paginator.paginate(pageNum, pageSize, pageCount, Symphonys.getInt("defaultPaginationWindowSize"));
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);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
String searchEmptyLabel = langPropsService.get("searchEmptyLabel");
searchEmptyLabel = searchEmptyLabel.replace("${key}", keyword);
dataModel.put("searchEmptyLabel", searchEmptyLabel);
}
use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.
the class SettingsProcessor method updateFunction.
/**
* Updates user function.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/settings/function", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, CSRFCheck.class })
public void updateFunction(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
context.renderJSON();
JSONObject requestJSONObject;
try {
requestJSONObject = Requests.parseRequestJSONObject(request, response);
request.setAttribute(Keys.REQUEST, requestJSONObject);
} catch (final Exception e) {
LOGGER.warn(e.getMessage());
requestJSONObject = new JSONObject();
}
String userListPageSizeStr = requestJSONObject.optString(UserExt.USER_LIST_PAGE_SIZE);
final int userCommentViewMode = requestJSONObject.optInt(UserExt.USER_COMMENT_VIEW_MODE);
final int userAvatarViewMode = requestJSONObject.optInt(UserExt.USER_AVATAR_VIEW_MODE);
final boolean notifyStatus = requestJSONObject.optBoolean(UserExt.USER_NOTIFY_STATUS);
final boolean subMailStatus = requestJSONObject.optBoolean(UserExt.USER_SUB_MAIL_STATUS);
final boolean keyboardShortcutsStatus = requestJSONObject.optBoolean(UserExt.USER_KEYBOARD_SHORTCUTS_STATUS);
int userListPageSize;
try {
userListPageSize = Integer.valueOf(userListPageSizeStr);
if (10 > userListPageSize) {
userListPageSize = 10;
}
if (userListPageSize > 60) {
userListPageSize = 60;
}
} catch (final Exception e) {
userListPageSize = Symphonys.getInt("indexArticlesCnt");
}
final JSONObject user = userQueryService.getCurrentUser(request);
user.put(UserExt.USER_LIST_PAGE_SIZE, userListPageSize);
user.put(UserExt.USER_COMMENT_VIEW_MODE, userCommentViewMode);
user.put(UserExt.USER_AVATAR_VIEW_MODE, userAvatarViewMode);
user.put(UserExt.USER_NOTIFY_STATUS, notifyStatus ? UserExt.USER_XXX_STATUS_C_ENABLED : UserExt.USER_XXX_STATUS_C_DISABLED);
user.put(UserExt.USER_SUB_MAIL_STATUS, subMailStatus ? UserExt.USER_XXX_STATUS_C_ENABLED : UserExt.USER_XXX_STATUS_C_DISABLED);
user.put(UserExt.USER_KEYBOARD_SHORTCUTS_STATUS, keyboardShortcutsStatus ? UserExt.USER_XXX_STATUS_C_ENABLED : UserExt.USER_XXX_STATUS_C_DISABLED);
try {
userMgmtService.updateUser(user.optString(Keys.OBJECT_ID), user);
context.renderTrueResult();
} catch (final ServiceException e) {
context.renderMsg(e.getMessage());
}
}
Aggregations