use of com.erudika.para.core.utils.Pager in project scoold by Erudika.
the class QuestionController method changeSpaceForAllAnswers.
private void changeSpaceForAllAnswers(Post showPost, String space) {
if (showPost == null || showPost.isReply()) {
return;
}
Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
List<Reply> answerslist;
try {
do {
answerslist = pc.getChildren(showPost, Utils.type(Reply.class), pager);
for (Reply reply : answerslist) {
reply.setSpace(space);
}
if (!answerslist.isEmpty()) {
pc.updateAll(answerslist);
Thread.sleep(500);
}
} while (!answerslist.isEmpty());
} catch (InterruptedException ex) {
logger.error(null, ex);
Thread.currentThread().interrupt();
}
}
use of com.erudika.para.core.utils.Pager in project scoold by Erudika.
the class QuestionController method merge.
@PostMapping("/{id}/merge-into")
public String merge(@PathVariable String id, @RequestParam String id2, HttpServletRequest req) {
Post showPost = pc.read(id);
Post targetPost = pc.read(id2);
Profile authUser = utils.getAuthUser(req);
if (!(utils.canEdit(showPost, authUser) && utils.canEdit(targetPost, authUser)) || showPost == null || targetPost == null || showPost.isReply() || targetPost.isReply() || showPost.equals(targetPost)) {
return "redirect:" + req.getRequestURI();
}
if (utils.canEdit(showPost, authUser) && utils.canEdit(targetPost, authUser)) {
if (Config.getConfigBoolean("merge_question_bodies", true)) {
targetPost.setBody(targetPost.getBody() + "\n\n" + showPost.getBody());
}
targetPost.setAnswercount(targetPost.getAnswercount() + showPost.getAnswercount());
targetPost.setViewcount(targetPost.getViewcount() + showPost.getViewcount());
if (showPost.hasFollowers()) {
for (Map.Entry<String, String> entry : showPost.getFollowers().entrySet()) {
User u = new User(entry.getKey());
u.setEmail(entry.getValue());
targetPost.addFollower(u);
}
}
Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
List<Reply> answers;
do {
answers = pc.getChildren(showPost, Utils.type(Reply.class), pager);
for (Reply answer : answers) {
answer.setParentid(targetPost.getId());
answer.setTitle(targetPost.getTitle());
}
// overwrite
pc.createAll(answers);
} while (!answers.isEmpty());
targetPost.update();
showPost.delete();
}
return "redirect:" + targetPost.getPostLink(false, false);
}
use of com.erudika.para.core.utils.Pager in project scoold by Erudika.
the class SearchController method get.
@GetMapping({ "/search/{type}/{query}", "/search" })
public String get(@PathVariable(required = false) String type, @PathVariable(required = false) String query, @RequestParam(required = false) String q, HttpServletRequest req, Model model) {
if (!utils.isDefaultSpacePublic() && !utils.isAuthenticated(req)) {
return "redirect:" + SIGNINLINK + "?returnto=" + SEARCHLINK + "?q=" + Optional.ofNullable(query).orElse("*");
}
List<Profile> userlist = new ArrayList<Profile>();
List<Post> questionslist = new ArrayList<Post>();
List<Post> answerslist = new ArrayList<Post>();
List<Post> feedbacklist = new ArrayList<Post>();
List<Post> commentslist = new ArrayList<Post>();
Pager itemcount = utils.getPager("page", req);
String queryString = StringUtils.trimToEmpty(StringUtils.isBlank(q) ? query : q);
// [space query filter] + original query string
String qs = utils.sanitizeQueryString(queryString, req);
if ("questions".equals(type)) {
questionslist = utils.fullQuestionsSearch(qs, itemcount);
} else if ("answers".equals(type)) {
answerslist = pc.findQuery(Utils.type(Reply.class), qs, itemcount);
} else if ("feedback".equals(type) && utils.isFeedbackEnabled()) {
feedbacklist = pc.findQuery(Utils.type(Feedback.class), queryString, itemcount);
} else if ("people".equals(type)) {
userlist = pc.findQuery(Utils.type(Profile.class), getUsersSearchQuery(queryString, req), itemcount);
} else if ("comments".equals(type)) {
commentslist = pc.findQuery(Utils.type(Comment.class), qs, itemcount);
} else {
questionslist = utils.fullQuestionsSearch(qs);
answerslist = pc.findQuery(Utils.type(Reply.class), qs);
if (utils.isFeedbackEnabled()) {
feedbacklist = pc.findQuery(Utils.type(Feedback.class), queryString);
}
userlist = pc.findQuery(Utils.type(Profile.class), getUsersSearchQuery(queryString, req));
commentslist = pc.findQuery(Utils.type(Comment.class), qs, itemcount);
}
ArrayList<Post> list = new ArrayList<Post>();
list.addAll(questionslist);
list.addAll(answerslist);
list.addAll(feedbacklist);
utils.fetchProfiles(list);
model.addAttribute("path", "search.vm");
model.addAttribute("title", utils.getLang(req).get("search.title"));
model.addAttribute("searchSelected", "navbtn-hover");
model.addAttribute("showParam", type);
model.addAttribute("searchQuery", queryString);
model.addAttribute("itemcount", itemcount);
model.addAttribute("userlist", userlist);
model.addAttribute("questionslist", questionslist);
model.addAttribute("answerslist", answerslist);
model.addAttribute("feedbacklist", feedbacklist);
model.addAttribute("commentslist", commentslist);
return "base";
}
use of com.erudika.para.core.utils.Pager in project scoold by Erudika.
the class ScooldUtils method sendCommentNotifications.
public void sendCommentNotifications(Post parentPost, Comment comment, Profile commentAuthor, HttpServletRequest req) {
// send email notification to author of post except when the comment is by the same person
if (parentPost != null && comment != null) {
// parent author is not current user (authUser)
parentPost.setAuthor(pc.read(Profile.id(parentPost.getCreatorid())));
Map<String, Object> payload = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(comment, false));
payload.put("parent", parentPost);
payload.put("author", commentAuthor);
triggerHookEvent("comment.create", payload);
// get the last 5-6 commentators who want to be notified - https://github.com/Erudika/scoold/issues/201
Pager p = new Pager(1, Config._TIMESTAMP, false, 5);
boolean isCommentatorThePostAuthor = StringUtils.equals(parentPost.getCreatorid(), comment.getCreatorid());
Set<String> last5ids = pc.findChildren(parentPost, Utils.type(Comment.class), "!(" + Config._CREATORID + ":\"" + comment.getCreatorid() + "\")", p).stream().map(c -> c.getCreatorid()).distinct().collect(Collectors.toSet());
if (!isCommentatorThePostAuthor && !last5ids.contains(parentPost.getCreatorid())) {
last5ids = new HashSet<>(last5ids);
last5ids.add(parentPost.getCreatorid());
}
Map<String, String> lang = getLang(req);
List<Profile> last5commentators = pc.readAll(new ArrayList<>(last5ids));
last5commentators = last5commentators.stream().filter(u -> u.getCommentEmailsEnabled()).collect(Collectors.toList());
pc.readAll(last5commentators.stream().map(u -> u.getCreatorid()).collect(Collectors.toList())).forEach(author -> {
if (isCommentNotificationAllowed()) {
Map<String, Object> model = new HashMap<String, Object>();
String name = commentAuthor.getName();
String body = Utils.markdownToHtml(comment.getComment());
String pic = Utils.formatMessage("<img src='{0}' width='25'>", escapeHtmlAttribute(avatarRepository.getLink(commentAuthor, AvatarFormat.Square25)));
String postURL = getServerURL() + parentPost.getPostLink(false, false);
String subject = Utils.formatMessage(lang.get("notification.comment.subject"), name, parentPost.getTitle());
model.put("subject", escapeHtml(subject));
model.put("logourl", getSmallLogoUrl());
model.put("heading", Utils.formatMessage(lang.get("notification.comment.heading"), Utils.formatMessage("<a href='{0}'>{1}</a>", postURL, escapeHtml(parentPost.getTitle()))));
model.put("body", Utils.formatMessage("<h2>{0} {1}:</h2><div class='panel'>{2}</div>", pic, escapeHtml(name), body));
emailer.sendEmail(Arrays.asList(((User) author).getEmail()), subject, compileEmailTemplate(model));
}
});
}
}
use of com.erudika.para.core.utils.Pager in project scoold by Erudika.
the class ScooldUtils method pagerFromParams.
public Pager pagerFromParams(String pageParamName, HttpServletRequest req) {
Pager p = new Pager();
p.setPage(Math.min(NumberUtils.toLong(req.getParameter(pageParamName), 1), Config.MAX_PAGES));
p.setLimit(NumberUtils.toInt(req.getParameter("limit"), Config.MAX_ITEMS_PER_PAGE));
String lastKey = req.getParameter("lastKey");
String sort = req.getParameter("sortby");
String desc = req.getParameter("desc");
if (!StringUtils.isBlank(desc)) {
p.setDesc(Boolean.parseBoolean(desc));
}
if (!StringUtils.isBlank(lastKey)) {
p.setLastKey(lastKey);
}
if (!StringUtils.isBlank(sort)) {
p.setSortby(sort);
}
return p;
}
Aggregations