int page,
String filter,
String groupName,
int msgid) throws Exception {
Topic topic = messageDao.getById(msgid);
Template tmpl = Template.getTemplate(request);
Map<String, Object> params = new HashMap<>();
List<TagRef> tags = topicTagService.getTagRefs(topic);
MessageText messageText = msgbaseDao.getMessageText(topic.getId());
String plainText = lorCodeService.extractPlainText(messageText);
Future<List<List<MoreLikeThisTopic>>> moreLikeThis = moreLikeThisService.search(topic, tags, plainText);
PreparedTopic preparedMessage = messagePrepareService.prepareTopic(
topic,
tags,
request.isSecure(),
tmpl.getCurrentUser(),
messageText
);
Group group = preparedMessage.getGroup();
if (!group.getUrlName().equals(groupName) || group.getSectionId() != section.getId()) {
return new ModelAndView(new RedirectView(topic.getLink()));
}
boolean showDeleted = request.getParameter("deleted") != null;
if (showDeleted) {
page = -1;
}
if (!tmpl.isModeratorSession()) {
if (showDeleted && !"POST".equals(request.getMethod())) {
return new ModelAndView(new RedirectView(topic.getLink()));
}
}
if (page == -1 && !showDeleted) {
return new ModelAndView(new RedirectView(topic.getLink()));
}
int pages = topic.getPageCount(tmpl.getProf().getMessages());
if (page >= pages && (page > 0 || pages > 0)) {
if (pages==0) {
return new ModelAndView(new RedirectView(topic.getLink()));
} else {
return new ModelAndView(new RedirectView(topic.getLinkPage(pages - 1)));
}
}
if (showDeleted) {
if (!tmpl.isSessionAuthorized()) {
throw new BadInputException("Вы уже вышли из системы");
}
}
params.put("showDeleted", showDeleted);
User currentUser = AuthUtil.getCurrentUser();
permissionService.checkView(group, topic, currentUser, showDeleted);
params.put("message", topic);
params.put("preparedMessage", preparedMessage);
if (topic.isExpired()) {
response.setDateHeader("Expires", System.currentTimeMillis() + 30 * 24 * 60 * 60 * 1000L);
}
CommentList comments = commentService.getCommentList(topic, showDeleted);
if (messageText.isLorcode()) {
params.put("ogDescription", lorCodeService.trimPlainText(plainText, 250, true));
}
params.put("page", page);
params.put("group", group);
params.put("showAdsense", !tmpl.isSessionAuthorized() || !tmpl.getProf().isHideAdsense());
if (!tmpl.isSessionAuthorized()) { // because users have IgnoreList and memories
String etag = getEtag(topic);
response.setHeader("Etag", etag);
if (request.getHeader("If-None-Match") != null) {
if (etag.equals(request.getHeader("If-None-Match"))) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return null;
}
} else if (checkLastModified(webRequest, topic)) {
return null;
}
}
params.put("messageMenu", messagePrepareService.getTopicMenu(
preparedMessage,
currentUser,
request.isSecure(),
tmpl.getProf(),
true
));
Set<Integer> ignoreList;
if (currentUser != null) {
ignoreList = ignoreListDao.get(currentUser);
} else {
ignoreList = ImmutableSet.of();
}
int defaultFilterMode = getDefaultFilter(tmpl.getProf(), ignoreList.isEmpty());
int filterMode;
if (filter != null) {
filterMode = CommentFilter.parseFilterChain(filter);
if (!ignoreList.isEmpty() && filterMode == CommentFilter.FILTER_ANONYMOUS) {
filterMode += CommentFilter.FILTER_IGNORED;
}
} else {
filterMode = defaultFilterMode;
}
params.put("filterMode", CommentFilter.toString(filterMode));
params.put("defaultFilterMode", CommentFilter.toString(defaultFilterMode));
loadTopicScroller(params, topic, currentUser, !ignoreList.isEmpty());
Set<Integer> hideSet = commentService.makeHideSet(comments, filterMode, ignoreList);
CommentFilter cv = new CommentFilter(comments);
boolean reverse = tmpl.getProf().isShowNewFirst();
List<Comment> commentsFiltred = cv.getCommentsForPage(reverse, page, tmpl.getProf().getMessages(), hideSet);
List<Comment> commentsFull = cv.getCommentsForPage(reverse, page, tmpl.getProf().getMessages(), ImmutableSet.<Integer>of());
params.put("unfilteredCount", commentsFull.size());
List<PreparedComment> commentsPrepared = prepareService.prepareCommentList(
comments,
commentsFiltred,
request.isSecure(),
tmpl,
topic
);
params.put("commentsPrepared", commentsPrepared);
IPBlockInfo ipBlockInfo = ipBlockDao.getBlockInfo(request.getRemoteAddr());
params.put("ipBlockInfo", ipBlockInfo);
if (pages>1 && !showDeleted) {
params.put("pages", buildPages(topic, tmpl.getProf().getMessages(), filterMode, defaultFilterMode, page));
}
if (moreLikeThis!=null) {
params.put("moreLikeThis", moreLikeThisService.resultsOrNothing(moreLikeThis));
}